CRM_Utils_Date - Month and day names should match active locale
authorTim Otten <totten@civicrm.org>
Wed, 22 Sep 2021 06:48:06 +0000 (23:48 -0700)
committerTim Otten <totten@civicrm.org>
Wed, 22 Sep 2021 20:54:43 +0000 (13:54 -0700)
CRM/Utils/Date.php
tests/phpunit/CRM/Utils/DateTest.php

index 632eb4910bbafd25080a7d9159f8f849615df7e7..d2a7bab78daaa66d81e460af5e209bfb1a71d886 100644 (file)
@@ -160,8 +160,10 @@ class CRM_Utils_Date {
    *
    */
   public static function getAbbrWeekdayNames() {
-    static $days = [];
+    $key = 'abbrDays_' . \CRM_Core_I18n::getLocale();
+    $days = &\Civi::$statics[__CLASS__][$key];
     if (!$days) {
+      $days = [];
       // First day of the week
       $firstDay = Civi::settings()->get('weekBegins');
 
@@ -189,8 +191,10 @@ class CRM_Utils_Date {
    *
    */
   public static function getFullWeekdayNames() {
-    static $days = [];
+    $key = 'fullDays_' . \CRM_Core_I18n::getLocale();
+    $days = &\Civi::$statics[__CLASS__][$key];
     if (!$days) {
+      $days = [];
       // First day of the week
       $firstDay = Civi::settings()->get('weekBegins');
 
@@ -214,7 +218,8 @@ class CRM_Utils_Date {
    *
    */
   public static function &getAbbrMonthNames($month = FALSE) {
-    static $abbrMonthNames;
+    $key = 'abbrMonthNames_' . \CRM_Core_I18n::getLocale();
+    $abbrMonthNames = &\Civi::$statics[__CLASS__][$key];
     if (!isset($abbrMonthNames)) {
 
       // set LC_TIME and build the arrays from locale-provided names
@@ -237,11 +242,12 @@ class CRM_Utils_Date {
    *
    */
   public static function &getFullMonthNames() {
-    if (empty(\Civi::$statics[__CLASS__]['fullMonthNames'])) {
+    $key = 'fullMonthNames_' . \CRM_Core_I18n::getLocale();
+    if (empty(\Civi::$statics[__CLASS__][$key])) {
       // Not relying on strftime because it depends on the operating system
       // and most people will not have a non-US locale configured out of the box
       // Ignoring other date names for now, since less visible by default
-      \Civi::$statics[__CLASS__]['fullMonthNames'] = [
+      \Civi::$statics[__CLASS__][$key] = [
         1 => ts('January'),
         2 => ts('February'),
         3 => ts('March'),
@@ -257,7 +263,7 @@ class CRM_Utils_Date {
       ];
     }
 
-    return \Civi::$statics[__CLASS__]['fullMonthNames'];
+    return \Civi::$statics[__CLASS__][$key];
   }
 
   /**
index ac73ae3bb6eca0e51f79137b0a0c6a6bf8805781..ca8305192aa2789d70eb36c29ffe0e90056f3d28 100644 (file)
@@ -306,4 +306,22 @@ class CRM_Utils_DateTest extends CiviUnitTestCase {
     ], $date);
   }
 
+  public function testLocalizeConsts() {
+    $expect['en_US'] = ['Jan', 'Tue', 'March', 'Thursday'];
+    $expect['fr_FR'] = ['janv.', 'mar.', 'Mars', 'jeudi'];
+    $expect['es_MX'] = ['ene', 'mar', 'Marzo', 'jueves'];
+
+    foreach ($expect as $lang => $expectNames) {
+      $useLocale = CRM_Utils_AutoClean::swapLocale($lang);
+      $actualNames = [
+        CRM_Utils_Date::getAbbrMonthNames()[1],
+        CRM_Utils_Date::getAbbrWeekdayNames()[2],
+        CRM_Utils_Date::getFullMonthNames()[3],
+        CRM_Utils_Date::getFullWeekdayNames()[4],
+      ];
+      $this->assertEquals($expectNames, $actualNames, "Check temporal names in $lang");
+      unset($useLocale);
+    }
+  }
+
 }