From cb305aeffe7243ac143a451f6b6b43c0bf1d83e3 Mon Sep 17 00:00:00 2001 From: Mathieu Lutfy Date: Wed, 25 Jan 2023 15:42:26 -0500 Subject: [PATCH] Allow extension gettext mo files to live in the I18N resource dir CiviCRM core already supports having the civicrm.mo files in a custom directory, defined by the CIVICRM_L10N_BASEDIR constant. With this patch, it will also be possible to have the 'mo' files from extensions in that directory. It will be assumed that if an extension is called "foo" then the mo file will be foo.mo and in the same directory as civicrm.mo. --- CRM/Core/I18n.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/CRM/Core/I18n.php b/CRM/Core/I18n.php index 01e6aef7fe..c9db571c32 100644 --- a/CRM/Core/I18n.php +++ b/CRM/Core/I18n.php @@ -565,19 +565,25 @@ class CRM_Core_I18n { // It's only necessary to find/bind once if (!isset($this->_extensioncache[$key])) { try { + $path = CRM_Core_I18n::getResourceDir(); $mapper = CRM_Extension_System::singleton()->getMapper(); - $path = $mapper->keyToBasePath($key); $info = $mapper->keyToInfo($key); $domain = $info->file; + // Support extension .mo files outside the CiviCRM codebase (relates to dev/translation#52) + if (!file_exists(CRM_Core_I18n::getResourceDir() . $this->locale . DIRECTORY_SEPARATOR . 'LC_MESSAGES' . DIRECTORY_SEPARATOR . $domain . '.mo')) { + // Extensions that are not on Transifed might have their .po/mo files in their git repo + $path = $mapper->keyToBasePath($key) . DIRECTORY_SEPARATOR . 'l10n' . DIRECTORY_SEPARATOR; + } + if ($this->_nativegettext) { - bindtextdomain($domain, $path . DIRECTORY_SEPARATOR . 'l10n'); + bindtextdomain($domain, $path); bind_textdomain_codeset($domain, 'UTF-8'); $this->_extensioncache[$key] = $domain; } else { // phpgettext - $mo_file = $path . DIRECTORY_SEPARATOR . 'l10n' . DIRECTORY_SEPARATOR . $this->locale . DIRECTORY_SEPARATOR . 'LC_MESSAGES' . DIRECTORY_SEPARATOR . $domain . '.mo'; + $mo_file = $path . $this->locale . DIRECTORY_SEPARATOR . 'LC_MESSAGES' . DIRECTORY_SEPARATOR . $domain . '.mo'; $streamer = new FileReader($mo_file); $this->_extensioncache[$key] = $streamer->length() ? new gettext_reader($streamer) : NULL; } -- 2.25.1