From 5f0e9d40308fe4043ed5d329314e9ef37667c59f Mon Sep 17 00:00:00 2001 From: Tim Otten <totten@civicrm.org> Date: Tue, 21 Sep 2021 23:48:06 -0700 Subject: [PATCH] CRM_Utils_Date - Month and day names should match active locale --- CRM/Utils/Date.php | 18 ++++++++++++------ tests/phpunit/CRM/Utils/DateTest.php | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/CRM/Utils/Date.php b/CRM/Utils/Date.php index 632eb4910b..d2a7bab78d 100644 --- a/CRM/Utils/Date.php +++ b/CRM/Utils/Date.php @@ -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]; } /** diff --git a/tests/phpunit/CRM/Utils/DateTest.php b/tests/phpunit/CRM/Utils/DateTest.php index ac73ae3bb6..ca8305192a 100644 --- a/tests/phpunit/CRM/Utils/DateTest.php +++ b/tests/phpunit/CRM/Utils/DateTest.php @@ -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); + } + } + } -- 2.25.1