1

I am currently working on a web application and I try to make the website multilingual. From The user I got the following things:

  • language - from $_SERVER['HTTP_ACCEPT_LANGUAGE']
  • country - from the useres IP address (with an API)

Now, the website takes the language and shows the page in the correct language - for example english or german. Now I want to format numbers and dates and I dont know what should I use to format a date for example:

  • Germany: dd.MM.yyyy
  • United States: MM/dd/yyyy

But what should I use to format the date? The language or the country? What if a user is in the US but speaks german? Should I use the US or the german format?

Would be nice to hear your opinion about that and how you handle localization.

ropip31271
  • 13
  • 2

1 Answers1

0

The questions you are asking are solved using Locales. A locale is specific to a language, and then possibly more specific with geographical information and script variants.

CLDR is a repository of information relating to locales. You can find the names of specific cities in any languages, for example, or see how they format dates in a specific area.

You can leverage this data in conjunction with the Globalize.js library and its date formatting module.

See this answer for a general overview of how to use Globalize.js.

Codebling
  • 10,764
  • 2
  • 38
  • 66
  • Okay, sounds good - but how to get the locale? Is it just lang_COUNTRY so en_US or de_DE? And when the user selected german as language and us as country? Will it be de_US? – ropip31271 Apr 26 '20 at 00:09
  • The `Accept-Language` header that you've already mentioned actually [passes a locale](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Language), not just a language, so you can use this. It passes a list of accepted locales, usually sorted by user preference. – Codebling Apr 26 '20 at 00:27
  • @ropip31271 and yes, if the `de_US` would be the locale. That doesn't necessarily mean that any differences exist between that `de_US` and `de`. I don't remember how Globalize handles it, but usually the behaviour is to fall back to the most specific locale that we DO have data for. – Codebling Apr 26 '20 at 00:31
  • So just pop whatever you get from the browser into Globalize. If your site lets the user select the language, default to the first language in `Accept-Language`, but it's polite to offer the user a choice of any other languages passed in the header, as well as any languages that might be typical for an area (for example, you would know IP geolocation API that the visitor is from the US and they speak English in the US, so it would be nice to also offer that option) – Codebling Apr 26 '20 at 00:35
  • 1
    okay, so i can generate the locale also by getting the lang from accept lang and the country from the users ip and then generate a locale with `lang_COUNTRY` which will be used by globalize.js. and if the user changes one part - country or lang - i should generate this locale new and then reload or whatever. i just want to be clear, that the user can change the locale if he wants. i think in 90% of cases it would be a normal locale like en_US en_GB or something else, but in some cases it may be en_DE so something what isnt very common - i just want to handle this the best way.thanks – ropip31271 Apr 26 '20 at 00:38
  • 1
    yeah, i will offer all supported languages for my site - so all languages the app is translated in. and i will only let him choose between supported countries bc my app doest support all. – ropip31271 Apr 26 '20 at 00:40
  • on the first visit the browser gets all this infos - in the footer the user can change country and lang – ropip31271 Apr 26 '20 at 00:42
  • @ropip31271 "generate the locale also by getting the lang from accept lang and the country from the users ip and then generate a locale with lang_COUNTRY" -- no. And also yes.. Just to make sure there is no confusion, `Accept-Language` passes a locale like `de_US` already. You ***can*** generate an additional locale by taking the language from this header and adding the country (this was the "yes" part), but you **do not** have to (this was the "no" part), you can just use the locale from the `Accept-Language` directly without modifying. – Codebling Apr 26 '20 at 00:53
  • Pretty sure you already understood that, just want to make sure it's clear! Good luck and also welcome to StackOverflow! – Codebling Apr 26 '20 at 00:54
  • 1
    yeah, i understood - i now looked at some big sites like google, youtube and amazon and understood how they handle it. and thanks, hope i can learn a lot here :P – ropip31271 Apr 26 '20 at 00:56