Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

figure out missing region information #5

Closed
smhg opened this issue Oct 17, 2016 · 5 comments
Closed

figure out missing region information #5

smhg opened this issue Oct 17, 2016 · 5 comments
Labels

Comments

@smhg
Copy link
Owner

smhg commented Oct 17, 2016

@ekkis wrote:
I notice that setting my language preference to Swedish (in Chrome) sets the language to 'sv' but includes no locale, so whereas I might have 'sv-SE' (Sweden), I could also have 'sv-FI' (Finland) but without locale I don't know how to properly format a price (because I don't know the currency). that seems to be browser dependent though as Chrome doesn't set it but Firefox does. so is there a way to tell the locale when the browser has not set it?

@smhg smhg added the question label Oct 17, 2016
@smhg smhg mentioned this issue Oct 17, 2016
@smhg
Copy link
Owner Author

smhg commented Oct 17, 2016

In short, no.
You might need to look at an IP-to-location lookup. If that fits your project.

@smhg smhg closed this as completed Oct 17, 2016
@ekkis
Copy link

ekkis commented Oct 17, 2016

wow. one would think the browser makers would include this information in the headers since, after all, it's tremendously useful. but thanks for answering the question

@smhg
Copy link
Owner Author

smhg commented Oct 18, 2016

I was a bit quick yesterday, but in the end the answer probably is to get the location from the IP.

However there are 2 nuances:

  • In many cases browsers will send something like:
Accept-Language: de,de-CH;q=0.8,en;q=0.6

In which case express-locale uses the first full locale (de-CH).

  • Secondly, you can provide a mapping in the middleware configuration to turn de into de_DE (for everyone):
express()
  .use(createLocaleMiddleware({
    map: {
      language: {
        de: 'de_DE'
      }
    }
  });

Depending on your situation the above might be enough to avoid using IP lookups.

@ekkis
Copy link

ekkis commented Oct 20, 2016

yes, I did notice that sometimes I get a locale. it has to do with the particular list of languages that the browser displays to the user. sometimes you can pick German/Austria and sometimes it's just German. as for the second point, mapping de to de-DE is not feasible. you can make the mapping but you really don't know where the user is from so you'll get it wrong. so yeah, IP seems to be the only way to do it, which of course, will run into issues when the user is behind a VPN, but I guess there just isn't a good way right. someone needs to fix this

@smhg
Copy link
Owner Author

smhg commented Oct 20, 2016

You present a region (country) selection to your user when detection fails?
Depends how 'global' your project is I guess. If you only server one language in multiple (close) regions, it gets weirder.

For future reference: I guess something like ['path', 'cookie', 'ip', 'accept-language', 'default'] as a default priority for express-locale makes sense for larger, multi-locale projects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants