コード例 #1
0
 /**
  * Sets up a configuration string with a translation.
  *
  * The actual configuration is already available by installing locale_test
  * module, as it is done in LocaleConfigSubscriberTest::setUp(). This sets up
  * the necessary source and translation strings and verifies that everything
  * is as expected to avoid false positives.
  *
  * @param string $config_name
  *   The configuration name.
  * @param string $key
  *   The configuration key.
  * @param string $source
  *   The source string.
  * @param string $translation
  *   The translation string.
  */
 protected function setUpTranslation($config_name, $key, $source, $translation)
 {
     // Create source and translation strings for the configuration value and add
     // the configuration name as a location. This would be performed by
     // locale_translate_batch_import() and locale_config_update_multiple()
     // normally.
     $source_object = $this->stringStorage->createString(['source' => $source, 'context' => ''])->save();
     $this->stringStorage->createTranslation(['lid' => $source_object->getId(), 'language' => $this->langcode, 'translation' => $translation])->save();
     $this->localeConfigManager->translateString($config_name, $this->langcode, $source, '');
     $this->languageManager->setConfigOverrideLanguage(ConfigurableLanguage::load($this->langcode));
     $this->assertConfigValue($config_name, $key, $translation);
     $this->assertTranslation($config_name, $translation, FALSE);
 }
コード例 #2
0
 /**
  * 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);
         }
     }
 }
コード例 #3
0
 /**
  * Get the translation object for the given source/context and language.
  *
  * @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 \Drupal\locale\TranslationString|FALSE
  *   The translation object if the string was not empty or FALSE otherwise.
  */
 public function getStringTranslation($name, $langcode, $source, $context)
 {
     if ($source) {
         $this->translateString($name, $langcode, $source, $context);
         if ($string = $this->translations[$name][$langcode][$context][$source]) {
             if (!$string->isTranslation()) {
                 $conditions = array('lid' => $string->lid, 'language' => $langcode);
                 $translation = $this->localeStorage->createTranslation($conditions);
                 $this->translations[$name][$langcode][$context][$source] = $translation;
                 return $translation;
             } else {
                 return $string;
             }
         }
     }
     return FALSE;
 }