示例#1
0
 /**
  * Set current language
  * @param string $url
  */
 public static function setCurrent($url = null)
 {
     $language = self::getLanguageByUrl($url);
     self::$current = $language === null ? self::getDefaultLanguage() : $language;
     Yii::$app->language = static::$current->url;
 }
 /**
  * Load available languages.
  * @return array of language data
  * @TODO: Shouldn't get the current language
  */
 public static function getLanguages()
 {
     $key = 'languages';
     $data = Yii::$app->cache->get($key);
     if ($data === false) {
         $model = Languages::find()->indexBy('id')->orderBy('sort')->all();
         foreach ($model as $lang) {
             if ($lang->active && !empty($lang->name)) {
                 $data['languages'][$lang->locale] = $lang->name;
                 $data['flags'][$lang->locale] = self::getFlagUrl($lang);
             }
             if ($lang->default) {
                 $data['default'] = $lang->locale;
             }
         }
         Yii::$app->cache->set($key, $data, 0);
     }
     self::$languages = $data['languages'] ? $data['languages'] : array();
     self::$languagesFlags = $data['flags'];
     self::$languagesDefault = $data['default'];
     return self::$languages;
 }