Exemplo n.º 1
0
 /**
  * Fill LanguageStringTranslation objects from array.
  *
  * @param Language $language
  * @param array $values
  */
 protected function fillTranslationsFromForm(Language $language, array $values)
 {
     foreach (LanguageStringQuery::create()->find() as $string) {
         $field = sha1($string->getId());
         if ($values[$field] != '') {
             // ignore empty string as we want to keep fallback in that case
             // find existing translation
             $translation = LanguageStringTranslationQuery::create()->filterByLanguage($language)->filterByLanguageString($string)->findOne();
             // create new?
             if (!$translation) {
                 $translation = new LanguageStringTranslation();
                 $translation->setLanguage($language);
                 $translation->setLanguageString($string);
             }
             // set new translation and save
             $translation->setTranslation($values[$field]);
             $translation->save();
         }
     }
 }
Exemplo n.º 2
0
 /**
  * Get array with language strings for the specified language. This function caches the language strings in memory.
  *
  * @param Language|string|null $language Language, langcode (string) or null for active language.
  * @return array
  */
 private static function _getLanguage($language)
 {
     if ($language === null) {
         $language = self::$currentLanguage;
     } else {
         if (is_string($language)) {
             $language = LanguageQuery::create()->findPk($language);
         } else {
             if (!$language instanceof Language) {
                 throw new Exception('Invalid language');
             }
         }
     }
     if ($language) {
         // initialize language strings
         if (!array_key_exists($language->getLangcode(), self::$languageStrings)) {
             trace_notice('Loading translation-strings for ' . $language->getName() . ' (' . $language->getLangcode() . ')');
             self::$languageStrings[$language->getLangcode()] = LanguageStringTranslationQuery::create()->filterByLanguage($language)->find()->toKeyValue('StringId', 'Translation');
         }
     }
     return $language;
 }