English 中文(简体)
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 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.

最佳回答

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

问题回答

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.

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.




相关问题
django blocktrans and i18n in templates

I have an i18n problem in django: This works fine : {% trans cat.name %} cat.name will be translated But this doesn t work: {% blocktrans with cat.name|slugify as cat_slug %}{{ cat_slug }}{%...

internationalization of php website

I am currently working on a project / website and I will need to make it available in several languages. The site was done with PHP / mysql and a lot of javascript (jQuery). I have no idea where to ...

ASP.NET component to edit .resx files

I m developing a multilingual web site and the localization is done mostly by using .resx files. But now I need to enable users with some permissions to edit resource files online from the web site. ...

converting MATLAB code to Fortran [closed]

I a medical researcher with code written in MATLAB 2009b that runs very slowly because of a self-referential loop (not sure of the programming lingo here), i.e., the results of the first iteration is ...

determining the character set to use

my delphi 2009 app has a basic translation system that uses GNUGetText. i had used some win API calls to prepare the fonts. i thought it was working correctly until recently when someone from Malta ...

热门标签