Specify language when translating templates? #321
-
My goal is to compile templates in multiple languages for reuse in my program. According to documentation, I can just specify a "language" string $lang like so $latte->addExtension(new Latte\Essential\TranslatorExtension($translator, $lang)); However it's unclear how $lang is used within the compilation pipeline. Surely I shouldn't add the same extension to a Latte Engine instance multiple times since the last added one overrides the previous. // NOT GOOD
$latte->addExtension(new Latte\Essential\TranslatorExtension($translator1, 'en'));
$latte->addExtension(new Latte\Essential\TranslatorExtension($translator2, 'ja'));
$latte->renderToString('template.latte', $data); // this will only use $translator2 I can create multiple instances of Latte Engine to achieve my goal, but then the $lang parameter feels redundant. However if this is indeed recommended usage, then what's the purpose of $lang? To help Latte differentiate the compiled templates? // Intended & recommended usage??
$latte1->addExtension(new Latte\Essential\TranslatorExtension($translator1, 'en'));
$latte2->addExtension(new Latte\Essential\TranslatorExtension($translator2, 'ja'));
$latte1->renderToString('template.latte', $data);
$latte2->renderToString('template.latte', $data); Suggestions are welcome! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 8 replies
-
What do you want to achieve? Precompile all templates to warm up cache? |
Beta Was this translation helpful? Give feedback.
-
Without $lang parameter there is only one cached compiled version of template that translates strings every time it is rendered. If you use $lang parameter then cached compiled template contains translated strings for given language (translated at compile time) so there is no need to translate them every time during rendering. And yes there would be cached compiled template for every language with language specific strings already translated. |
Beta Was this translation helpful? Give feedback.
-
No you should have just one Translator and one TranslatorExtension. Selected language is part of internal state of Translator. Different Translator implementations use different methods to set/detect current language. Single Translator can translate to all language versions depending on selected language. |
Beta Was this translation helpful? Give feedback.
Without $lang parameter there is only one cached compiled version of template that translates strings every time it is rendered. If you use $lang parameter then cached compiled template contains translated strings for given language (translated at compile time) so there is no need to translate them every time during rendering. And yes there would be cached compiled template for every language with language specific strings already translated.