帮助程序功能以添加自定义翻译:
/**
* Helper to manually add a single translation string.
*
* @param string $source_string
* Source string.
* @param string $langcode
* The langcode.
* @param string $translated_string
* Translated string.
*/
function MYMODULE_add_translation($source_string, $langcode, $translated_string) {
// Find existing source string.
$storage = \Drupal::service('locale.storage');
$string = $storage->findString(['source' => $source_string]);
if (is_null($string)) {
$string = new SourceString();
$string->setString($source_string);
$string->setStorage($storage);
$string->save();
}
// Create translation. If one already exists, it will be replaced.
$translation = $storage->createTranslation([
'lid' => $string->lid,
'language' => $langcode,
'translation' => $translated_string,
]);
$translation->save();
}
在更新挂钩中的用法:
/**
* Add translations.
*/
function MYMODULE_update_8002() {
MYMODULE_add_translation('Adjust', 'de', 'Anpassen');
}