4

我需要使用 PHP 获取通过 IP 地址访问我网站的用户的语言。我该怎么做,有任何API可以做到这一点,请指教

4

3 回答 3

5

按照之前的建议使用 GeoIP 模块,然后将此代码插入项目中的某处:

if($country = geoip_country_code_by_name($host))
{
    switch($country)
    {
        case "DJ":
        case "ER":
        case "ET":

            $lang = "aa";
            break;

        case "AE":
        case "BH":
        case "DZ":
        case "EG":
        case "IQ":
        case "JO":
        case "KW":
        case "LB":
        case "LY":
        case "MA":
        case "OM":
        case "QA":
        case "SA":
        case "SD":
        case "SY":
        case "TN":
        case "YE":

            $lang = "ar";
            break;

        case "AZ":

            $lang = "az";
            break;

        case "BY":

            $lang = "be";
            break;

        case "BG":

            $lang = "bg";
            break;

        case "BD":

            $lang = "bn";
            break;

        case "BA":

            $lang = "bs";
            break;

        case "CZ":

            $lang = "cs";
            break;

        case "DK":

            $lang = "da";
            break;

        case "AT":
        case "CH":
        case "DE":
        case "LU":

            $lang = "de";
            break;

        case "MV":

            $lang = "dv";
            break;

        case "BT":

            $lang = "dz";
            break;

        case "GR":

            $lang = "el";
            break;

        case "AG":
        case "AI":
        case "AQ":
        case "AS":
        case "AU":
        case "BB":
        case "BW":
        case "CA":
        case "GB":
        case "IE":
        case "KE":
        case "NG":
        case "NZ":
        case "PH":
        case "SG":
        case "US":
        case "ZA":
        case "ZM":
        case "ZW":

            $lang = "en";
            break;

        case "AD":
        case "AR":
        case "BO":
        case "CL":
        case "CO":
        case "CR":
        case "CU":
        case "DO":
        case "EC":
        case "ES":
        case "GT":
        case "HN":
        case "MX":
        case "NI":
        case "PA":
        case "PE":
        case "PR":
        case "PY":
        case "SV":
        case "UY":
        case "VE":

            $lang = "es";
            break;

        case "EE":

            $lang = "et";
            break;

        case "IR":

            $lang = "fa";
            break;

        case "FI":

            $lang = "fi";
            break;

        case "FO":

            $lang = "fo";
            break;

        case "BE":
        case "FR":
        case "SN":

            $lang = "fr";
            break;

        case "IL":

            $lang = "he";
            break;

        case "IN":

            $lang = "hi";
            break;

        case "HR":

            $lang = "hr";
            break;

        case "HT":

            $lang = "ht";
            break;

        case "HU":

            $lang = "hu";
            break;

        case "AM":

            $lang = "hy";
            break;

        case "ID":

            $lang = "id";
            break;

        case "IS":

            $lang = "is";
            break;

        case "IT":

            $lang = "it";
            break;

        case "JP":

            $lang = "ja";
            break;

        case "GE":

            $lang = "ka";
            break;

        case "KZ":

            $lang = "kk";
            break;

        case "GL":

            $lang = "kl";
            break;

        case "KH":

            $lang = "km";
            break;

        case "KR":

            $lang = "ko";
            break;

        case "KG":

            $lang = "ky";
            break;

        case "UG":

            $lang = "lg";
            break;

        case "LA":

            $lang = "lo";
            break;

        case "LT":

            $lang = "lt";
            break;

        case "LV":

            $lang = "lv";
            break;

        case "MG":

            $lang = "mg";
            break;

        case "MK":

            $lang = "mk";
            break;

        case "MN":

            $lang = "mn";
            break;

        case "MY":

            $lang = "ms";
            break;

        case "MT":

            $lang = "mt";
            break;

        case "MM":

            $lang = "my";
            break;

        case "NP":

            $lang = "ne";
            break;

        case "AW":
        case "NL":

            $lang = "nl";
            break;

        case "NO":

            $lang = "no";
            break;

        case "PL":

            $lang = "pl";
            break;

        case "AF":

            $lang = "ps";
            break;

        case "AO":
        case "BR":
        case "PT":

            $lang = "pt";
            break;

        case "RO":

            $lang = "ro";
            break;

        case "RU":
        case "UA":

            $lang = "ru";
            break;

        case "RW":

            $lang = "rw";
            break;

        case "AX":

            $lang = "se";
            break;

        case "SK":

            $lang = "sk";
            break;

        case "SI":

            $lang = "sl";
            break;

        case "SO":

            $lang = "so";
            break;

        case "AL":

            $lang = "sq";
            break;

        case "ME":
        case "RS":

            $lang = "sr";
            break;

        case "SE":

            $lang = "sv";
            break;

        case "TZ":

            $lang = "sw";
            break;

        case "LK":

            $lang = "ta";
            break;

        case "TJ":

            $lang = "tg";
            break;

        case "TH":

            $lang = "th";
            break;

        case "TM":

            $lang = "tk";
            break;

        case "CY":
        case "TR":

            $lang = "tr";
            break;

        case "PK":

            $lang = "ur";
            break;

        case "UZ":

            $lang = "uz";
            break;

        case "VN":

            $lang = "vi";
            break;

        case "CN":
        case "HK":
        case "TW":

            $lang = "zh";
            break;

        default:break;
    }
}
于 2014-01-07T14:12:35.177 回答
4

您可以使用任何 geoIP 模块。它允许您通过 IP 检测国家/地区。但这不是很正确的方法。例如我现在在泰国(我有泰国 IP),但我的语言是俄语 :)

当谷歌向我展示泰语页面时,这不是很好。

对于检测语言,您可以使用浏览器中的标题。首选语言列在此标题中。

于 2012-11-27T13:22:20.317 回答
1

虽然您可以查找 IP 地址以了解一般物理位置,但物理位置可能与您正在查找其 IP 地址的人的语言没有太大关系。

例如,虽然 IP 位置可能是美国的纽约地区,但该人可能会说普通话作为他们的主要语言,因为他们正在访问该地区。

这是一项可用于 IP 地址查找的服务http://ipinfodb.com/ip_location_api.php

这是一个 stackoverflow 讨论Get user location by IP address in C#可能也有帮助。

但是,您需要提供一种允许更改语言选择的机制。并使用 cookie 或类似的东西来记住语言选择。

位置信息最常见的用途是提供更有可能对该位置的人有用的有针对性的广告。

编辑:使用移动设备

由于通过高速 3G 和 4G 蜂窝网络访问互联网的移动设备和智能手机变得越来越普遍,一个问题是 IP 地址地理定位是否适用于这些类型的设备。

简短的回答是,不太好。请参阅这篇文章,电话在哪里?: Microsoft Research的 3G 网络上的 IP 地址地理定位。

此外,这是一个来自 HTTP 请求的关于手机位置的 stackoverflow 问题

这是一篇文章,在蜂窝数据网络中地理定位 IP 地址,它也提供了有关该问题的一些信息。

于 2012-11-27T13:23:14.793 回答