Project

General

Profile

How to get user locale when WEnvironment is not available?

Added by Plug Gulp almost 4 years ago

Hello Wt Team,

How do I get access to user's browser locale if I am not implementing WApplication? I am directly using WServer to start my web application. I do not see a way to get the locale information. I am registering the root path("/"), and all other paths, as WResource. I am basically trying to implement RESTful API.

Thanks and kind regards,

~Plug


Replies (3)

RE: How to get user locale when WEnvironment is not available? - Added by Roel Standaert almost 4 years ago

Are you looking for language information or timezone information?

The resource will derive locale information from the Accept-Language header sent by the browser. In the body of handleRequest(), WLocale::currentLocale() will be set to this, so you don't need to do anything extra except ensure that the Accept-Language header is sent.

Timezone information, like WEnvironment::timeZoneOffset() and WEnvironment::timeZoneName() are determined using JavaScript code, and sent as a parameter. If you want this to work for resources, you will have to write JavaScript that sends this information as a parameter.

Here's a snippet of the JavaScript used to get the timezone information:

// determine time zone offset
var tzOffset = (new Date()).getTimezoneOffset();
otherInfo += "&tz=" + (-tzOffset);

// determine time zone name, if available
if (typeof Intl === 'object' &&
    typeof Intl.DateTimeFormat === "function" &&
    typeof Intl.DateTimeFormat().resolvedOptions === "function" &&
    Intl.DateTimeFormat().resolvedOptions().timeZone) {
  otherInfo += "&tzS=" + encodeURIComponent(Intl.DateTimeFormat().resolvedOptions().timeZone);
}

RE: How to get user locale when WEnvironment is not available? - Added by Plug Gulp almost 4 years ago

Are you looking for language information or timezone information?

language information.

I am trying to trim leading and trailing spaces of request parameter string. For that I require locale information to pass to std::isspace function. Following is the code for trimming function I am using:

<code class="cpp">
        auto v = request.getParameter(name);
        if(nullptr != v) {
                 // Trim leading spaces.
                std::istringstream s{*v};
                std::getline(s >> std::ws, value);

                // Trim trailing spaces.
                auto i = std::find_if(value.rbegin(), value.rend(), [](char c) {
                                return !std::isspace(c, std::locale(Wt::WLocale::currentLocale().name()));
                         });
                value.erase(i.base(), value.end());
        }
</code>

Thank you for the JavaScript code.

Kind regards,

~Plug

RE: How to get user locale when WEnvironment is not available? - Added by Plug Gulp almost 4 years ago

I think I am lost in this...the locale name returned by WLocale is from the web browser (in my case 'en-GB'), where as the std::locale expects OS locale name (in my case 'en_GB.utf8'). std::locale throws an exception(locale::facet::_S_create_c_locale name not valid) when the WLocale name is used. Is there a way to use the name returned by WLocale with std::locale? What is the best way to use WLocale information with std::locale?

Thanks and kind regards,

~Plug

    (1-3/3)