How to set language of web page determined by visitor language
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开发者_如何学Go 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.
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.
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
This is a bad idea from the offset.
- Geolocating on IP address doesn't always work (and doesn't work if you use TOR)
- Even if you get this right, what would you do for countries like Canada or Switzerland?
- 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.
精彩评论