简体   繁体   English

如何通过IP地址检测用户的语言

[英]How to detect a user's language through their IP Address

I have developed a website www.tenxian.com. 我开发了一个网站www.tenxian.com。

It has three language versions, English, Japanese and Chinese. 它有三种语言版本,英语,日语和中文。 How can I write an effective PHP program which can automatically choose a language version based on the IP address of the visitor? 如何编写有效的PHP程序,可以根据访问者的IP地址自动选择语言版本?

If I use "if-else", the code would be much complicated; 如果我使用“if-else”,代码将会非常复杂; If I use switch-case, how to write it since the data which should be dealt with are IP ranges, not particular numbers. 如果我使用switch-case,如何写它,因为应该处理的数据是IP范围,而不是特定的数字。 Besides, I don't know these IP ranges 此外,我不知道这些IP范围

What is the easiest way to do it? 最简单的方法是什么?

Please, PLEASE, do not make the mistake of thinking that IP == language. 请,请不要错误地认为IP ==语言。 Look at the browsers accept-language header first, then the browser identification string which might contain the OS language, and only then take the IP into account. 首先查看浏览器accept-language标头,然后查看可能包含OS语言的浏览器标识字符串,然后再考虑IP。 In almost 100% of all cases the browser accept-language header will be present and sufficient. 在几乎100%的情况下,浏览器接受语言标题将存在并且足够。

And always give the user the choice to switch to another language. 并始终让用户选择切换到另一种语言。

Just apart from the simple case of a foreigner abroad, how do you determine the language for Belgium, where they speak French, Dutch and German? 除了国外外国人的简单案例之外,您如何确定比利时的语言,他们会说法语,荷兰语和德语? (Maybe that doesn't apply to your case, but just philosophically. :)). (也许这不适用于你的情况,但只是哲学上的。:))。

Yes please don't do it... Google does this and dreaking annoying.. I always get the thai version instead the english one from my browser. 是的,请不要这样做...谷歌这样做并且令人讨厌...我总是从我的浏览器获得泰语版本而不是英文版本。

Use the http headers from the browser. 使用浏览器中的http标头。

<?php
$ln = split(",",$_SERVER["HTTP_ACCEPT_LANGUAGE"]);
print_r($ln[0]);
?>

也许这会有所帮助: www.countryipblocks.net

take a look at the maxmind geoip module for PHP ( http://www.maxmind.com/app/php ), as for your data structure perhaps key it to the ISO-3166-1 country code which apache_note("GEOIP_COUNTRY_CODE"); 看一下PHP的maxmind geoip模块( http://www.maxmind.com/app/php ),你的数据结构可能是关键到ISO-3166-1国家代码apache_note("GEOIP_COUNTRY_CODE"); returns. 回报。

您可能想要使用某种形式的IP地理编码数据库( 示例 )。

Assuming you can convert IP ranges to one of your language choices, you could do this (all replies above): have all your messages in the applications stored in an associative array of this form. 假设您可以将IP范围转换为您的一种语言选择,您可以执行此操作(上面的所有回复):将应用程序中的所有消息存储在此表单的关联数组中。

$MESSAGES[$USER_LANGUAGE][$msgId]

where $USER_LANGUAGE can be chinese, japanese, or english (or any other equivalent enum). 其中$USER_LANGUAGE可以是中文,日文或英文(或任何其他等效的枚举)。 $msgId can be things like "login.successful", "login.fail" etc. Where ever you display messages to the user do not display hardcoded strings, make a reference to the variable using the $msgId . $msgId可以是“login.successful”,“login.fail”等。如果您向用户显示消息,则不显示硬编码字符串,请使用$msgId引用该变量。

You can access it as a global variable OR you can create a function that takes in the $msgId as a parameter and returns the message, $USER_LANGUAGE can be a global variable as well (which is set the first time the user comes in). 您可以将其作为全局变量访问,或者您可以创建一个函数,该函数将$ msgId作为参数接收并返回消息, $USER_LANGUAGE也可以是全局变量(在用户第一次进入时设置)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM