Created
February 15, 2022 15:13
-
-
Save zuhairkareem/946045796c6e22d6e2a5b5e8a03cebbf to your computer and use it in GitHub Desktop.
Create translated strings manually in Drupal 8 config interface translation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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