/**
  * Deletes translation data from locale module.
  *
  * This will invoke LocaleConfigSubscriber through the event dispatcher. To
  * make sure the configuration was persisted correctly, the configuration
  * value is checked. Because LocaleConfigSubscriber temporarily disables the
  * override state of the configuration factory we check that the correct value
  * is restored afterwards.
  *
  * @param string $config_name
  *   The configuration name.
  * @param string $key
  *   The configuration key.
  * @param string $source_value
  *   The source configuration value to verify the correct value is returned
  *   from the configuration factory after the deletion.
  * @param string $langcode
  *   The language code.
  */
 protected function deleteLocaleTranslationData($config_name, $key, $source_value, $langcode)
 {
     $this->localeConfigManager->getStringTranslation($config_name, $langcode, $source_value, '')->delete();
     $this->localeConfigManager->reset();
     $this->localeConfigManager->updateConfigTranslations(array($config_name), array($langcode));
     $this->configFactory->reset($config_name);
     $this->assertNoConfigOverride($config_name, $key, $source_value, $langcode);
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 public function hasTranslation(LanguageInterface $language)
 {
     foreach ($this->getConfigNames() as $name) {
         if ($this->localeConfigManager->hasTranslation($name, $language->getId())) {
             return TRUE;
         }
     }
     return FALSE;
 }
 /**
  * Updates the translation strings of shipped configuration.
  *
  * @param \Drupal\language\Config\LanguageConfigOverrideCrudEvent $event
  *   The language configuration event.
  * @param $callable
  *   A callable to apply to each translatable string of the configuration.
  */
 protected function updateTranslationStrings(LanguageConfigOverrideCrudEvent $event, $callable)
 {
     $translation_config = $event->getLanguageConfigOverride();
     $name = $translation_config->getName();
     // Only do anything if the configuration was shipped.
     if ($this->stringStorage->getLocations(['type' => 'configuration', 'name' => $name])) {
         $source_config = $this->configFactory->getEditable($name);
         $schema = $this->localeConfigManager->get($name)->getTypedConfig();
         $this->traverseSchema($schema, $source_config, $translation_config, $callable);
     }
 }
Example #4
0
 /**
  * Saves a translation string and marks it as customized.
  *
  * @param string $name
  *   The configuration name.
  * @param string $source
  *   The source string value.
  * @param string $context
  *   The source string context.
  * @param string $translation
  *   The translation string.
  * @param string $langcode
  *   The language code of the translation.
  */
 protected function saveCustomizedTranslation($name, $source, $context, $translation, $langcode)
 {
     $locale_translation = $this->localeConfigManager->getStringTranslation($name, $langcode, $source, $context);
     if (!empty($locale_translation)) {
         // Save this translation as custom if it was a new translation and not the
         // same as the source. (The interface prefills translation values with the
         // source). Or if there was an existing translation and the user changed
         // it (even if it was changed back to the original value). Otherwise the
         // translation file would be overwritten with the locale copy again later.
         if ($locale_translation->isNew() && $source != $translation || !$locale_translation->isNew() && $translation != $locale_translation->getString()) {
             $locale_translation->setString($translation)->setCustomized(TRUE)->save();
         }
     }
 }
 /**
  * Tests ConfigNamesMapper::hasTranslation().
  *
  * @param array $mock_return_values
  *   An array of values that the mocked configuration mapper manager should
  *   return for hasTranslation().
  * @param bool $expected
  *   The expected return value of ConfigNamesMapper::hasTranslation().
  *
  * @dataProvider providerTestHasTranslation
  */
 public function testHasTranslation(array $mock_return_values, $expected)
 {
     $language = new Language();
     // As the configuration names are arbitrary, simply use integers.
     $config_names = range(1, count($mock_return_values));
     $this->configNamesMapper->setConfigNames($config_names);
     $map = array();
     foreach ($config_names as $i => $config_name) {
         $map[] = array($config_name, $language->getId(), $mock_return_values[$i]);
     }
     $this->localeConfigManager->expects($this->any())->method('hasTranslation')->will($this->returnValueMap($map));
     $result = $this->configNamesMapper->hasTranslation($language);
     $this->assertSame($expected, $result);
 }
 /**
  * Translates element's value if it fits our translation criteria.
  *
  * For an element to be translatable by locale module it needs to be of base
  * type 'string' and have 'translatable = TRUE' in the element's definition.
  * Translatable elements may use these additional keys in their data
  * definition:
  * - 'translatable', FALSE to opt out of translation.
  * - 'translation context', to define the string context.
  *
  * @param \Drupal\Core\TypedData\TypedDataInterface $element
  *   Configuration element.
  * @param array $options
  *   Array with translation options that must contain the following keys:
  *   - 'source', Source language code.
  *   - 'target', Target language code.
  *
  * @return bool
  *   Whether the element fits the translation criteria.
  */
 protected function translateElement(TypedDataInterface $element, array $options)
 {
     if ($this->canTranslate($options['source'], $options['target'])) {
         $definition = $element->getDataDefinition();
         $value = $element->getValue();
         if ($value && !empty($definition['translatable'])) {
             $context = isset($definition['translation context']) ? $definition['translation context'] : '';
             if ($translation = $this->localeConfig->translateString($this->name, $options['target'], $value, $context)) {
                 $element->setValue($translation);
                 return TRUE;
             }
         }
     }
     // The element does not have a translation.
     return FALSE;
 }
 /**
  * Deletes translation data from locale module.
  *
  * This will invoke LocaleConfigSubscriber through the event dispatcher. To
  * make sure the configuration was persisted correctly, the configuration
  * value is checked. Because LocaleConfigSubscriber temporarily disables the
  * override state of the configuration factory we check that the correct value
  * is restored afterwards.
  *
  * @param string $config_name
  *   The configuration name.
  * @param string $key
  *   The configuration key.
  * @param string $source_value
  *   The source configuration value to verify the correct value is returned
  *   from the configuration factory after the deletion.
  */
 protected function deleteLocaleTranslationData($config_name, $key, $source_value)
 {
     $this->localeConfigManager->deleteTranslationData($config_name, $this->langcode);
     $this->configFactory->reset($config_name);
     $this->assertConfigValue($config_name, $key, $source_value);
 }