i18n problem

Added by Аз Есмь 10 months ago

hello,

every time WAnchor is clicked, locale() resets, so a visitor has to restore the language again and again.

here's how my application handles languages:

WtApplication::WtApplication(const Wt::WEnvironment& env) : Wt::WApplication(env) {
...
if (locale() != "en" && locale() != "ru") setLocale("ru");
...
Wt::WText *languageSwitch = new Wt::WText(Wt::WString::tr("language"));
languageSwitch->clicked().connect(this, &WtApplication::switchLanguage);
languageSwitch->setStyleClass("language-widget");
header->addWidget(languageSwitch);
...
}
void WtApplication::switchLanguage() {
locale() == "ru" ? setLocale("en") : setLocale("ru");
return;
}

there's nothing else that changes locale(). however as soon as a visitor follows any hyperlink [either WAnchor("?_=about"), or <a href="?_=about">] on the site, the locale resets to default.

is that a bug?


Replies (6)

RE: i18n problem - Added by Wim Dumon 10 months ago

That's because following your anchors causes a new session to be started. I'm a bit confused that that happens when you're using a WAnchor, though.

On our homepage, we encode the locale in the URL.

BR,
Wim.

RE: i18n problem - Added by Аз Есмь 10 months ago

thank you, Wim, for the explanation. i wonder if starting a new session because of anchors is a common practiсe in the industry.

okay, i'll resort to the workaround you mentioned.

RE: i18n problem - Added by Wim Dumon 10 months ago

Hello,

See http://www.webtoolkit.eu/wt/doc/reference/html/overview.html#urls_sec
In Wt you can avoid this with WAnchor and the internal path API:

WAnchor *a = new WAnchor(WLink(WLink::internalPath, "/about"));

When the user clicks that anchor, the active session will receive a WApplication::internalPathChanged() event and no new session will be started.

BR,
Wim.

RE: i18n problem - Added by Аз Есмь 10 months ago

sounds like it only works for WAnchor(WLink(WLink::internalPath, "/about")), but not for <a href="/about">. or am i wrong?

RE: i18n problem - Added by Koen Deforche 10 months ago

Hey,

Yes but you can also specify internal paths in HTML in WTemplate and WText, see the docs for setInternalPathEncoding.

Regards,
Koen

RE: i18n problem - Added by Аз Есмь 10 months ago

yes, Koen, this has finally solved my problem. thank you very much. i only wish there would be a global setInternalPathEncoding(), so that one doesn't have to invoke it for all widgets with HTML anchors or specify WLink::internalPath for all WAnchors which point to internal paths. i suggest that this should be a default behavior, to begin with.

(1-6/6)