Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRM_Utils_Date - Month and day names should match active locale #21569

Merged
merged 2 commits into from
Sep 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions CRM/Utils/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,10 @@ public static function format($date, $separator = '', $invalidDate = 0) {
*
*/
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');

Expand Down Expand Up @@ -189,8 +191,10 @@ public static function getAbbrWeekdayNames() {
*
*/
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');

Expand All @@ -214,7 +218,8 @@ public static function getFullWeekdayNames() {
*
*/
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
Expand All @@ -237,11 +242,12 @@ public static function &getAbbrMonthNames($month = FALSE) {
*
*/
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'),
Expand All @@ -257,7 +263,7 @@ public static function &getFullMonthNames() {
];
}

return \Civi::$statics[__CLASS__]['fullMonthNames'];
return \Civi::$statics[__CLASS__][$key];
}

/**
Expand Down
18 changes: 18 additions & 0 deletions tests/phpunit/CRM/Utils/DateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,4 +306,22 @@ public function testRelativeEarlierDay() {
], $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);
}
}

}
11 changes: 7 additions & 4 deletions tests/phpunit/Civi/Token/TokenProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,20 +171,23 @@ public function testRowTokens() {
}

public function testRenderLocalizedSmarty() {
\CRM_Utils_Time::setTime('2022-04-08 16:32:04');
$resetTime = \CRM_Utils_AutoClean::with(['CRM_Utils_Time', 'resetTime']);
$this->dispatcher->addSubscriber(new \CRM_Core_DomainTokens());
$this->dispatcher->addSubscriber(new TokenCompatSubscriber());
$p = new TokenProcessor($this->dispatcher, [
'controller' => __CLASS__,
'smarty' => TRUE,
]);
$p->addMessage('text', '{ts}Yes{/ts} {ts}No{/ts}', 'text/plain');
$p->addMessage('text', '{ts}Yes{/ts} {ts}No{/ts} {domain.now|crmDate:"%B"}', 'text/plain');
$p->addRow([]);
$p->addRow(['locale' => 'fr_FR']);
$p->addRow(['locale' => 'es_MX']);

$expectText = [
'Yes No',
'Oui Non',
'Sí No',
'Yes No April',
'Oui Non Avril',
'Sí No Abril',
];

$rowCount = 0;
Expand Down