public function preDispatch($request) { try { $locale = new Zend_Locale(); $locale->setDefault('en'); $locale->setLocale(Zend_Locale::BROWSER); $requestedLanguage = key($locale->getBrowser()); $formatter = new Zend_Log_Formatter_Simple('%message%' . PHP_EOL); $writer = new Zend_Log_Writer_Stream(APPLICATION_LOG_PATH . 'translations.log'); $writer->setFormatter($formatter); $logger = new Zend_Log($writer); $frontendOptions = array('cache_id_prefix' => 'translation', 'lifetime' => 86400, 'automatic_serialization' => true); $backendOptions = array('cache_dir' => APPLICATION_CACHE_PATH); $cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions); $options = array('adapter' => 'gettext', 'scan' => Zend_Translate::LOCALE_FILENAME, 'content' => APPLICATION_PATH . '/languages/en/en.mo', 'locale' => 'auto', 'disableNotices' => true); $translate = new Zend_Translate($options); if (!$translate->isAvailable($locale->getLanguage())) { $locale->setLocale('en'); } else { $translate->setLocale($locale); } $translate->setCache($cache); Zend_Registry::set('locale', $locale->getLanguage()); Zend_Registry::set('Zend_Translate', $translate); } catch (Exception $e) { try { $writer = new Zend_Log_Writer_Stream(APPLICATION_LOG_PATH . 'plugin-locale.log'); $logger = new Zend_Log($writer); $logger->log($e->getMessage(), Zend_Log::ERR); } catch (Exception $e) { } } }
/** * Try very hard to get a locale for this user. Helps for i18n etc. * @return string */ public static function get_smart_locale($language = null) { require_once FRAMEWORK_PATH . '/thirdparty/Zend/Locale.php'; $locale = Zend_Locale::getBrowser(); if (!$locale) { if ($language) { return i18n::get_locale_from_lang($language); } else { return i18n::get_locale(); } } $locale = array_keys($locale); $firstPref = array_shift($locale); if (strpos($firstPref, '_') === false) { return i18n::get_locale_from_lang($language); } return $firstPref; }
/** * Get best available language setting from browser * if browser = es_AR and no es_AR.mo but es.mo, load es.mo * @return string language name */ public function getBestAvailableLanguageFromBrowser() { $acceptedLangsByBrowser = $this->_zLocale->getBrowser(); array_multisort($acceptedLangsByBrowser, SORT_DESC, SORT_NUMERIC); $currentLanguage = I18n::DEFAULT_LANGUAGE; if ($acceptedLangsByBrowser != null) { foreach ($acceptedLangsByBrowser as $acceptedLang => $quality) { $isAvailable = $this->_loadLanguage($acceptedLang); if ($isAvailable === FALSE) { $parts = explode('_', $acceptedLang); $isAvailable = $this->_loadLanguage($parts[0]); } if ($isAvailable) { $currentLanguage = $acceptedLang; break; } } } return $currentLanguage; }
protected function _addTranslationData($data, $locale, array $options = array()) { try { $locale = Zend_Locale::findLocale($locale); } catch (Zend_Locale_Exception $e) { require_once 'Zend/Translate/Exception.php'; throw new Zend_Translate_Exception("The given Language '{$locale}' does not exist"); } if ($options['clear'] || !isset($this->_translate[$locale])) { $this->_translate[$locale] = array(); } $ext = strtolower(ltrim(strrchr($data, '.'), '.')); if (!isset($this->_extToParser[$ext])) { return false; } $class = $this->_extToParser[$ext]; $temp = call_user_func(array($class, 'parse'), $data, $locale, $options); if (empty($temp)) { $temp = array(); } $keys = array_keys($temp); foreach ($keys as $key) { if (!isset($this->_translate[$key])) { $this->_translate[$key] = array(); } $this->_translate[$key] = $temp[$key] + $this->_translate[$key]; } if ($this->_automatic === true) { $find = new Zend_Locale($locale); $browser = $find->getEnvironment() + $find->getBrowser(); arsort($browser); foreach ($browser as $language => $quality) { if (isset($this->_translate[$language])) { $this->_options['locale'] = $language; break; } } } return $this; }
/** * Internal function for adding translation data * * This may be a new language or additional data for an existing language * If the options 'clear' is true, then the translation data for the specified * language is replaced and added otherwise * * @see Zend_Locale * @param array|Zend_Config $content Translation data to add * @throws Zend_Translate_Exception * @return Zend_Translate_Adapter Provides fluent interface */ private function _addTranslationData($options = array()) { if ($options instanceof Zend_Config) { $options = $options->toArray(); } else { if (func_num_args() > 1) { $args = func_get_args(); $options['content'] = array_shift($args); if (!empty($args)) { $options['locale'] = array_shift($args); } if (!empty($args)) { $options += array_shift($args); } } } if ($options['content'] instanceof Zend_Translate || $options['content'] instanceof Zend_Translate_Adapter) { $options['usetranslateadapter'] = true; if (!empty($options['locale']) && $options['locale'] !== 'auto') { $options['content'] = $options['content']->getMessages($options['locale']); } else { $content = $options['content']; $locales = $content->getList(); foreach ($locales as $locale) { $options['locale'] = $locale; $options['content'] = $content->getMessages($locale); $this->_addTranslationData($options); } return $this; } } try { $options['locale'] = Zend_Locale::findLocale($options['locale']); } catch (Zend_Locale_Exception $e) { #require_once 'Zend/Translate/Exception.php'; throw new Zend_Translate_Exception("The given Language '{$options['locale']}' does not exist", 0, $e); } if ($options['clear'] || !isset($this->_translate[$options['locale']])) { $this->_translate[$options['locale']] = array(); } $read = true; if (isset(self::$_cache)) { $id = 'Zend_Translate_' . md5(serialize($options['content'])) . '_' . $this->toString(); $temp = self::$_cache->load($id); if ($temp) { $read = false; } } if ($options['reload']) { $read = true; } if ($read) { if (!empty($options['usetranslateadapter'])) { $temp = array($options['locale'] => $options['content']); } else { $temp = $this->_loadTranslationData($options['content'], $options['locale'], $options); } } if (empty($temp)) { $temp = array(); } $keys = array_keys($temp); foreach ($keys as $key) { if (!isset($this->_translate[$key])) { $this->_translate[$key] = array(); } if (array_key_exists($key, $temp) && is_array($temp[$key])) { $this->_translate[$key] = $temp[$key] + $this->_translate[$key]; } } if ($this->_automatic === true) { $find = new Zend_Locale($options['locale']); $browser = $find->getEnvironment() + $find->getBrowser(); arsort($browser); foreach ($browser as $language => $quality) { if (isset($this->_translate[$language])) { $this->_options['locale'] = $language; break; } } } if ($read and isset(self::$_cache)) { $id = 'Zend_Translate_' . md5(serialize($options['content'])) . '_' . $this->toString(); if (self::$_cacheTags) { self::$_cache->save($temp, $id, array($this->_options['tag'])); } else { self::$_cache->save($temp, $id); } } return $this; }
/** * Internal function for adding translation data * * It may be a new language or additional data for existing language * If $clear parameter is true, then translation data for specified * language is replaced and added otherwise * * @see Zend_Locale * @param array|string $data Translation data * @param string|Zend_Locale $locale Locale/Language to add data for, identical with locale identifier, * @see Zend_Locale for more information * @param array $options (optional) Option for this Adapter * @throws Zend_Translate_Exception * @return Zend_Translate_Adapter Provides a fluid interface */ private function _addTranslationData($data, $locale, array $options = array()) { if (!($locale = Zend_Locale::isLocale($locale))) { /** * @see Zend_Translate_Exception */ require_once 'Zend/Translate/Exception.php'; throw new Zend_Translate_Exception("The given Language ({$locale}) does not exist"); } if (!array_key_exists($locale, $this->_translate)) { $this->_translate[$locale] = array(); } $this->_loadTranslationData($data, $locale, $options); if ($this->_automatic === true) { $find = new Zend_Locale($locale); $browser = $find->getBrowser() + $find->getEnvironment(); arsort($browser); foreach ($browser as $language => $quality) { if (array_key_exists($language, $this->_translate)) { $this->_options['locale'] = $language; break; } } } if (isset(self::$_cache)) { $id = 'Zend_Translate_' . $this->toString(); $temp = $this->_translate; $temp['_options_'] = $this->_options; self::$_cache->save(serialize($temp), $id); } return $this; }
protected function _initLanguage() { $available_languages = Core_Model_Language::getLanguageCodes(); $current_language = in_array($this->_request->getLanguageCode(), $available_languages) ? $this->_request->getLanguageCode() : ''; $language_session = Core_Model_Language::getSession(); $language = ''; if ($language_session) { $language = $language_session->current_language; } if (!empty($current_language)) { Core_Model_Language::setCurrentLanguage($current_language); } else { if (!empty($language)) { // $this->_request->setLanguageCode($language); } else { if ($accepted_languages = Zend_Locale::getBrowser()) { $accepted_languages = array_keys($accepted_languages); // $accepted_languages = preg_split('/(,)|(;)|(-)|(=)/', $accepted_languages); foreach ($accepted_languages as $lang) { if (in_array($lang, $available_languages)) { $current_language = $lang; break; } } if (!$current_language) { $current_language = Core_Model_Language::getDefaultLanguage(); } Core_Model_Language::setCurrentLanguage($current_language); } } } }
/** * Add translation data * * It may be a new language or additional data for existing language * If $clear parameter is true, then translation data for specified * language is replaced and added otherwise * * @param array|string $data Translation data * @param string|Zend_Locale $locale Locale/Language to add data for, identical with locale identifier, * see Zend_Locale for more information * @param array $options OPTIONAL Option for this Adapter * @throws Zend_Translate_Exception */ public function addTranslation($data, $locale, array $options = array()) { if (!$locale = Zend_Locale::isLocale($locale)) { throw new Zend_Translate_Exception("The given Language ({$locale}) does not exist"); } if (!in_array($locale, $this->_languages)) { $this->_languages[$locale] = $locale; } $this->_loadTranslationData($data, $locale, $options); if ($this->_automatic === true) { $find = new Zend_Locale($locale); $browser = $find->getBrowser() + $find->getEnvironment(); arsort($browser); foreach($browser as $language => $quality) { if (in_array($language, $this->_languages)) { $this->_locale = $language; break; } } } }
protected function _initLanguage() { $available_languages = Core_Model_Language::getLanguageCodes(); $current_language = in_array($this->getRequest()->getLanguageCode(), $available_languages) ? $this->getRequest()->getLanguageCode() : ""; $language_session = Core_Model_Language::getSession(); $language = ''; if (!$this->getRequest()->isApplication()) { if ($language_session->current_language) { $language = $language_session->current_language; } else { if (!$this->getRequest()->isInstalling()) { $current_language = System_Model_Config::getValueFor("system_default_language"); } } } else { $language = $language_session->current_language; } if (!empty($current_language)) { Core_Model_Language::setCurrentLanguage($current_language); } else { if (!empty($language)) { } else { if ($accepted_languages = Zend_Locale::getBrowser()) { $accepted_languages = array_keys($accepted_languages); foreach ($accepted_languages as $lang) { if (in_array($lang, $available_languages)) { $current_language = $lang; break; } } if (!$current_language) { $current_language = Core_Model_Language::getDefaultLanguage(); } Core_Model_Language::setCurrentLanguage($current_language); } else { Core_Model_Language::setCurrentLanguage(Core_Model_Language::getDefaultLanguage()); } } } }
/** * Initialize localization * * @return void */ protected function initializeLocalization() { $trFilePathPattern = $this->config['GUI_ROOT_DIR'] . '/i18n/locales/%s/LC_MESSAGES/%s.mo'; if (PHP_SAPI != 'cli') { $lang = iMSCP_Registry::set('user_def_lang', isset($_SESSION['user_def_lang']) ? $_SESSION['user_def_lang'] : (isset($this->config['USER_INITIAL_LANG']) ? $this->config['USER_INITIAL_LANG'] : 'auto')); if (Zend_Locale::isLocale($lang)) { $locale = new Zend_Locale($lang); if ($lang == 'auto') { $locale->setLocale('en_GB'); $browser = $locale->getBrowser(); arsort($browser); foreach ($browser as $language => $quality) { if (file_exists(sprintf($trFilePathPattern, $language, $language))) { $locale->setLocale($language); break; } } } elseif (!file_exists(sprintf($trFilePathPattern, $locale, $locale))) { $locale->setLocale('en_GB'); } } else { $locale = new Zend_Locale('en_GB'); } } else { $locale = new Zend_Locale('en_GB'); } // Setup cache object for translations $cache = Zend_Cache::factory('Core', 'File', array('caching' => true, 'lifetime' => null, 'automatic_serialization' => true, 'automatic_cleaning_factor' => 0, 'ignore_user_abort' => true, 'cache_id_prefix' => 'iMSCP_Translate'), array('hashed_directory_level' => 0, 'cache_dir' => CACHE_PATH . '/translations')); if ($this->config['DEBUG']) { $cache->clean(Zend_Cache::CLEANING_MODE_ALL); } else { Zend_Translate::setCache($cache); } // Setup primary translator for iMSCP core translations iMSCP_Registry::set('translator', new Zend_Translate(array('adapter' => 'gettext', 'content' => sprintf($trFilePathPattern, $locale, $locale), 'locale' => $locale, 'disableNotices' => true, 'tag' => 'iMSCP'))); }
/** * Check user browser and choose first valid locale. * * @return string|null */ protected function getBrowserLanguage() { $locale = null; // detection of user browser language preference $browser = \Zend_Locale::getBrowser(); $valid = Tool::getValidLanguages(); foreach ($browser as $lang => $p) { $lang = explode('_', $lang); if (in_array($lang[0], $valid)) { $locale = $lang[0]; break; } } return $locale; }
/** * Internal function for adding translation data * * It may be a new language or additional data for existing language * If $clear parameter is true, then translation data for specified * language is replaced and added otherwise * * @see Zend_Locale * @param array|string $data Translation data * @param string|Zend_Locale $locale Locale/Language to add data for, identical with locale identifier, * @see Zend_Locale for more information * @param array $options (optional) Option for this Adapter * @throws Zend_Translate_Exception * @return Zend_Translate_Adapter Provides fluent interface */ private function _addTranslationData($data, $locale, array $options = array()) { if (!Zend_Locale::isLocale($locale, true, false)) { if (!Zend_Locale::isLocale($locale, false, false)) { /** * @see Zend_Translate_Exception */ require_once 'Zend/Translate/Exception.php'; throw new Zend_Translate_Exception("The given Language ({$locale}) does not exist"); } $locale = new Zend_Locale($locale); } $locale = (string) $locale; if (isset($this->_translate[$locale]) === false) { $this->_translate[$locale] = array(); } $read = true; if (isset(self::$_cache)) { $id = 'Zend_Translate_' . preg_replace('/[^a-zA-Z0-9_]/', '_', $data) . '_' . $locale . '_' . $this->toString(); $result = self::$_cache->load($id); if ($result) { $this->_translate[$locale] = unserialize($result); $read = false; } } if ($read) { $this->_loadTranslationData($data, $locale, $options); } if ($this->_automatic === true) { $find = new Zend_Locale($locale); $browser = $find->getEnvironment() + $find->getBrowser(); arsort($browser); foreach ($browser as $language => $quality) { if (isset($this->_translate[$language]) === true) { $this->_options['locale'] = $language; break; } } } if ($read and isset(self::$_cache)) { $id = 'Zend_Translate_' . preg_replace('/[^a-zA-Z0-9_]/', '_', $data) . '_' . $locale . '_' . $this->toString(); self::$_cache->save(serialize($this->_translate[$locale]), $id); } return $this; }
/** * test getBrowser * expected true */ public function testgetBrowser() { putenv("HTTP_ACCEPT_LANGUAGE=,de,en-UK-US;q=0.5,fr_FR;q=0.2"); $value = new Zend_Locale(); $list = $value->getBrowser(); $this->assertTrue(isset($list['de'])); }
/** * Add translation data * * It may be a new language or additional data for existing language * If $clear parameter is true, then translation data for specified * language is replaced and added otherwise * * @param array|string $data Translation data * @param string|Zend_Locale $locale Locale/Language to add data for, identical with locale identifier, * see Zend_Locale for more information * @param array $options OPTIONAL Option for this Adapter * @throws Zend_Translate_Exception */ public function addTranslation($data, $locale, array $options = array()) { if (!($locale = Zend_Locale::isLocale($locale))) { throw new Zend_Translate_Exception("The given Language ({$locale}) does not exist"); } if (!array_key_exists($locale, $this->_translate)) { $this->_translate[$locale] = array(); } $this->_loadTranslationData($data, $locale, $options); if ($this->_automatic === true) { $find = new Zend_Locale($locale); $browser = $find->getBrowser() + $find->getEnvironment(); arsort($browser); foreach ($browser as $language => $quality) { if (array_key_exists($language, $this->_translate)) { $this->_locale = $language; break; } } } if (isset(self::$_cache)) { $id = 'Zend_Translate_' . $this->toString(); self::$_cache->save(serialize($this->_translate), $id); } }
/** * test getBrowser * expected true */ public function testBrowser() { $value = new Zend_Locale(); $default = $value->getBrowser(); $this->assertTrue(is_array($default)); }
/** * @return string */ public function getDefaultLocale() { $locales = $this->getLocales(); $default = array_keys(Zend_Locale::getBrowser()); if(!empty($default)) { if ($default[0] == 'en_US') { $default[] = 'en_GB'; } $defaultSelect = Shopware()->Db()->quote($default); $sql = 'SELECT locale, id FROM s_core_locales WHERE locale IN (' . $defaultSelect . ')'; $defaultIds = Shopware()->Db()->fetchPairs($sql); foreach($default as $key => $locale) { if(isset($defaultIds[$locale])) { $default[$key] = (int)$defaultIds[$locale]; } else { unset($default[$key]); } } $default = array_intersect($default, $locales); } return !empty($default) ? array_shift($default) : array_shift($locales); }
private static function detectLang(Zend_Controller_Request_Http &$request) { self::$_lang = self::$_config['langs']['default']; if (isset($_COOKIE['lang']) && array_key_exists($_COOKIE['lang'], self::$_config['langs']['supported'])) { self::$_lang = $_COOKIE['lang']; } else { $langs = Zend_Locale::getBrowser(); asort($langs); foreach (array_keys($langs) as $lang) { if (array_key_exists($lang, self::$_config['langs']['supported'])) { self::$_lang = $lang; } } } $locale = new Zend_Locale(self::$_lang); Zend_Registry::set('Zend_Locale', $locale); return self::$_lang; }
/** * test getBrowser * expected true */ public function testBrowser() { $value = new Zend_Locale(); $default = $value->getBrowser(); $this->assertTrue(is_array($default), 'No Environment Locale found'); }
/** * Find out locale from the request, settings or session * if language choice enabled, try the following: * - REQUEST parameter "lang" * - SESSION parameter "lang" * - Am_App::getUser->lang * - default in App * - en_US * else use latter 2 */ static function initLocale(Am_Di $di) { if (defined('AM_ADMIN') && AM_ADMIN) { Zend_Locale::setDefault('en_US'); } else { $possibleLang = array(); if ($di->config->get('lang.display_choice')) { $auth = $di->auth; $user = $auth->getUserId() ? $auth->getUser() : null; if (!empty($_REQUEST['_lang'])) { $possibleLang[] = filterId($_REQUEST['_lang']); } elseif (!empty($di->session->lang)) { $possibleLang[] = $di->session->lang; } elseif ($user && $user->lang) { $possibleLang[] = $user->lang; } $br = Zend_Locale::getBrowser(); arsort($br); $possibleLang = array_merge($possibleLang, array_keys($br)); } $possibleLang[] = $di->config->get('lang.default', 'en_US'); $possibleLang[] = 'en_US'; // last but not least // now choose the best candidate $enabledLangs = $di->config->get('lang.enabled', array()); $checked = array(); foreach ($possibleLang as $lc) { list($lang) = explode('_', $lc, 2); if (!in_array($lc, $enabledLangs) && !in_array($lang, $enabledLangs)) { continue; } if ($lc == $lang) { // we have not got entire locale,guess it if ($lc == 'en') { $lc = 'en_US'; } elseif ($lc == 'sv') { $lc = 'sv_SE'; } elseif ($lc == 'et') { $lc = 'et_EE'; } elseif ($lc == 'vi') { $lc = 'vi_VN'; } else { $lc = Zend_Locale::getLocaleToTerritory($lang); } if (!$lc && $lang == 'ko') { $lc = 'ko_KR'; } if (!$lc && $lang == 'ja') { $lc = 'ja_JP'; } if (!$lc && $lang == 'nb') { $lc = 'nb_NO'; } if (!$lc && $lang == 'zh') { $lc = 'zh_Hans'; } if (!$lc && $lang == 'el') { $lc = 'el_GR'; } if (!$lc && $lang == 'he') { $lc = 'he_IL'; } if (!$lc && $lang == 'da') { $lc = 'da_DK'; } if (!$lc && $lang == 'cs') { $lc = 'cs_CZ'; } if (!$lc && $lang == 'sq') { $lc = 'sq_AL'; } if (!$lc) { continue; } } if (isset($checked[$lc])) { continue; } $checked[$lc] = true; // check if locale file is exists $lc = preg_replace('/[^A-Za-z0-9_]/', '', $lc); if (!Zend_Locale::isLocale($lc)) { continue; } Zend_Locale::setDefault($lc); // then update user if it was request // and set to session break; } if ($di->config->get('lang.display_choice') && !empty($_REQUEST['_lang'])) { if (($_REQUEST['_lang'] == $lang || $_REQUEST['_lang'] == $lc) && $user && $user->lang != $lang) { $user->updateQuick('lang', $lc); } // set to session $di->session->lang = $lc; } } Zend_Registry::set('Zend_Locale', new Zend_Locale()); $amLocale = new Am_Locale(); Zend_Registry::set('Am_Locale', $amLocale); $di->locale = $amLocale; Zend_Locale_Format::setOptions(array('date_format' => $amLocale->getDateFormat())); }
/** * Format a number according to locale and currency * @param integer|float $number * @return string * @see Zend_Currency::toCurrency() */ public function toCurrency($value, $currency, $options = array()) { $options = array_merge(array('locale' => $this->getLocale(), 'display' => 2, 'precision' => 2), $options); // Doesn't like locales w/o regions if (is_object($options['locale'])) { $locale = $options['locale']->__toString(); } else { $locale = (string) $options['locale']; } if (strlen($locale) < 5) { $locale = Zend_Locale::getBrowser(); if (is_array($locale)) { foreach ($locale as $browserLocale => $q) { if (strlen($browserLocale) >= 5) { $locale = $browserLocale; break; } } } if (!$locale || strlen($locale) < 5) { $locale = 'en_US'; } } unset($options['locale']); $currency = new Zend_Currency($currency, $locale); return $currency->toCurrency($value, $options); }
/** * Internal function for adding translation data * * It may be a new language or additional data for existing language * If $clear parameter is true, then translation data for specified * language is replaced and added otherwise * * @see Zend_Locale * @param array|string $data Translation data * @param string|Zend_Locale $locale Locale/Language to add data for, identical with locale identifier, * @see Zend_Locale for more information * @param array $options (optional) Option for this Adapter * @throws Zend_Translate_Exception * @return Zend_Translate_Adapter Provides fluent interface */ private function _addTranslationData($data, $locale, array $options = array()) { try { $locale = Zend_Locale::findLocale($locale); } catch (Zend_Locale_Exception $e) { require_once 'Zend/Translate/Exception.php'; throw new Zend_Translate_Exception("The given Language '{$locale}' does not exist", 0, $e); } if ($options['clear'] || !isset($this->_translate[$locale])) { $this->_translate[$locale] = array(); } $read = true; if (isset(self::$_cache)) { $id = 'Zend_Translate_' . md5(serialize($data)) . '_' . $this->toString(); $temp = self::$_cache->load($id); if ($temp) { $read = false; } } if ($options['reload']) { $read = true; } if ($read) { $temp = $this->_loadTranslationData($data, $locale, $options); } if (empty($temp)) { $temp = array(); } $keys = array_keys($temp); foreach ($keys as $key) { if (!isset($this->_translate[$key])) { $this->_translate[$key] = array(); } if (array_key_exists($key, $temp) && is_array($temp[$key])) { $this->_translate[$key] = $temp[$key] + $this->_translate[$key]; } } if ($this->_automatic === true) { $find = new Zend_Locale($locale); $browser = $find->getEnvironment() + $find->getBrowser(); arsort($browser); foreach ($browser as $language => $quality) { if (isset($this->_translate[$language])) { $this->_options['locale'] = $language; break; } } } if ($read and isset(self::$_cache)) { $id = 'Zend_Translate_' . md5(serialize($data)) . '_' . $this->toString(); self::$_cache->save($temp, $id, array('Zend_Translate')); } return $this; }
/** * @return string */ public function getDefaultLocale() { $backendLocales = $this->getLocales(); $browserLocales = array_keys(Zend_Locale::getBrowser()); if (!empty($browserLocales)) { $quotedBackendLocale = Shopware()->Db()->quote($backendLocales); $orderIndex = 1; $orderCriteria = ''; foreach ($browserLocales as $browserLocale) { $orderCriteria .= 'WHEN ' . Shopware()->Db()->quote($browserLocale) . ' THEN ' . $orderIndex . ' '; $orderIndex++; } $orderCriteria .= 'ELSE ' . $orderIndex . ' END '; // For each browser locale, get exact or similar // filtered by allowed backend locales // ordered by exact match from browser $sql = 'SELECT id FROM s_core_locales WHERE locale LIKE :browserLocale AND id IN (' . $quotedBackendLocale . ') ORDER BY CASE locale ' . $orderCriteria . ' LIMIT 1'; foreach ($browserLocales as $key => $locale) { $fetchResult = Shopware()->Db()->fetchOne($sql, array('browserLocale' => $locale . '%')); if ($fetchResult) { return $fetchResult; } } } // No match from the browser locales, fallback to default shop locale $defaultShopLocale = Shopware()->Db()->fetchOne('SELECT locale_id FROM s_core_shops WHERE `default` = 1 AND active = 1 LIMIT 1'); // if default shop locale is allowed, use it, otherwise use the first allowed locale return in_array($defaultShopLocale, $backendLocales) ? $defaultShopLocale : array_shift($backendLocales); }