/**
  * @return array
  */
 protected function getData()
 {
     /**@var  $currentLanguage Language */
     list($route, $params) = Yii::$app->getUrlManager()->parseRequest(Yii::$app->getRequest());
     $route = Yii::$app->controller->getRoute() != '' ? Yii::$app->controller->route : $route;
     $params = ArrayHelper::merge($_GET, $params);
     $data = [0];
     foreach ($this->languages as $language) {
         if ($language['code'] === Yii::$app->language) {
             $this->current = ArrayHelper::merge(['url' => $this->urlManager->createUrl(ArrayHelper::merge($params, [array_key_exists('route', $params) ? $params['route'] : $route, 'language' => $language['code']]))], $language);
             $data[0] = $this->current;
         } else {
             $data[] = ArrayHelper::merge(['url' => $this->urlManager->createUrl(ArrayHelper::merge($params, [array_key_exists('route', $params) ? $params['route'] : $route, 'language' => $language['code']]))], $language);
         }
     }
     if (!array_key_exists(0, $data) || $data[0] === 0) {
         $currentLanguage = Language::findOne(['code' => Yii::$app->language]);
         if ($currentLanguage) {
             $data[0] = ['url' => $this->urlManager->createUrl(ArrayHelper::merge($params, [array_key_exists('route', $params) ? $params['route'] : $route, 'language' => $currentLanguage->code])), 'code' => $currentLanguage->code, 'name' => $currentLanguage->name, 'country' => $currentLanguage->country];
         }
     }
     if (count($data) > count(Language::getLanguages())) {
         return array_unique($data);
     } else {
         return $data;
     }
 }
Exemplo n.º 2
0
 /**
  * Initializes the object.
  * This method is called at the end of the constructor.
  * The default implementation will trigger an [[EVENT_INIT]] event.
  * If you override this method, make sure you call the parent implementation at the end
  * to ensure triggering of the event.
  */
 public function init()
 {
     $this->languages = Language::getLanguages();
     foreach ($this->languages as $language) {
         $this->_dynamicField[$language->code] = $language->name;
     }
 }
Exemplo n.º 3
0
 /**
  * {@inheritDoc}
  */
 public function init()
 {
     parent::init();
     $languages = ArrayHelper::map(Language::getLanguages(), 'code', 'name');
     if (isset($_GET['language']) && isset($languages[$_GET['language']]) && $_GET['language'] != Yii::$app->request->getCookies()->getValue('_language')) {
         Yii::$app->session['language'] = $_GET['language'];
         $cookie = new Cookie(['name' => '_language', 'value' => $_GET['language'], 'path' => '/']);
         Yii::$app->language = $_GET['language'];
         Yii::$app->response->cookies->add($cookie);
         Yii::$app->response->refresh();
     }
     if (!Yii::$app->request->cookies->has('_language')) {
         if (Module::hasSetting()) {
             $cookie = new Cookie(['name' => '_language', 'value' => Yii::$app->setting->general_language, 'path' => '/']);
             Yii::$app->language = Yii::$app->setting->general_language;
         } else {
             $cookie = new Cookie(['name' => '_language', 'value' => Yii::$app->language, 'path' => '/']);
         }
         Yii::$app->response->cookies->add($cookie);
     } else {
         Yii::$app->language = Yii::$app->request->getCookies()->getValue('_language');
     }
 }
 /**
  * Return all translated attributes include value
  *
  * @param ActiveRecord $model
  * @param null         $language_code
  *
  * @return array
  * @throws InvalidConfigException
  */
 public static function attributes($model, $language_code = null)
 {
     $behavior = $model->behaviors();
     if (!isset($behavior['ml'])) {
         throw new InvalidConfigException("MultiLanguage was not defined in " . get_class($model) . ".");
     }
     $response = [];
     foreach (Language::getLanguages() as $language) {
         foreach ($behavior['ml']['attributes'] as $mlAttribute) {
             $attribute = $mlAttribute . '_' . $language->code;
             if ($language_code != null) {
                 if ($language_code == $language->code) {
                     $response[$attribute] = isset($model->behaviors['ml']) ? $model->{$attribute} : '';
                 }
             } else {
                 $response[$attribute] = isset($model->behaviors['ml']) ? $model->{$attribute} : '';
             }
         }
     }
     return $response;
 }