Bug #6327
opentr function in WTemplate requires browser refresh after locale change
0%
Description
I use the following simple template:
<hr/>
${title}
<hr/><br/>
${tr:preferences-select-lang-label} ${lang-select}
<hr/>
which is used as follows:
auto template_widget = std::make_unique<Wt::WTemplate>();
template_widget->addFunction("tr", &Wt::WTemplate::Functions::tr);
template_widget->setTemplateText(loadTemplate("language_select.tpl"));
(...)
auto langCombo = std::make_unique<Wt::WComboBox>();
(...)
langCombo->sactivated().connect(this, &LanguagePage::languageChanged);
template_widget->bindWidget("lang-select", std::move(langCombo));
addWidget(std::move(template_widget));
When the user selects another language with the combobox, the Wt application calls
Wt::WApplication::instance()->setLocale(Wt::WLocale(newLocale));
After this, the text of the preferences-select-lang-label item is not translated yet in the web browser.
Only after the user refreshes the web page, the correct translation is shown.
I tested this with both Firefox 52.4.0 and Chrome 58.0.3029.110 on Linux.
If there is no solution to update the translation without refresh, then it would be nice if this will be mentioned in the WTemplate documentation.
Updated by Roel Standaert over 6 years ago
Hm. I see that it will refresh the template if the template text is a localized string itself.
Updated by Roel Standaert over 6 years ago
Note: if you want to load your template text from a file with your loadTemplate
function instead of creating a template.xml
or similar file and using tr()
for your template text, then you could create a custom WLocalizedStrings
that resolves keys using files.