dev/core#3095 Permit setting of format_locale, prefer if set
authorEileen McNaughton <emcnaughton@wikimedia.org>
Thu, 3 Mar 2022 23:17:19 +0000 (12:17 +1300)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Thu, 3 Mar 2022 23:17:21 +0000 (12:17 +1300)
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

CRM/Admin/Form/Setting/Localization.php
CRM/Core/I18n.php
Civi/Core/Format.php
settings/Localization.setting.php
templates/CRM/Admin/Form/Setting/Localization.tpl

index 4273e9b98f55a3b3b6b1a75cae8114a35ab526a8..d18e75ab718fdcd4bd03708e1873466056a3b6ca 100644 (file)
@@ -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()) {
index f6ed3dcf03396e43a9bdb0950ddf152a89233d23..292b49c33ed608f65a2d046fb41dcb53816666e9 100644 (file)
@@ -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
index 582727cce57a1dfa81fd04bf16e7f2d52b038d35..b3076d0e8dc6b6f314c941c47729ec54e4480fc0 100644 (file)
@@ -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');
   }
 
   /**
index ec1be4f3bbfca877a7965c7094b1307b3a16cce8..4c09df3ad1a8fec927c9eeeab2a6fd73762e3867 100644 (file)
@@ -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',
index 754212bc0e1285489363b1596d1c67fee2811388..05fd577a9b322c79d1bcaf5ba78ea10d4de66487 100644 (file)
               <span class="description">{$settings_fields.contact_default_language.description}</span>
             </td>
           </tr>
-            <tr class="crm-localization-form-block-defaultCurrency">
-                <td class="label">{$form.defaultCurrency.label} {help id='defaultCurrency' title=$form.defaultCurrency.label}</td>
-                <td>{$form.defaultCurrency.html}</td>
-            </tr>
+          <tr class="crm-localization-form-block-defaultCurrency">
+            <td class="label">{$form.defaultCurrency.label} {help id='defaultCurrency' title=$form.defaultCurrency.label}</td>
+            <td>{$form.defaultCurrency.html}</td>
+          </tr>
+          <tr class="crm-localization-form-block-format_locale">
+            <td class="label">{$form.format_locale.label}</td>
+            <td>{$form.format_locale.html}<br />
+              <span class="description">{ts}Locale to use when formatting money (and in future dates). This replaces thousandsSeparator & decimalSeparator settings.{/ts}</span></td>
+          </tr>
             <tr class="crm-localization-form-block-monetaryThousandSeparator">
                 <td class="label">{$form.monetaryThousandSeparator.label}</td>
                 <td>{$form.monetaryThousandSeparator.html}</td>