+ /**
+ * Binds a gettext domain, wrapper over bindtextdomain().
+ *
+ * @param $key Key of the extension (can be 'civicrm', or 'org.example.foo').
+ *
+ * @return Boolean True if the domain was changed for an extension.
+ */
+ function setGettextDomain($key) {
+ /* No domain changes for en_US */
+ if (! $this->_phpgettext) {
+ return FALSE;
+ }
+
+ // It's only necessary to find/bind once
+ if (! isset($this->_extensioncache[$key])) {
+ $config = CRM_Core_Config::singleton();
+
+ try {
+ $mapper = CRM_Extension_System::singleton()->getMapper();
+ $path = $mapper->keyToBasePath($key);
+ $info = $mapper->keyToInfo($key);
+ $domain = $info->file;
+
+ if ($this->_nativegettext) {
+ bindtextdomain($domain, $path . DIRECTORY_SEPARATOR . 'l10n');
+ bind_textdomain_codeset($domain, 'UTF-8');
+ $this->_extensioncache[$key] = $domain;
+ }
+ else {
+ // phpgettext
+ $mo_file = $path . DIRECTORY_SEPARATOR . 'l10n' . DIRECTORY_SEPARATOR . $config->lcMessages . DIRECTORY_SEPARATOR . 'LC_MESSAGES' . DIRECTORY_SEPARATOR . $domain . '.mo';
+ $streamer = new FileReader($mo_file);
+ $this->_extensioncache[$key] = new gettext_reader($streamer);
+ }
+ }
+ catch (CRM_Extension_Exception $e) {
+ // Intentionally not translating this string to avoid possible infinit loops
+ // Only developers should see this string, if they made a mistake in their ts() usage.
+ CRM_Core_Session::setStatus('Unknown extension key in a translation string: ' . $key, '', 'error');
+ $this->_extensioncache[$key] = FALSE;
+ }
+ }
+
+ if (isset($this->_extensioncache[$key]) && $this->_extensioncache[$key]) {
+ if ($this->_nativegettext) {
+ textdomain($this->_extensioncache[$key]);
+ }
+ else {
+ $this->_phpgettext = $this->_extensioncache[$key];
+ }
+
+ return TRUE;
+ }
+
+ return FALSE;
+ }
+