Project

General

Profile

Actions

Bug #6327

open

tr function in WTemplate requires browser refresh after locale change

Added by Winfried Dobbe over 6 years ago. Updated 10 days ago.

Status:
New
Priority:
Normal
Assignee:
-
Target version:
-
Start date:
04/05/2018
Due date:
% Done:

0%

Estimated time:

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.

Actions #1

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.

Actions #2

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.

Actions #3

Updated by Winfried Dobbe 10 days ago

I run into the same problem again in a different Wt application. I tried your suggestion to load the template text also from the localized message resource bundle but that didn't help.

Could you show some code how to have the template and its bind widgets automatically refreshed after a call to Wt::WApplication::setLocale(...) ?
I am using Wt 4.10.4 at the moment.

Thanks!

Actions

Also available in: Atom PDF