Display website in a user's native language based on the IP address

混江龙づ霸主 提交于 2019-12-13 02:35:05

问题


I want my website to be opened in a visitor's native language as determined by the IP address where the user has logged in from.

By default the page opens in English, but I want my customers from France to read the site in French. I have gone through www.ipinfo.io for information, but I am still a bit confused and would like to know the json format.


回答1:


You should use the accept-language header that the user's browser passes to your site to determine the language, rather than the user's current country.

If you do want to customize the site content based on the country the http://ipinfo.io code you'd use would look something like this:

$.get("http://ipinfo.io", function(response) {
    if(response.country == 'US') {
        // User is in the USA
    } else {
        // User is elsewhere 
    }
}, "jsonp");

You can find a full mapping of country codes to things like names, currency codes, dialing codes and more at http://country.io/data/




回答2:


I would advise against using the IP alone to determine the display language. Using the Accept-Language header is far more reliable.

Consider the following relatively common scenario: An only-English-speaking person travels to Russia and opens your website. Then the Russian landing page is displayed in Cyrillic.




回答3:


This doesn't answer your last question, but it is a better solution to your problem.

Rather than using an IP address to determine locale, use localization based on the browser's "Accept-Language" header. This header is typically set by default when installing a browser based on your operating system's localization settings.

Most modern UI frameworks (be it web, mobile, or desktop) have localization/internationalization libraries.



来源:https://stackoverflow.com/questions/28630007/display-website-in-a-users-native-language-based-on-the-ip-address

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!