From 91cd40ead734c7e3b97a29328c66ff4714f90365 Mon Sep 17 00:00:00 2001 From: Mathieu Lutfy Date: Wed, 13 Mar 2013 18:28:07 -0400 Subject: [PATCH] CRM-11755 : support option.language for multi-lingual installations. --- api/api.php | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/api/api.php b/api/api.php index dcd430231c..b18318b967 100644 --- a/api/api.php +++ b/api/api.php @@ -41,6 +41,11 @@ function civicrm_api($entity, $action, $params, $extra = NULL) { $transaction = new CRM_Core_Transaction(); } + // support multi-lingual requests + if ($language = CRM_Utils_Array::value('option.language', $params)) { + _civicrm_api_set_locale($language); + } + _civicrm_api3_api_check_permission($apiRequest['entity'], $apiRequest['action'], $apiRequest['params']); // we do this before we @@ -540,3 +545,50 @@ function _civicrm_api_get_entity_name_from_dao($bao){ } + +/** + * Sets the tsLocale and dbLocale for multi-lingual sites. + * Some code duplication from CRM/Core/BAO/ConfigSetting.php retrieve() + * to avoid regressions from refactoring. + */ +function _civicrm_api_set_locale($lcMessagesRequest) { + // We must validate whether the locale is valid, otherwise setting a bad + // dbLocale could probably lead to sql-injection. + $domain = new CRM_Core_DAO_Domain(); + $domain->id = CRM_Core_Config::domainID(); + $domain->find(TRUE); + + if ($domain->config_backend) { + $defaults = unserialize($domain->config_backend); + + // are we in a multi-language setup? + $multiLang = $domain->locales ? TRUE : FALSE; + $lcMessages = NULL; + + // on multi-lang sites based on request and civicrm_uf_match + if ($multiLang) { + $languageLimit = array(); + if (array_key_exists('languageLimit', $defaults) && is_array($defaults['languageLimit'])) { + $languageLimit = $defaults['languageLimit']; + } + + if (in_array($lcMessagesRequest, array_keys($languageLimit))) { + $lcMessages = $lcMessagesRequest; + } + else { + throw new API_Exception(ts('Language not enabled: %1', array(1 => $lcMessagesRequest))); + } + } + + global $dbLocale; + + // set suffix for table names - use views if more than one language + if ($lcMessages) { + $dbLocale = $multiLang && $lcMessages ? "_{$lcMessages}" : ''; + + // FIXME: an ugly hack to fix CRM-4041 + global $tsLocale; + $tsLocale = $lcMessages; + } + } +} -- 2.25.1