Inheritance: extends yii\db\ActiveRecord
コード例 #1
0
 /**
  * @param array $params Search conditions.
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $translateLanguage = Yii::$app->request->get('language_id', Yii::$app->sourceLanguage);
     $sourceLanguage = $this->_getSourceLanguage();
     $query = LanguageSource::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $dataProvider->setSort(['attributes' => ['id', 'category', 'message', 'translation' => ['asc' => ['lt.translation' => SORT_ASC], 'desc' => ['lt.translation' => SORT_DESC], 'label' => Yii::t('language', 'Translation')]]]);
     if (!($this->load($params) && $this->validate())) {
         $query->joinWith(['languageTranslate' => function ($query) use($translateLanguage) {
             $query->from(['lt' => LanguageTranslate::tableName()])->onCondition(['lt.language' => $translateLanguage]);
         }]);
         $query->joinWith(['languageTranslateByLanguage' => function ($query) use($sourceLanguage) {
             $query->from(['ts' => LanguageTranslate::tableName()])->onCondition(['ts.language' => $sourceLanguage]);
         }]);
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'category' => $this->category]);
     $query->andFilterWhere(['or', $this->createLikeExpression('message', $this->message), $this->createLikeExpression('ts.translation', $this->message)]);
     $query->joinWith(['languageTranslate' => function ($query) use($translateLanguage) {
         $query->from(['lt' => LanguageTranslate::tableName()])->onCondition(['lt.language' => $translateLanguage])->andFilterWhere($this->createLikeExpression('lt.translation', $this->translation));
     }]);
     $query->joinWith(['languageTranslateByLanguage' => function ($query) use($sourceLanguage) {
         $query->from(['ts' => LanguageTranslate::tableName()])->onCondition(['ts.language' => $sourceLanguage]);
     }]);
     return $dataProvider;
 }
コード例 #2
0
 /**
  * @return Array[] Generate a two dimensional array of the translation data for the exportLanguages:
  * 
  * ~~~
  * [
  *  'languages' => [],
  *  'languageSources' => [],
  *  'languageTranslations' => [],
  * ]
  * ~~~
  * 
  */
 public function getExportData()
 {
     $languages = Language::findAll($this->exportLanguages);
     $languageSources = LanguageSource::find()->all();
     $languageTranslations = LanguageTranslate::findAll(['language' => $this->exportLanguages]);
     $data = ['languages' => $languages, 'languageSources' => $languageSources, 'languageTranslations' => $languageTranslations];
     return $data;
 }
コード例 #3
0
 /**
  * Returnes language object by id and language_id. If not found, creates a new one.
  * @param integer $id LanguageSource id
  * @param string $languageId Language language_id
  * @return LanguageTranslate
  * @deprecated since version 1.2.7
  */
 public static function getLanguageTranslateByIdAndLanguageId($id, $languageId)
 {
     $languageTranslate = LanguageTranslate::findOne(['id' => $id, 'language' => $languageId]);
     if (!$languageTranslate) {
         $languageTranslate = new LanguageTranslate(['id' => $id, 'language' => $languageId]);
     }
     return $languageTranslate;
 }
コード例 #4
0
 /**
  * Returning messages in the given language
  * @return string
  */
 public function run()
 {
     $languageTranslate = LanguageTranslate::findOne(['id' => Yii::$app->request->get('id', 0), 'language' => Yii::$app->request->get('language_id', '')]);
     if ($languageTranslate) {
         $translation = $languageTranslate->translation;
     } else {
         $languageSource = LanguageSource::findOne(['id' => Yii::$app->request->get('id', 0)]);
         $translation = $languageSource ? $languageSource->message : '';
     }
     return $translation;
 }
コード例 #5
0
 /**
  * Saving translated language elements.
  * @return Json
  */
 public function run()
 {
     Yii::$app->response->format = Response::FORMAT_JSON;
     $id = Yii::$app->request->post('id', 0);
     $languageId = Yii::$app->request->post('language_id', Yii::$app->language);
     $languageTranslate = LanguageTranslate::findOne(['id' => $id, 'language' => $languageId]) ?: new LanguageTranslate(['id' => $id, 'language' => $languageId]);
     $languageTranslate->translation = Yii::$app->request->post('translation', '');
     if ($languageTranslate->validate() && $languageTranslate->save()) {
         $generator = new Generator($this->controller->module, $languageId);
         $generator->generate();
     }
     return $languageTranslate->getErrors();
 }
コード例 #6
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getLanguageSources()
 {
     return $this->hasMany(LanguageSource::className(), ['id' => 'id'])->viaTable(LanguageTranslate::tableName(), ['language' => 'language_id']);
 }
コード例 #7
0
 /**
  * Import the uploaded file. Existing languages and translations will be updated, new ones will be created.
  * Source messages won't be updated, only created if they not exist.
  * @return array
  * @throws BadRequestHttpException
  * @throws Exception
  */
 public function import()
 {
     $result = ['languages' => ['new' => 0, 'updated' => 0], 'languageSources' => ['new' => 0, 'updated' => 0], 'languageTranslations' => ['new' => 0, 'updated' => 0]];
     $data = $this->parseImportFile();
     /** @var Language[] $languages */
     $languages = Language::find()->indexBy('language_id')->all();
     foreach ($data['languages'] as $importedLanguage) {
         if (isset($languages[$importedLanguage['language_id']])) {
             $language = $languages[$importedLanguage['language_id']];
         } else {
             $language = new Language();
         }
         //cast integers
         $importedLanguage['status'] = (int) $importedLanguage['status'];
         $language->attributes = $importedLanguage;
         if (count($language->getDirtyAttributes())) {
             $saveType = $language->isNewRecord ? 'new' : 'updated';
             if ($language->save()) {
                 $result['languages'][$saveType]++;
             } else {
                 $this->throwInvalidModelException($language);
             }
         }
     }
     /** @var LanguageSource[] $languageSources */
     $languageSources = LanguageSource::find()->indexBy('id')->all();
     /** @var LanguageTranslate[] $languageTranslations */
     $languageTranslations = LanguageTranslate::find()->all();
     /*
      *  Create 2 dimensional array for current and imported translation, first index by LanguageSource->id
      *  and than indexed by LanguageTranslate->language.
      *  E.g.: [
      *      id => [
      *          language => LanguageTranslate (for $languageTranslations) / Array (for $importedLanguageTranslations)
      *          ...
      *      ]
      *      ...
      * ]
      */
     $languageTranslations = ArrayHelper::map($languageTranslations, 'language', function ($languageTranslation) {
         return $languageTranslation;
     }, 'id');
     $importedLanguageTranslations = ArrayHelper::map($data['languageTranslations'], 'language', function ($languageTranslation) {
         return $languageTranslation;
     }, 'id');
     foreach ($data['languageSources'] as $importedLanguageSource) {
         $languageSource = null;
         //check if id exist and if category and messages are matching
         if (isset($languageSources[$importedLanguageSource['id']]) && $languageSources[$importedLanguageSource['id']]->category == $importedLanguageSource['category'] && $languageSources[$importedLanguageSource['id']]->message == $importedLanguageSource['message']) {
             $languageSource = $languageSources[$importedLanguageSource['id']];
         }
         if (is_null($languageSource)) {
             //no match by id, search by message
             foreach ($languageSources as $languageSourceSearch) {
                 if ($languageSourceSearch->category == $importedLanguageSource['category'] && $languageSourceSearch->message == $importedLanguageSource['message']) {
                     $languageSource = $languageSourceSearch;
                     break;
                 }
             }
         }
         if (is_null($languageSource)) {
             //still no match, create new
             $languageSource = new LanguageSource(['category' => $importedLanguageSource['category'], 'message' => $importedLanguageSource['message']]);
             if ($languageSource->save()) {
                 $result['languageSources']['new']++;
             } else {
                 $this->throwInvalidModelException($languageSource);
             }
         }
         //do we have translations for the current source?
         if (isset($importedLanguageTranslations[$importedLanguageSource['id']])) {
             //loop through the translations for the current source
             foreach ($importedLanguageTranslations[$importedLanguageSource['id']] as $importedLanguageTranslation) {
                 $languageTranslate = null;
                 //is there already a translation for this souce
                 if (isset($languageTranslations[$languageSource->id]) && isset($languageTranslations[$languageSource->id][$importedLanguageTranslation['language']])) {
                     $languageTranslate = $languageTranslations[$languageSource->id][$importedLanguageTranslation['language']];
                 }
                 //no translation found, create a new one
                 if (is_null($languageTranslate)) {
                     $languageTranslate = new LanguageTranslate();
                 }
                 $languageTranslate->attributes = $importedLanguageTranslation;
                 //overwrite the id because the $languageSource->id might be different from the $importedLanguageTranslation['id']
                 $languageTranslate->id = $languageSource->id;
                 if (count($languageTranslate->getDirtyAttributes())) {
                     $saveType = $languageTranslate->isNewRecord ? 'new' : 'updated';
                     if ($languageTranslate->save()) {
                         $result['languageTranslations'][$saveType]++;
                     } else {
                         $this->throwInvalidModelException($languageTranslate);
                     }
                 }
             }
         }
     }
     return $result;
 }