How to set language of web page determined by visitor language

匆匆过客 提交于 2019-12-13 00:19:46

问题


Can someone tell me how to set language of a web page on the knowledge of IP address of visitor so that he automatically gets page in his country's language. Of course if that language is implemented by web developer. Even better I sow some examples of automatic Google translation of the text.

So how to achieve that visitor from USA gets text on my page in English and visitor from France in French. All that translated by Google from some third original language.

Thanks a lot.


回答1:


Either:

  • Guess language from location (which you can get from their IP).

  • Look at the request header's "accept-language" value.

To see what's in your request header, have a look here: http://www.ericgiguere.com/tools/http-header-viewer.html




回答2:


When it come to automatic language selection I'd go the mod_rewrite route if you're using Apache. It's easier to change in a production environment that touching application code. mod_rewrite grabs the "Accept-Language" out of the header then applies the rewrite rule.

RewriteCond %{HTTP:Accept-Language} ^fr [NC]

RewriteRule ^$ /fr/ [L,R=301]

You can stack the rewrite conditions and rules to work as a catch all language variations (fr, fr-ca, fr-fr, fr-mo, fr-ch all go to fr)

Checkout the official mod-rewrite documentation

good language example: http://tech-blog.borychowski.com/index.php/2009/03/htaccess/redirect-according-to-browser-language-mod-rewrite-and-http_accept_language/

Once you push the user to the right general language (when none is defined in the URL) the application can set a session language, write links with the set lang. It's also good to allow people to change language on the fly since most users in bilingual locals (i.e. Quebec) work in more than one language. I've worked with French speaking programmers who prefer reading technical documents in English.

When it comes to Google translating text I'd be careful. If you're doing any e-commerce transaction your international customers (or local customers with international browser settings) may get incorrect or inaccurate product information, descriptions and "terms and conditions". If you don't save the exact Google translation text to your DB for every on the fly translation there is no way to track what the user has committed to in their language. Some non-translated legal copy may be in order.

I hope this helps.




回答3:


This is a bad idea from the offset.

  1. Geolocating on IP address doesn't always work (and doesn't work if you use TOR)
  2. Even if you get this right, what would you do for countries like Canada or Switzerland?
  3. Automatic translation is ok, but it would be better to tell someone that you don't have the site in their language - they may then prefer to read it in a different language.


来源:https://stackoverflow.com/questions/1826603/how-to-set-language-of-web-page-determined-by-visitor-language

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