/**
  * Gets a translation string.
  *
  * @param string $source_value
  *   The source string value.
  * @param string $langcode
  *   The language code of the translation.
  * @param bool $create_fallback
  *   (optional) By default if a source string could be found and no
  *   translation in the given language exists yet, a translation object is
  *   created. This can be circumvented by passing FALSE.
  *
  * @return \Drupal\locale\TranslationString|null
  *   The translation string if one was found or created.
  */
 protected function getTranslation($source_value, $langcode, $create_fallback = TRUE)
 {
     // There is no point in creating a translation without a source.
     if ($source_string = $this->stringStorage->findString(['source' => $source_value])) {
         // Get the translation for this original source string from locale.
         $conditions = ['lid' => $source_string->lid, 'language' => $langcode];
         $translations = $this->stringStorage->getTranslations($conditions + ['translated' => TRUE]);
         if ($translations) {
             return reset($translations);
         } elseif ($create_fallback) {
             return $this->stringStorage->createTranslation($conditions);
         }
     }
 }
예제 #2
0
 /**
  * Ensures a translation exists and is marked as customized.
  *
  * @param string $config_name
  *   The configuration name.
  * @param string $translation
  *   The translation.
  * @param string $langcode
  *   The language code.
  * @param bool $customized
  *   Whether or not the string should be asserted to be customized or not
  *   customized.
  *
  * @return bool
  *   TRUE if the assertion succeeded, FALSE otherwise.
  */
 protected function assertTranslation($config_name, $translation, $langcode, $customized = TRUE)
 {
     // Make sure a string exists.
     $strings = $this->stringStorage->getTranslations(['type' => 'configuration', 'name' => $config_name, 'language' => $langcode, 'translated' => TRUE]);
     $pass = $this->assertIdentical(1, count($strings));
     $string = reset($strings);
     if ($this->assertTrue($string instanceof StringInterface)) {
         /** @var \Drupal\locale\StringInterface $string */
         $pass = $pass && $this->assertIdentical($translation, $string->getString());
         $pass = $pass && $this->assertTrue($string->isTranslation());
         if ($this->assertTrue($string instanceof TranslationString)) {
             /** @var \Drupal\locale\TranslationString $string */
             // Make sure the string is marked as customized so that it does not get
             // overridden when the string translations are updated.
             return $pass && $this->assertEqual($customized, $string->customized);
         }
     }
     return FALSE;
 }
 /**
  * Translates string using the localization system.
  *
  * So far we only know how to translate strings from English so the source
  * string should be in English.
  * Unlike regular t() translations, strings will be added to the source
  * tables only if this is marked as default data.
  *
  * @param string $name
  *   Name of the configuration location.
  * @param string $langcode
  *   Language code to translate to.
  * @param string $source
  *   The source string, should be English.
  * @param string $context
  *   The string context.
  *
  * @return string|false
  *   Translated string if there is a translation, FALSE if not.
  */
 public function translateString($name, $langcode, $source, $context)
 {
     if ($source) {
         // If translations for a language have not been loaded yet.
         if (!isset($this->translations[$name][$langcode])) {
             // Preload all translations for this configuration name and language.
             $this->translations[$name][$langcode] = array();
             foreach ($this->localeStorage->getTranslations(array('language' => $langcode, 'type' => 'configuration', 'name' => $name)) as $string) {
                 $this->translations[$name][$langcode][$string->context][$string->source] = $string;
             }
         }
         if (!isset($this->translations[$name][$langcode][$context][$source])) {
             // There is no translation of the source string in this config location
             // to this language for this context.
             if ($translation = $this->localeStorage->findTranslation(array('source' => $source, 'context' => $context, 'language' => $langcode))) {
                 // Look for a translation of the string. It might have one, but not
                 // be saved in this configuration location yet.
                 // If the string has a translation for this context to this language,
                 // save it in the configuration location so it can be looked up faster
                 // next time.
                 $this->localeStorage->createString((array) $translation)->addLocation('configuration', $name)->save();
             } else {
                 // No translation was found. Add the source to the configuration
                 // location so it can be translated, and the string is faster to look
                 // for next time.
                 $translation = $this->localeStorage->createString(array('source' => $source, 'context' => $context))->addLocation('configuration', $name)->save();
             }
             // Add an entry, either the translation found, or a blank string object
             // to track the source string, to this configuration location, language,
             // and context.
             $this->translations[$name][$langcode][$context][$source] = $translation;
         }
         // Return the string only when the string object had a translation.
         if ($this->translations[$name][$langcode][$context][$source]->isTranslation()) {
             return $this->translations[$name][$langcode][$context][$source]->getString();
         }
     }
     return FALSE;
 }