Problem/Motivation
To translate interface text, configuration content, and entity content in a specific render context is difficult. To accomplish this, you must change the site's default language, set the config override, and modify the default langcode on the translation service.
+ $this->languageDefault->set($language);
+
+ $this->languageManager->setConfigOverrideLanguage($language);
+ $this->languageManager->reset();
+
+ $string_translation = $this->getStringTranslation();
+ if ($string_translation instanceof TranslationManager) {
+ $string_translation->setDefaultLangcode($language->getId());
+ $string_translation->reset();
Proposed resolution
The LanguageManagerInterface should have a "setLanguage" method (or other naming) which streamlines this task.
Remaining tasks
User interface changes
API changes
Data model changes
Release notes snippet
Comments
Comment #11
johnvNot sure what the use case is here.
To interface the User translation text, you can
- install locale (Interface translation) module
- goto admin/config/regional/translate
- set the target language, to be able to the new language.
Or, for the others,
switch from default language '/node/8/edit' to target language '/nl/node/8/edit'
having set up /admin/config/regional/language/detection properly.
But, 5 years have past.
Settings this to outdated.
Comment #12
flocondetoileI believe this issue is still relevant.
The use case is to change programmatically the language in order to translate something in another language. An example as a use case is how Drupal Commerce send Email Order Receipt, given the preferred language of a customer.
See https://git.drupalcode.org/project/commerce/-/blob/8.x-2.x/src/MailHandl...
Comment #13
flocondetoileComment #14
johnvThen this issue seems like the same (duplicate) feature request? #2350907: Create simple helper function to add a string translation programatically
Adding the word 'programmatically' already helps the reader.
Comment #15
johnvAnd this issue marks the problem as a bug: #3393852: TranslatableMarkup does not seem to use current but rather default language
Comment #16
sleitner commentedComment #17
sleitner commentedDuplicate of #2410579: Allow to change the current language