-->

Display website in a user's native language ba

2019-08-10 05:24发布

问题:

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.