Support #6493
closedREST server - setting locale locale for messages
0%
Description
Hi,
I'm sure I must be missing something simple here. I have a rest server that serves mobile clients and I would like to return messages based on their language set in their mobile and I pass the language set in their mobile.
I have multiple message files, i.e. general.xml, general_es.xml, general_ca.xml.
My server startup is like:
WServer server(argv[0], "");
try {
server.setServerConfiguration(argc, argv);
auto bundle = std::make_shared<Wt::WMessageResourceBundle>();
bundle->use("/Users/xxx/general", true);
server.setLocalizedStrings(bundle);
cout << "welcome-text is " << WString::tr("no_match") << endl;
The english message is being returned correctly.
When I pass the preferred language through in the client request, e.g. es_ES I want the message_es.xml file to be used in the response.
If I understand it correctly, Wt httpd will get the locale from the request header but doesn't that mean that if the user is in an internet cafe in Dubai that it will use that locale (which is why I wanted to send the preferred language from the client app)?
If it was an app derived from WApplication I could call setLocale() but how do I do it for web clients in a WServer app?
Updated by Roel Standaert over 6 years ago
It uses the Accept-Language
header, which should be the language that the user's browser prefers, or if you have a client application, you can send an appropriate Accept-Language
header to select the language that you want to use.
You can also use WLocale::setCurrentLocale()
, which sets the current locale for just that request.
Updated by go mac over 6 years ago
HI,
Yes, WLocale::setCurrentLocale() worked. I thought I had tried that before but maybe with "es_ES" instead of "es".
Thank you for such a quick response, it's a fantastic product. I will try to close off the issue.
regards,
Gordon
Updated by Roel Standaert over 6 years ago
- Status changed from New to Closed
You're welcome, I'll close it.