Enable language selection on non-multilingual sites
authorAidan Saunders <aidan.saunders@squiffle.uk>
Wed, 5 Dec 2018 21:32:26 +0000 (21:32 +0000)
committerAidan Saunders <aidan.saunders@squiffle.uk>
Mon, 10 Dec 2018 10:49:26 +0000 (10:49 +0000)
CRM/Admin/Form/Setting/Localization.php
CRM/Core/BAO/ConfigSetting.php
CRM/Core/I18n.php
CRM/Core/Smarty.php
templates/CRM/Admin/Form/Setting/Localization.js [new file with mode: 0644]
templates/CRM/Admin/Form/Setting/Localization.tpl

index 0658d4db387916ec5c1ff99a7bbc5f9e1b0a559c..92f573fe75b1384bcbedcca6e1afda939fada86b 100644 (file)
@@ -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, ' &nbsp; ');
       $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));
 
index 4b5f3111e80fdc882809dc02ea6a01ebd5c1f53e..c016dabdf10d827842c0c066416dd234411af618 100644 (file)
@@ -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;
         }
index 62f339f0a7beddad45490c082721bf32697a6b6c..df1a0ebfe49f622bb6141951a7e7e1b1d8e08e87 100644 (file)
@@ -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.
    *
index 0d89e7cb61843f11a8efe23331a08ceb4fd629e2..155c41cb7147155ef1b53aab15f96232b6d633a2 100644 (file)
@@ -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 (file)
index 0000000..f9eb8b9
--- /dev/null
@@ -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();
+});
index cb9c390f4fcb855e258cde387c4aef2631da0495..0b8d17983190b5c5c547452783a5b04dabf838c5 100644 (file)
                 <td class="label">{$form.inheritLocale.label} {help id='inheritLocale' title=$form.inheritLocale.label}</td>
                 <td>{$form.inheritLocale.html}</td>
             </tr>
+          {if !$form.languageLimit}
             <tr class="crm-localization-form-block-uiLanguages">
                 <td class="label">{$form.uiLanguages.label}</td>
                 <td>{$form.uiLanguages.html}</td>
             </tr>
+          {/if}
           <tr class="crm-localization-form-contact_default_language">
             <td class="label">{$form.contact_default_language.label}</td>
             <td>{$form.contact_default_language.html}<br />
         </table>
     <h3>{ts}Multiple Languages Support{/ts}</h3>
       <table class="form-layout-compressed">
-        {if $form.languageLimit}
+        {if $form.makeSinglelingual}
           <tr class="crm-localization-form-block-makeSinglelingual_description">
               <td></td>
               <td><span class="description">{ts 1="http://documentation.civicrm.org"}This is a multilingual installation. It contains certain schema differences compared to regular installations of CiviCRM. Please <a href="%1">refer to the documentation</a> for details.{/ts}</span></td>