I have a list of country codes and a list of language codes. How do I map from country code to language code?

后端 未结 2 501
予麋鹿
予麋鹿 2021-01-02 19:02

When the user visits the site, I can get their country code. I want to use this to set the default language (which they can later modify if necessary, just a general guess

2条回答
  •  借酒劲吻你
    2021-01-02 19:45

    While there may be good reasons to try and map countries (or indeed specific locations/areas within a country) to languages, it seems that for your purpose, using the

    HTTP Request header Accept-Language,

    if only as a one of the elements in your language selection heuristic (along with mabye teh country code) may be an easy and safe choice...

    This approach occasionally errs because the default language(s) associated with a given web browser installation (when the underlying user has not taken the time to select a installation package / setup for his/her preferred natural language). On the other hand it also provides a better language hint for many expatriates (who have install / setup a browser in accordance to preferred language) even though the geolocation / language association would be totally wrong.

    In general this approach should outperform methods solely based on geolocation (and on a lookup table), in the many countries such as Switzerland, Luxembourg... as well as many metropolitan areas say New York, Geneva or Miami...

提交回复
热议问题