From fd1f3a266a087d557658f87979faece9ddf4161f Mon Sep 17 00:00:00 2001 From: Samuel Vanhove Date: Wed, 29 Apr 2015 18:38:16 -0400 Subject: [PATCH] CRM-16355 - bgm addon to change CMS locale --- CRM/Core/I18n.php | 7 ++++++- CRM/Mailing/BAO/MailingJob.php | 9 +++++++++ CRM/Utils/System.php | 19 ++++++++++++++++++- CRM/Utils/System/DrupalBase.php | 23 +++++++++++++++++++++++ CRM/Utils/System/Joomla.php | 8 ++++++++ CRM/Utils/System/WordPress.php | 8 ++++++++ 6 files changed, 72 insertions(+), 2 deletions(-) diff --git a/CRM/Core/I18n.php b/CRM/Core/I18n.php index eb14a52d33..6a4fdd0722 100644 --- a/CRM/Core/I18n.php +++ b/CRM/Core/I18n.php @@ -549,9 +549,14 @@ class CRM_Core_I18n { * Language (for example 'en_US', or 'fr_CA'). * True if the domain was changed for an extension. */ - public function setLanguage($language) { + public function setLocale($language) { $config = CRM_Core_Config::singleton(); + + // Change the language of the CMS as well, for URLs. + CRM_Utils_System::setUFLocale($language); + + // change the gettext ressources if ($this->_nativegettext) { $locale = $language . '.utf8'; putenv("LANG=$locale"); diff --git a/CRM/Mailing/BAO/MailingJob.php b/CRM/Mailing/BAO/MailingJob.php index a1a232c16b..81fdcc1928 100644 --- a/CRM/Mailing/BAO/MailingJob.php +++ b/CRM/Mailing/BAO/MailingJob.php @@ -544,6 +544,15 @@ VALUES (%1, %2, %3, %4, %5, %6, %7) $config = CRM_Core_Config::singleton(); } + if ($mailing->language && $mailing->language != 'en_US') { + // Since we are called from cron, CiviCRM runs in the default language, + // which causes issues for multi-lingual environments. + // NB: we do not switch back once the mailing is finished, + // assuming that CiviCRM stops running after the mail run. + $i18n = CRM_Core_I18n::singleton(); + $i18n->setLocale($mailing->language); + } + $job_date = CRM_Utils_Date::isoToMysql($this->scheduled_date); $fields = array(); diff --git a/CRM/Utils/System.php b/CRM/Utils/System.php index c9bcbab49a..1a4a2ddd3a 100644 --- a/CRM/Utils/System.php +++ b/CRM/Utils/System.php @@ -1331,7 +1331,7 @@ class CRM_Utils_System { } /** - * Get the locale set in the hosting CMS. + * Get the locale of the hosting CMS. * * @return string * The used locale or null for none. @@ -1341,6 +1341,23 @@ class CRM_Utils_System { return $config->userSystem->getUFLocale(); } + /** + * Set the locale of the hosting CMS. + * + * For example, a mailing will want to change the CMS language so that + * URLs are in the correct language (such as the Drupal language prefix). + * + * @param String $civicrm_language + * An array of parameters (see CRM_Utils_System::docURL2 method for names) + * + * @return bool + * Returns whether the locale was successfully changed. + */ + public static function setUFLocale($civicrm_language) { + $config = CRM_Core_Config::singleton(); + return $config->userSystem->setUFLocale($civicrm_language); + } + /** * Execute external or internal URLs and return server response. * diff --git a/CRM/Utils/System/DrupalBase.php b/CRM/Utils/System/DrupalBase.php index aeab9544e2..56cf330f94 100644 --- a/CRM/Utils/System/DrupalBase.php +++ b/CRM/Utils/System/DrupalBase.php @@ -425,6 +425,29 @@ abstract class CRM_Utils_System_DrupalBase extends CRM_Utils_System_Base { return CRM_Core_I18n_PseudoConstant::longForShort(substr($language->language, 0, 2)); } + /** + * @inheritDoc + */ + public function setUFLocale($civicrm_language) { + global $language; + + $langcode = substr($civicrm_language, 0, 2); + $languages = language_list(); + + if (isset($languages[$langcode])) { + $language = $languages[$langcode]; + + // Config must be re-initialized to reset the base URL + // otherwise links will have the wrong language prefix/domain. + $config = CRM_Core_Config::singleton(); + $config->free(); + + return TRUE; + } + + return FALSE; + } + /** * Perform any post login activities required by the UF - * e.g. for drupal: records a watchdog message about the new session, saves the login timestamp, diff --git a/CRM/Utils/System/Joomla.php b/CRM/Utils/System/Joomla.php index 018fc51a0d..03ca3fe485 100644 --- a/CRM/Utils/System/Joomla.php +++ b/CRM/Utils/System/Joomla.php @@ -462,6 +462,14 @@ class CRM_Utils_System_Joomla extends CRM_Utils_System_Base { return NULL; } + /** + * @inheritDoc + */ + public function setUFLocale($civicrm_language) { + // TODO + return TRUE; + } + /** * @inheritDoc */ diff --git a/CRM/Utils/System/WordPress.php b/CRM/Utils/System/WordPress.php index a1d4110f7e..56e57e6fbe 100644 --- a/CRM/Utils/System/WordPress.php +++ b/CRM/Utils/System/WordPress.php @@ -320,6 +320,14 @@ class CRM_Utils_System_WordPress extends CRM_Utils_System_Base { } } + /** + * @inheritDoc + */ + public function setUFLocale($civicrm_language) { + // TODO (probably not possible with WPML?) + return TRUE; + } + /** * Load wordpress bootstrap. * -- 2.25.1