/** * 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); }
/** * 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; }