From: Aidan Saunders Date: Wed, 5 Dec 2018 21:32:26 +0000 (+0000) Subject: Enable language selection on non-multilingual sites X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=921ed8ae7518661c7d659060cc8bf732cb0e4d52;p=civicrm-core.git Enable language selection on non-multilingual sites --- diff --git a/CRM/Admin/Form/Setting/Localization.php b/CRM/Admin/Form/Setting/Localization.php index 0658d4db38..92f573fe75 100644 --- a/CRM/Admin/Form/Setting/Localization.php +++ b/CRM/Admin/Form/Setting/Localization.php @@ -55,6 +55,13 @@ class CRM_Admin_Form_Setting_Localization extends CRM_Admin_Form_Setting { 'uiLanguages' => CRM_Core_BAO_Setting::LOCALIZATION_PREFERENCES_NAME, ); + public function preProcess() { + if (!CRM_Core_I18n::isMultiLingual()) { + CRM_Core_Resources::singleton() + ->addScriptFile('civicrm', 'templates/CRM/Admin/Form/Setting/Localization.js', 1, 'html-header'); + } + } + /** * Build the form object. */ @@ -66,10 +73,7 @@ class CRM_Admin_Form_Setting_Localization extends CRM_Admin_Form_Setting { $warningTitle = json_encode(ts("Warning")); $defaultLocaleOptions = CRM_Admin_Form_Setting_Localization::getDefaultLocaleOptions(); - $domain = new CRM_Core_DAO_Domain(); - $domain->find(TRUE); - - if ($domain->locales) { + if (CRM_Core_I18n::isMultiLingual()) { // add language limiter and language adder $this->addCheckBox('languageLimit', ts('Available Languages'), array_flip($defaultLocaleOptions), NULL, NULL, NULL, NULL, '   '); $this->addElement('select', 'addLanguage', ts('Add Language'), array_merge(array('' => ts('- select -')), array_diff(CRM_Core_I18n::languages(), $defaultLocaleOptions))); @@ -227,6 +231,11 @@ class CRM_Admin_Form_Setting_Localization extends CRM_Admin_Form_Setting { $values['languageLimit'][$values['addLanguage']] = 1; } + // current language should be in the ui list + if (!in_array($values['lcMessages'], $values['uiLanguages'])) { + $values['uiLanguages'][] = $values['lcMessages']; + } + // if we manipulated the language list, return to the localization admin screen $return = (bool) (CRM_Utils_Array::value('makeMultilingual', $values) or CRM_Utils_Array::value('addLanguage', $values)); diff --git a/CRM/Core/BAO/ConfigSetting.php b/CRM/Core/BAO/ConfigSetting.php index 4b5f3111e8..c016dabdf1 100644 --- a/CRM/Core/BAO/ConfigSetting.php +++ b/CRM/Core/BAO/ConfigSetting.php @@ -141,20 +141,22 @@ class CRM_Core_BAO_ConfigSetting { $session = CRM_Core_Session::singleton(); - // on multi-lang sites based on request and civicrm_uf_match - if ($multiLang) { - $languageLimit = array(); - if (is_array($settings->get('languageLimit'))) { - $languageLimit = $settings->get('languageLimit'); - } - + $permittedLanguages = CRM_Core_I18n::uiLanguages(TRUE); + + // The locale to be used can come from various places: + // - the request (url) + // - the session + // - civicrm_uf_match + // - inherited from the CMS + // Only look at this if there is actually a choice of permitted languages + if (count($permittedLanguages) >= 2) { $requestLocale = CRM_Utils_Request::retrieve('lcMessages', 'String'); - if (in_array($requestLocale, array_keys($languageLimit))) { + if (in_array($requestLocale, $permittedLanguages)) { $chosenLocale = $requestLocale; //CRM-8559, cache navigation do not respect locale if it is changed, so reseting cache. // Ed: This doesn't sound good. - CRM_Core_BAO_Cache::deleteGroup('navigation'); + // CRM_Core_BAO_Cache::deleteGroup('navigation'); } else { $requestLocale = NULL; @@ -162,7 +164,7 @@ class CRM_Core_BAO_ConfigSetting { if (!$requestLocale) { $sessionLocale = $session->get('lcMessages'); - if (in_array($sessionLocale, array_keys($languageLimit))) { + if (in_array($sessionLocale, $permittedLanguages)) { $chosenLocale = $sessionLocale; } else { @@ -184,7 +186,7 @@ class CRM_Core_BAO_ConfigSetting { $ufm = new CRM_Core_DAO_UFMatch(); $ufm->contact_id = $session->get('userID'); if ($ufm->find(TRUE) && - in_array($ufm->language, array_keys($languageLimit)) + in_array($ufm->language, $permittedLanguages) ) { $chosenLocale = $ufm->language; } diff --git a/CRM/Core/I18n.php b/CRM/Core/I18n.php index 62f339f0a7..df1a0ebfe4 100644 --- a/CRM/Core/I18n.php +++ b/CRM/Core/I18n.php @@ -223,7 +223,7 @@ class CRM_Core_I18n { } } - ksort($all); + asort($all); } if ($enabled === NULL) { @@ -241,6 +241,28 @@ class CRM_Core_I18n { return $justEnabled ? $enabled : $all; } + /** + * Return the available UI languages + * @return array(string languageCode) if $justCodes + * array(string languageCode => string languageName) if !$justCodes + */ + public static function uiLanguages($justCodes = FALSE) { + // In multilang we only allow the languages that are configured in db + // Otherwise, the languages configured in uiLanguages + $settings = Civi::settings(); + if (CRM_Core_I18n::isMultiLingual()) { + $codes = array_keys((array) $settings->get('languageLimit')); + } + else { + $codes = $settings->get('uiLanguages'); + if (!$codes) { + $codes = [$settings->get('lcMessages')]; + } + } + return $justCodes ? $codes + : CRM_Utils_Array::subset(CRM_Core_I18n::languages(), $codes); + } + /** * Replace arguments in a string with their values. Arguments are represented by % followed by their number. * diff --git a/CRM/Core/Smarty.php b/CRM/Core/Smarty.php index 0d89e7cb61..155c41cb71 100644 --- a/CRM/Core/Smarty.php +++ b/CRM/Core/Smarty.php @@ -151,7 +151,7 @@ class CRM_Core_Smarty extends Smarty { // CRM-7163 hack: we don’t display langSwitch on upgrades anyway if (!CRM_Core_Config::isUpgradeMode()) { - $this->assign('langSwitch', CRM_Core_I18n::languages(TRUE)); + $this->assign('langSwitch', CRM_Core_I18n::uiLanguages()); } $this->register_function('crmURL', array('CRM_Utils_System', 'crmURL')); diff --git a/templates/CRM/Admin/Form/Setting/Localization.js b/templates/CRM/Admin/Form/Setting/Localization.js new file mode 100644 index 0000000000..f9eb8b9f71 --- /dev/null +++ b/templates/CRM/Admin/Form/Setting/Localization.js @@ -0,0 +1,16 @@ +CRM.$(function($) { + $('input[name=inheritLocale]').click(function () { + showHideUiLanguages(); + }); + + function showHideUiLanguages() { + var val = $('input[name=inheritLocale]:checked').val(); + if(val == 0) { + $('.crm-localization-form-block-uiLanguages').show(); + } else { + $('.crm-localization-form-block-uiLanguages').hide(); + } + } + + showHideUiLanguages(); +}); diff --git a/templates/CRM/Admin/Form/Setting/Localization.tpl b/templates/CRM/Admin/Form/Setting/Localization.tpl index cb9c390f4f..0b8d179831 100644 --- a/templates/CRM/Admin/Form/Setting/Localization.tpl +++ b/templates/CRM/Admin/Form/Setting/Localization.tpl @@ -51,10 +51,12 @@ {$form.inheritLocale.label} {help id='inheritLocale' title=$form.inheritLocale.label} {$form.inheritLocale.html} + {if !$form.languageLimit} {$form.uiLanguages.label} {$form.uiLanguages.html} + {/if} {$form.contact_default_language.label} {$form.contact_default_language.html}
@@ -118,7 +120,7 @@

{ts}Multiple Languages Support{/ts}

- {if $form.languageLimit} + {if $form.makeSinglelingual}
{ts 1="http://documentation.civicrm.org"}This is a multilingual installation. It contains certain schema differences compared to regular installations of CiviCRM. Please refer to the documentation for details.{/ts}