/**
  * Sets configuration based on a nested form value array.
  *
  * @param \Drupal\Core\Language\LanguageInterface $language
  *   Set the configuration in this language.
  * @param \Drupal\Core\Config\Config $base_config
  *   Base configuration values, in the source language.
  * @param \Drupal\language\Config\LanguageConfigOverride $config_translation
  *   Translation configuration override data.
  * @param array $config_values
  *   A simple one dimensional or recursive array:
  *     - simple:
  *        array(name => array('translation' => 'French site name'));
  *     - recursive:
  *        cancel_confirm => array(
  *          cancel_confirm.subject => array('translation' => 'Subject'),
  *          cancel_confirm.body => array('translation' => 'Body content'),
  *        );
  *   Either format is used, the nested arrays are just containers and not
  *   needed for saving the data.
  * @param bool $shipped_config
  *   (optional) Flag to specify whether the configuration had a shipped
  *   version and therefore should also be stored in the locale database.
  *
  * @return array
  *   Translation configuration override data.
  */
 protected function setConfig(LanguageInterface $language, Config $base_config, LanguageConfigOverride $config_translation, array $config_values, $shipped_config = FALSE)
 {
     foreach ($config_values as $key => $value) {
         if (is_array($value) && !isset($value['translation'])) {
             // Traverse into this level in the configuration.
             $this->setConfig($language, $base_config, $config_translation, $value, $shipped_config);
         } else {
             // If the configuration file being translated was originally shipped, we
             // should update the locale translation storage. The string should
             // already be there, but we make sure to check.
             if ($shipped_config && ($source_string = $this->localeStorage->findString(array('source' => $base_config->get($key))))) {
                 // Get the translation for this original source string from locale.
                 $conditions = array('lid' => $source_string->lid, 'language' => $language->id);
                 $translations = $this->localeStorage->getTranslations($conditions + array('translated' => TRUE));
                 // If we got a translation, take that, otherwise create a new one.
                 $translation = reset($translations) ?: $this->localeStorage->createTranslation($conditions);
                 // If we have a new translation or different from what is stored in
                 // locale before, save this as an updated customize translation.
                 if ($translation->isNew() || $translation->getString() != $value['translation']) {
                     $translation->setString($value['translation'])->setCustomized()->save();
                 }
             }
             // Save value, if different from the source value in the base
             // configuration. If same as original configuration, remove override.
             if ($base_config->get($key) !== $value['translation']) {
                 $config_translation->set($key, $value['translation']);
             } else {
                 $config_translation->clear($key);
             }
         }
     }
 }
Example #2
0
 /**
  * Creates single translation for source string.
  */
 protected function createTranslation($source, $translation, $langcode)
 {
     $values = array('lid' => $source->lid, 'language' => $langcode, 'translation' => $translation);
     return $this->localeStorage->createTranslation($values)->save();
 }
Example #3
0
 /**
  * Creates single translation for source string.
  */
 public function createTranslation($source, $langcode, $values = array())
 {
     return $this->storage->createTranslation($values + array('lid' => $source->lid, 'language' => $langcode, 'translation' => $this->randomName(100)))->save();
 }
Example #4
0
 /**
  * Creates single translation for source string.
  */
 public function createTranslation($source, $langcode)
 {
     return $this->localeStorage->createTranslation(array('lid' => $source->lid, 'language' => $langcode, 'translation' => $this->randomMachineName(100)))->save();
 }