Backend\Core\Language\Language::setLocale PHP Метод

setLocale() публичный статический Метод

Set locale It will require the correct file and init the needed vars
public static setLocale ( string $language )
$language string The language to load.
    public static function setLocale($language)
    {
        $language = (string) $language;
        // validate file, generate it if needed
        if (!is_file(BACKEND_CACHE_PATH . '/Locale/en.json')) {
            BackendLocaleModel::buildCache('en', APPLICATION);
        }
        if (!is_file(BACKEND_CACHE_PATH . '/Locale/' . $language . '.json')) {
            // if you use the language in the console act like it is in the backend
            BackendLocaleModel::buildCache($language, defined('APPLICATION') && APPLICATION === 'Console' ? 'Backend' : APPLICATION);
        }
        // store
        self::$currentInterfaceLanguage = $language;
        // attempt to set a cookie
        try {
            // Needed to make it possible to use the backend language in the console.
            if (defined('APPLICATION') && APPLICATION !== 'Console') {
                CommonCookie::set('interface_language', $language);
            }
        } catch (\SpoonCookieException $e) {
            // settings cookies isn't allowed, because this isn't a real problem we ignore the exception
        }
        // set English translations, they'll be the fallback
        $translations = json_decode(file_get_contents(BACKEND_CACHE_PATH . '/Locale/en.json'), true);
        self::$err = (array) $translations['err'];
        self::$lbl = (array) $translations['lbl'];
        self::$msg = (array) $translations['msg'];
        // overwrite with the requested language's translations
        $translations = json_decode(file_get_contents(BACKEND_CACHE_PATH . '/Locale/' . $language . '.json'), true);
        $err = (array) $translations['err'];
        $lbl = (array) $translations['lbl'];
        $msg = (array) $translations['msg'];
        foreach ($err as $module => $translations) {
            if (!isset(self::$err[$module])) {
                self::$err[$module] = array();
            }
            self::$err[$module] = array_merge(self::$err[$module], $translations);
        }
        foreach ($lbl as $module => $translations) {
            if (!isset(self::$lbl[$module])) {
                self::$lbl[$module] = array();
            }
            self::$lbl[$module] = array_merge(self::$lbl[$module], $translations);
        }
        foreach ($msg as $module => $translations) {
            if (!isset(self::$msg[$module])) {
                self::$msg[$module] = array();
            }
            self::$msg[$module] = array_merge(self::$msg[$module], $translations);
        }
    }

Usage Example

Пример #1
0
 /**
  * Set locale
  * It will require the correct file and init the needed vars
  *
  * @param string $language The language to load.
  */
 public static function setLocale($language)
 {
     trigger_error('Backend\\Core\\Engine\\Language is deprecated.
          It has been moved to Backend\\Core\\Language\\Language', E_USER_DEPRECATED);
     parent::setLocale($language);
 }
All Usage Examples Of Backend\Core\Language\Language::setLocale