As twig does render templates in the UI language instead of the content language, all the t() functions deliver the default language. In my case this is wrong as the ui language is german but the content language can vary.

Can I tell the t() function to deliver the translation for Spanish for example?


1 Answer 1


You can choose the target language, see more information in the API.

In code it would look like this.

$variables['string_in_spanish'] = t(
  'Hello World',
  [], // (The args in the string)
  ['langcode' => 'es'] //  The options

note that when using the t() function, the source language has to be English.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.