Commit 921ed8ae authored by ayduns's avatar ayduns

Enable language selection on non-multilingual sites

parent fe810d04
......@@ -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));
......
......@@ -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;
}
......
......@@ -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.
*
......
......@@ -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'));
......
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();
});
......@@ -51,10 +51,12 @@
<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 />
......@@ -118,7 +120,7 @@
</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>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment