如何通过其IP地址检测用户的语言 [英] How to detect a user's language through their IP Address

查看:106
本文介绍了如何通过其IP地址检测用户的语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开发了一个网站www.tenxian.com.

I have developed a website www.tenxian.com.

它具有三种语言版本,英语,日语和中文.如何编写有效的PHP程序,该程序可以根据访问者的IP地址自动选择语言版本?

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?

如果我使用"if-else",代码将非常复杂;如果我使用开关盒,由于应该处理的数据是IP范围,而不是特定的数字,因此如何编写它.此外,我不知道这些IP范围

If I use "if-else", the code would be much complicated; If I use switch-case, how to write it since the data which should be dealt with are IP ranges, not particular numbers. Besides, I don't know these IP ranges

最简单的方法是什么?

推荐答案

请不要误以为IP ==语言.首先查看浏览器的accept-language标头,然后查看可能包含OS语言的浏览器标识字符串,然后才考虑IP.在几乎所有情况下,浏览器的接受语言标头几乎都会有100%出现.

Please, PLEASE, do not make the mistake of thinking that IP == language. 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. In almost 100% of all cases the browser accept-language header will be present and sufficient.

并始终让用户选择切换到另一种语言.

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. :)).

这篇关于如何通过其IP地址检测用户的语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆