Skip to content
Snippets Groups Projects
Unverified Commit d9a54898 authored by eileen's avatar eileen :8ball: Committed by GitHub
Browse files

Merge pull request #13240 from aydun/langswitch2

translation#13 Enable users to choose their own language for accessing CiviCRM
parents 10bc9d11 0d9426af
No related branches found
No related tags found
No related merge requests found
......@@ -52,8 +52,16 @@ class CRM_Admin_Form_Setting_Localization extends CRM_Admin_Form_Setting {
'moneyformat' => CRM_Core_BAO_Setting::LOCALIZATION_PREFERENCES_NAME,
'moneyvalueformat' => CRM_Core_BAO_Setting::LOCALIZATION_PREFERENCES_NAME,
'provinceLimit' => CRM_Core_BAO_Setting::LOCALIZATION_PREFERENCES_NAME,
'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.
*/
......@@ -65,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)));
......@@ -226,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'));
......
......@@ -441,6 +441,28 @@ return array(
'callback' => 'CRM_Core_I18n::languages',
),
),
'uiLanguages' => array(
'group_name' => 'Localization Preferences',
'group' => 'localization',
'name' => 'uiLanguages',
'type' => 'Array',
'quick_form_type' => 'Select',
'html_type' => 'select',
'html_attributes' => array(
'multiple' => 1,
'class' => 'crm-select2',
),
'default' => NULL,
'add' => '5.9',
'title' => 'Available Languages',
'is_domain' => 1,
'is_contact' => 0,
'description' => '',
'help_text' => ts('User Interface languages available to users'),
'pseudoconstant' => array(
'callback' => 'CRM_Core_I18n::languages',
),
),
'lcMessages' => array(
'group_name' => 'Localization Preferences',
'group' => 'localization',
......
This diff is collapsed.
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,6 +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 />
......@@ -114,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>
......
......@@ -35,7 +35,6 @@ class CRM_Core_I18n_LocaleTest extends CiviUnitTestCase {
}
public function tearDown() {
CRM_Core_I18n_Schema::makeSinglelingual('en_US');
parent::tearDown();
}
......@@ -50,7 +49,52 @@ class CRM_Core_I18n_LocaleTest extends CiviUnitTestCase {
$locale = CRM_Core_I18n::getLocale();
$this->assertEquals($locale, 'fr_CA');
CRM_Core_I18n::singleton()->setLocale('en_US');
CRM_Core_I18n_Schema::makeSinglelingual('en_US');
Civi::$statics['CRM_Core_I18n']['singleton'] = [];
}
public function testUiLanguages() {
$languages = [
'en_US' => 'English (United States)',
'fr_CA' => 'French (Canada)',
'de_DE' => 'German',
];
$codes = array_keys($languages);
Civi::settings()->set('uiLanguages', $codes);
// Check we can retrieve the setting
$result = Civi::settings()->get('uiLanguages');
$this->assertEquals($codes, $result);
// Monolingual, codes
$result = CRM_Core_I18n::uiLanguages(TRUE);
$this->assertArrayValuesEqual($codes, $result);
// Monolingual, codes and language labels
$result = CRM_Core_I18n::uiLanguages();
$this->assertTreeEquals($languages, $result);
$this->enableMultilingual();
// Add fr_CA in db
CRM_Core_I18n_Schema::addLocale('fr_CA', 'en_US');
// Make fr_CA 'available'
Civi::settings()->set('languageLimit', ['en_US' => 1, 'fr_CA' => 1]);
// Multilingual, codes
$result = CRM_Core_I18n::uiLanguages(TRUE);
$this->assertArrayValuesEqual(['en_US', 'fr_CA'], $result);
// Multilingual, codes and language labels
$result = CRM_Core_I18n::uiLanguages();
$this->assertTreeEquals([
'en_US' => 'English (United States)',
'fr_CA' => 'French (Canada)',
], $result);
CRM_Core_I18n::singleton()->setLocale('en_US');
CRM_Core_I18n_Schema::makeSinglelingual('en_US');
Civi::$statics['CRM_Core_I18n']['singleton'] = [];
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment