From: Eileen McNaughton Date: Thu, 3 Mar 2022 23:17:19 +0000 (+1300) Subject: dev/core#3095 Permit setting of format_locale, prefer if set X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=57eda25db5a2e1efe1fcd328c75262fa70d9274a;p=civicrm-core.git dev/core#3095 Permit setting of format_locale, prefer if set This is a minimal patch to address https://lab.civicrm.org/dev/core/-/issues/3095 in time for 5.47. With this set it is possible to change the format locale to English, Canada or English, Australian (but not NZ!) and the currency will only be displayed before dollar amounts NOT of that currency. This should be enough to mitigate that regression feeling but missing are 1) fixing the admin form to hide irrelevant settings if format_locale is set 2) the psuedoconstant is cludgey - existing stuff doesn't seem to work so I added a function - also - if we ARE going to use this option group we should .... add NZ to it --- diff --git a/CRM/Admin/Form/Setting/Localization.php b/CRM/Admin/Form/Setting/Localization.php index 4273e9b98f..d18e75ab71 100644 --- a/CRM/Admin/Form/Setting/Localization.php +++ b/CRM/Admin/Form/Setting/Localization.php @@ -32,6 +32,7 @@ class CRM_Admin_Form_Setting_Localization extends CRM_Admin_Form_Setting { 'inheritLocale' => CRM_Core_BAO_Setting::LOCALIZATION_PREFERENCES_NAME, 'lcMessages' => CRM_Core_BAO_Setting::LOCALIZATION_PREFERENCES_NAME, 'legacyEncoding' => CRM_Core_BAO_Setting::LOCALIZATION_PREFERENCES_NAME, + 'format_locale' => CRM_Core_BAO_Setting::LOCALIZATION_PREFERENCES_NAME, 'monetaryThousandSeparator' => CRM_Core_BAO_Setting::LOCALIZATION_PREFERENCES_NAME, 'monetaryDecimalPoint' => CRM_Core_BAO_Setting::LOCALIZATION_PREFERENCES_NAME, 'moneyformat' => CRM_Core_BAO_Setting::LOCALIZATION_PREFERENCES_NAME, @@ -43,11 +44,9 @@ class CRM_Admin_Form_Setting_Localization extends CRM_Admin_Form_Setting { * Build the form object. */ public function buildQuickForm() { - $config = CRM_Core_Config::singleton(); - $this->setTitle(ts('Settings - Localization')); - $warningTitle = json_encode(ts("Warning")); + $warningTitle = json_encode(ts('Warning')); $defaultLocaleOptions = CRM_Admin_Form_Setting_Localization::getDefaultLocaleOptions(); if (CRM_Core_I18n::isMultiLingual()) { diff --git a/CRM/Core/I18n.php b/CRM/Core/I18n.php index f6ed3dcf03..292b49c33e 100644 --- a/CRM/Core/I18n.php +++ b/CRM/Core/I18n.php @@ -239,6 +239,21 @@ class CRM_Core_I18n { return $justEnabled ? $enabled : $all; } + /** + * Get the options available for format locale. + * + * Note the pseudoconstant can't be used as the key is the name not the value. + * + * @return array + */ + public static function getLocaleOptions(): array { + $values = CRM_Core_OptionValue::getValues(['name' => 'languages'], $optionValues, 'label', TRUE); + foreach ($values as $value) { + $return[$value['name']] = $value['label']; + } + return $return; + } + /** * Return the available UI languages * @return array|string diff --git a/Civi/Core/Format.php b/Civi/Core/Format.php index 582727cce5..b3076d0e8d 100644 --- a/Civi/Core/Format.php +++ b/Civi/Core/Format.php @@ -43,7 +43,7 @@ class Format { $currency = Civi::settings()->get('defaultCurrency'); } if (!isset($locale)) { - $locale = CRM_Core_I18n::getLocale(); + $locale = Civi::settings()->get('format_locale') ?? CRM_Core_I18n::getLocale(); } $money = Money::of($amount, $currency, NULL, RoundingMode::HALF_UP); $formatter = $this->getMoneyFormatter($currency, $locale); @@ -150,7 +150,7 @@ class Format { * we are looking at how to manage an 'opt in' */ protected function isUseSeparatorSettings(): bool { - return !CRM_Utils_Constant::value('IGNORE_SEPARATOR_CONFIG'); + return !Civi::settings()->get('format_locale') && !CRM_Utils_Constant::value('IGNORE_SEPARATOR_CONFIG'); } /** diff --git a/settings/Localization.setting.php b/settings/Localization.setting.php index ec1be4f3bb..4c09df3ad1 100644 --- a/settings/Localization.setting.php +++ b/settings/Localization.setting.php @@ -409,6 +409,28 @@ return [ 'callback' => 'CRM_Core_I18n::languages', ], ], + 'format_locale' => [ + 'group_name' => 'Localization Preferences', + 'group' => 'localization', + 'name' => 'format_locale', + 'type' => 'String', + 'quick_form_type' => 'Select', + 'html_type' => 'Select', + 'html_attributes' => [ + 'multiple' => 0, + 'class' => 'crm-select2', + ], + 'default' => NULL, + 'add' => '5.47', + 'title' => ts('Formatting locale'), + 'is_domain' => 1, + 'is_contact' => 0, + 'help_text' => NULL, + 'pseudoconstant' => [ + 'callback' => 'CRM_Core_I18n::getLocaleOptions', + ], + 'description' => ts('Locale to use when formatting money (and in future dates). This replaces thousandsSeparator & decimalSeparator & moneyFormat settings.'), + ], 'uiLanguages' => [ 'group_name' => 'Localization Preferences', 'group' => 'localization', diff --git a/templates/CRM/Admin/Form/Setting/Localization.tpl b/templates/CRM/Admin/Form/Setting/Localization.tpl index 754212bc0e..05fd577a9b 100644 --- a/templates/CRM/Admin/Form/Setting/Localization.tpl +++ b/templates/CRM/Admin/Form/Setting/Localization.tpl @@ -49,10 +49,15 @@ {$settings_fields.contact_default_language.description} - - {$form.defaultCurrency.label} {help id='defaultCurrency' title=$form.defaultCurrency.label} - {$form.defaultCurrency.html} - + + {$form.defaultCurrency.label} {help id='defaultCurrency' title=$form.defaultCurrency.label} + {$form.defaultCurrency.html} + + + {$form.format_locale.label} + {$form.format_locale.html}
+ {ts}Locale to use when formatting money (and in future dates). This replaces thousandsSeparator & decimalSeparator settings.{/ts} + {$form.monetaryThousandSeparator.label} {$form.monetaryThousandSeparator.html}