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