Skip to content

Instantly share code, notes, and snippets.

@zuhairkareem
Created February 15, 2022 15:13
Show Gist options
  • Save zuhairkareem/946045796c6e22d6e2a5b5e8a03cebbf to your computer and use it in GitHub Desktop.
Save zuhairkareem/946045796c6e22d6e2a5b5e8a03cebbf to your computer and use it in GitHub Desktop.
Create translated strings manually in Drupal 8 config interface translation
<?php
use \Drupal\locale\SourceString;
$source_string = "Source text string";
$translated_string = "translated string";
$langcode = "ar";
// 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)
->setStorage($storage)
->save();
}
// Create translation. If one already exists, it will be replaced.
$translation = $storage->createTranslation([
'lid' => $string->getId(),
'language' => $langcode,
'translation' => $translated_string,
])->save();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment