diff --git a/core/modules/locale/lib/Drupal/locale/LocaleConfigManager.php b/core/modules/locale/lib/Drupal/locale/LocaleConfigManager.php index 2f15f75..82c3c8f 100644 --- a/core/modules/locale/lib/Drupal/locale/LocaleConfigManager.php +++ b/core/modules/locale/lib/Drupal/locale/LocaleConfigManager.php @@ -66,56 +66,52 @@ public function __construct(LocaleConfigStorage $configStorage, TypedConfigManag * @param string $name * Configuration object name. * - * @return \Drupal\locale\LocaleTypedConfig|FALSE - * Locale-wrapped configuration element or false if there is no translatable data. + * @return \Drupal\locale\LocaleTypedConfig|NULL + * Locale-wrapped configuration element or null if there is no translatable data. */ public function getLocaleWrapper($name) { // We get only the data that didn't change from default. $data = $this->configStorage->getUnchanged($name); if ($data && $this->canTranslateData($data)) { - // Unless the configuration has a explicit language code we assume English. - $langcode = isset($data['langcode']) ? $data['langcode'] : 'en'; $definition = $this->typedConfig->getDefinition($name); - $wrapper = new LocaleTypedConfig($definition, $name, $langcode, $this); - $wrapper->setValue($data); + $element = $this->typedConfig->create($definition, $data, $name); + $wrapper = new LocaleTypedConfig($element, $this->getTranslator($name)); return $wrapper; } else { - return FALSE; + return NULL; } } /** - * Create locale wrapper with typed configuration data. + * Gets translated configuration data. * * @param string $name * Configuration object name. - * @param array $data - * Array of configuration data. + * @param string $langcode + * Language code. * - * @return \Drupal\locale\LocaleTypedConfig|FALSE - * Locale-wrapped configuration element or false if there is no translatable data. + * @return */ - public function createTypedConfig($name, array $data) { - $definition = $this->typedConfig->getDefinition($name); - $element = $this->typedConfig->create($definition, $data, $name); - $element->setValue($data); - return $element; + public function getTranslationData($name, $langcode) { + return $this->configStorage->readTranslation($name, $langcode); } - /** - * Gets translated configuration data. + * Gets string translator for configuration. * * @param string $name * Configuration object name. - * @param string $langcode - * Language code. * - * @return + * @return Drupal\Core\StringTranslation\Translator\TranslatorInterface + * Translator for this configuration name. */ - public function getTranslationData($name, $langcode) { - return $this->configStorage->readTranslation($name, $langcode); + public function getTranslator($name) { + // If translator for a configuration name is not created yet. + if (!isset($this->translations[$name])) { + $this->translations[$name] = new LocaleConfigTranslation($name, $this->localeStorage); + } + return $this->translations[$name]; } /** @@ -180,37 +176,6 @@ public function deleteLanguageTranslations($langcode) { } /** - * 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 translator for a configuration name is not created yet. - if (!isset($this->translations[$name])) { - $this->translations[$name] = new LocaleConfigTranslation($name, $this->localeStorage); - } - return $this->translations[$name]->getStringTranslation($langcode, $source, $context); - } - return FALSE; - } - - /** * Translate configuration object. * * @param Drupal\Core\Config\Config $config @@ -263,7 +228,7 @@ public function updateConfigTranslation($name, $langcodes = array()) { $wrapper = $this->getLocaleWrapper($name); $count = 0; foreach ($langcodes as $langcode) { - $translation = $wrapper ? $wrapper->getTranslation($langcode)->getValue() : NULL; + $translation = $wrapper ? $wrapper->getTranslation($langcode) : NULL; if ($translation) { $this->configStorage->writeTranslation($name, $langcode, $translation); $count++; diff --git a/core/modules/locale/lib/Drupal/locale/LocaleTypedConfig.php b/core/modules/locale/lib/Drupal/locale/LocaleTypedConfig.php index 28b66c3..a4d1692 100644 --- a/core/modules/locale/lib/Drupal/locale/LocaleTypedConfig.php +++ b/core/modules/locale/lib/Drupal/locale/LocaleTypedConfig.php @@ -11,6 +11,7 @@ use Drupal\Core\TypedData\ContextAwareInterface; use Drupal\Core\Config\Schema\Element; use Drupal\Core\Config\Schema\ArrayElement; +use Drupal\Core\StringTranslation\Translator\TranslatorInterface; /** * Defines the locale configuration wrapper object. @@ -18,18 +19,18 @@ class LocaleTypedConfig extends Element { /** - * The language code for which this is a translation. + * The wrapped typed configuration element. * - * @var string + * @var Drupal\Core\Config\Schema\Element */ - protected $langcode; + protected $element; /** - * The locale configuration manager object. + * The translator to use for strings. * * @var \Drupal\locale\LocaleConfigManager */ - protected $localeConfig; + protected $translator; /** * Constructs a configuration wrapper object. @@ -38,68 +39,58 @@ class LocaleTypedConfig extends Element { * The data definition. * @param string $name * The configuration object name. - * @param string $langcode - * Language code for the source configuration data. - * @param \Drupal\locale\LocaleConfigManager $localeConfig; - * The locale configuration manager object. + * @param Drupal\Core\Config\Schema\Element $element + * Typed configuration element. + * @param Drupal\Core\StringTranslation\Translator\TranslatorInterface $translator; + * Translator to use for strings. */ - public function __construct(array $definition, $name, $langcode, LocaleConfigManager $localeConfig) { - parent::__construct($definition, $name); - $this->langcode = $langcode; - $this->localeConfig = $localeConfig; + public function __construct($element, TranslatorInterface $translator) { + parent::__construct($element->getDefinition(), $element->getName()); + $this->setValue($element->getValue()); + $this->element = $element; + $this->translator = $translator; } /** * Gets wrapped typed config object. */ public function getTypedConfig() { - return $this->localeConfig->createTypedConfig($this->name, $this->value); + // Set the wrapped element with the latest value. + $this->element->setValue($this->getValue()); + return $this->element; } /** - * {@inheritdoc} + * Get translated configuration data. + * + * @param string $langcode + * Language code to translate to. + * @return array + * Translated configuration data. */ public function getTranslation($langcode) { - if ($this->localeConfig->canTranslateLangcode($this->langcode, $langcode)) { - $options = array( - 'source' => $this->langcode, - 'target' => $langcode, - ); - $data = $this->getElementTranslation($this->getTypedConfig(), $options); - return $this->localeConfig->createTypedConfig($this->name, $data); - } - else { - return NULL; - } - } - - /** - * {@inheritdoc} - */ - public function language() { - return language_load($this->langcode); + return $this->getElementTranslation($this->getTypedConfig(), $langcode); } /** * Gets translated configuration data for a typed configuration element. * - * @param mixed $element + * @param mixed $source_element * Typed configuration element, either \Drupal\Core\Config\Schema\Element or * \Drupal\Core\Config\Schema\ArrayElement. - * @param array $options - * Array with translation options that must contain the keys defined in - * \Drupal\locale\LocaleTypedConfig::translateElement() + * @param string $langcode + * Language code to translate to. * * @return array * Configuration data translated to the requested language if available, * an empty array otherwise. */ - protected function getElementTranslation($element, array $options) { + protected function getElementTranslation($element, $langcode) { $translation = array(); if ($element instanceof ArrayElement) { - $translation = $this->getArrayTranslation($element, $options); + $translation = $this->getArrayTranslation($element, $langcode); } - elseif ($this->translateElement($element, $options)) { + elseif ($this->translateElement($element, $langcode)) { $translation = $element->getValue(); } return $translation; @@ -110,17 +101,16 @@ protected function getElementTranslation($element, array $options) { * * @param \Drupal\Core\Config\Schema\ArrayElement $element * Typed configuration array element. - * @param array $options - * Array with translation options that must contain the keys defined in - * \Drupal\locale\LocaleTypedConfig::translateElement() + * @param string $langcode + * Language code to translate to. * * @return array * Configuration data translated to the requested language. */ - protected function getArrayTranslation(ArrayElement $element, array $options) { + protected function getArrayTranslation(ArrayElement $element, $langcode) { $translation = array(); foreach ($element as $key => $property) { - $value = $this->getElementTranslation($property, $options); + $value = $this->getElementTranslation($property, $langcode); if (!empty($value)) { $translation[$key] = $value; } @@ -140,20 +130,18 @@ protected function getArrayTranslation(ArrayElement $element, array $options) { * * @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. + * @param string $langcode + * Language code to translate to. * * @return bool * Whether the element fits the translation criteria. */ - protected function translateElement(\Drupal\Core\TypedData\TypedDataInterface $element, array $options) { + protected function translateElement(\Drupal\Core\TypedData\TypedDataInterface $element, $langcode) { $definition = $element->getDefinition(); $value = $element->getValue(); if ($value && !empty($definition['translatable'])) { $context = isset($definition['locale context']) ? $definition['locale context'] : ''; - if ($translation = $this->localeConfig->translateString($this->name, $options['target'], $value, $context)) { + if ($translation = $this->translator->getStringTranslation($langcode, $value, $context)) { $element->setValue($translation); return TRUE; } diff --git a/core/modules/locale/lib/Drupal/locale/Tests/LocaleConfigTranslationTest.php b/core/modules/locale/lib/Drupal/locale/Tests/LocaleConfigTranslationTest.php index 940a964..18f3d97 100644 --- a/core/modules/locale/lib/Drupal/locale/Tests/LocaleConfigTranslationTest.php +++ b/core/modules/locale/lib/Drupal/locale/Tests/LocaleConfigTranslationTest.php @@ -85,9 +85,8 @@ function testConfigTranslation() { // Get translation and check we've only got the site name. $translation = $wrapper->getTranslation($langcode); - $properties = $translation->getProperties(); - $this->assertEqual(count($properties), 1, 'Got the right number of properties after translation'); -// $this->assertEqual($properties['name']->getValue(), $site_name, 'Got the right translation for site name after translation'); + $this->assertEqual(count($translation), 1, 'Got the right number of properties after translation'); + $this->assertEqual($translation['name'], $site_name, 'Got the right translation for site name after translation'); // Check the translated site name is displayed. $this->drupalGet($langcode); @@ -135,8 +134,7 @@ function testConfigTranslation() { $wrapper = $this->container->get('locale.config.manager')->getLocaleWrapper('image.style.medium'); $translation = $wrapper->getTranslation($langcode); - $property = $translation->get('label'); - $this->assertEqual($property->getValue(), $image_style_label, 'Got the right translation for image style name after translation'); + $this->assertEqual($translation['label'], $image_style_label, 'Got the right translation for image style name after translation'); // Quick test to ensure translation file exists. $this->assertEqual(config('locale.config.xx.image.style.medium')->get('label'), $image_style_label); diff --git a/core/modules/locale/lib/Drupal/locale/Tests/LocaleImportFunctionalTest.php b/core/modules/locale/lib/Drupal/locale/Tests/LocaleImportFunctionalTest.php index 2c1203f..4c7af04 100644 --- a/core/modules/locale/lib/Drupal/locale/Tests/LocaleImportFunctionalTest.php +++ b/core/modules/locale/lib/Drupal/locale/Tests/LocaleImportFunctionalTest.php @@ -295,10 +295,9 @@ function testConfigPoFile() { // Translations got recorded in the config system. foreach ($config_strings as $config_key => $config_string) { $wrapper = $locale_config->getLocaleWrapper($config_key); - $translation = $wrapper->getTranslation($langcode); - $properties = $translation->getProperties(); + $properties = $wrapper->getTranslation($langcode); $this->assertEqual(count($properties), 1, 'Got the right number of properties with strict translation'); - $this->assertEqual($properties[$config_string[2]]->getValue(), $config_string[1]); + $this->assertEqual($properties[$config_string[2]], $config_string[1]); } }