Skip to content

Commit

Permalink
Merge pull request #22885 from eileenmcnaughton/lang
Browse files Browse the repository at this point in the history
dev/core#3095 Permit setting of format_locale, prefer if set
  • Loading branch information
seamuslee001 authored Mar 5, 2022
2 parents 26eefaf + 79f4e0b commit 682ce85
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 9 deletions.
5 changes: 2 additions & 3 deletions CRM/Admin/Form/Setting/Localization.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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()) {
Expand Down
22 changes: 22 additions & 0 deletions CRM/Core/I18n.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,28 @@ public static function languages($justEnabled = FALSE) {
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 getFormatLocales(): array {
$values = CRM_Core_OptionValue::getValues(['name' => 'languages'], $optionValues, 'label', TRUE);
$return = [];
$return[NULL] = ts('Inherit from language');
foreach ($values as $value) {
$return[$value['name']] = $value['label'];
}
// Sorry not sorry.
// Hacking in for now since the is probably the most important use-case for
// money formatting in an English speaking non-US locale based on any reasonable
// metric.
$return['en_NZ'] = ts('English (New Zealand)');
return $return;
}

/**
* Return the available UI languages
* @return array|string
Expand Down
4 changes: 2 additions & 2 deletions Civi/Core/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function money($amount, ?string $currency = NULL, ?string $locale = NULL)
$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);
Expand Down Expand Up @@ -150,7 +150,7 @@ public function moneyNumberLong($amount, ?string $currency, ?string $locale): st
* 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');
}

/**
Expand Down
21 changes: 21 additions & 0 deletions settings/Localization.setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,27 @@
'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' => [
'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::getFormatLocales',
],
'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',
Expand Down
13 changes: 9 additions & 4 deletions templates/CRM/Admin/Form/Setting/Localization.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,15 @@
<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>
Expand Down

0 comments on commit 682ce85

Please sign in to comment.