Project

General

Profile

Bug #6327

tr function in WTemplate requires browser refresh after locale change

Added by Winfried Dobbe over 4 years ago. Updated over 4 years 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.

#1

Updated by Roel Standaert over 4 years ago

Hm. I see that it will refresh the template if the template text is a localized string itself.

#2

Updated by Roel Standaert over 4 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.

Also available in: Atom PDF