Javascript 识别用户位置/IP 并更改语言的网站。以此为基础

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2039016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:31:20  来源:igfitidea点击:

Website that recognizes user's location/IP & changes lang. based on that

javascripthtmlwebgeolocation

提问by cr0z3r

title is pretty clear.

标题很清楚。

My websites consists of both English-written and Spanish-written versions. You can go to the main site, which is in Spanish, by clicking http://www.chrishonn.comand to the translated version, which is in English, at http://en.chrishonn.com. At the index of each page there is a link (at the bottom) which allow the user to pass from one site to the other.

我的网站由英文书写和西班牙文书写的版本组成。您可以通过单击http://www.chrishonn.com 访问主站点(西班牙语版本)和英语翻译版本,网址http://en.chrishonn.com。在每个页面的索引处都有一个链接(在底部),允许用户从一个站点转到另一个站点。

However, I was wondering, how do big sites like Google, Yahoo!, and other brands' websites to recognize the user's geographical location/IP so that - depending on that - the site's language is adapted (i.e. you are from China and you visit www.google.com, you'll be redirected to www.google.cn).

但是,我想知道,像 Google、Yahoo! 和其他品牌的网站这样的大网站如何识别用户的地理位置/IP 以便 - 取决于它 - 网站的语言被调整(即您来自china并且您访问www.google.com,您将被重定向到 www.google.cn)。



I have stated on every single page of my website the language:

我在我网站的每一页都说明了语言:

<meta http-equiv="content-language" content="en">

That example is of course from one of the http://en.chrishonn.comsites, which are in English.

该示例当然来自http://en.chrishonn.com站点之一,该站点是英文的。

I hope someone can give me a hand. Thank you (if I missed something, please let me know).

我希望有人能帮我一把。谢谢(如果我错过了什么,请告诉我)。

回答by AUSteve

Rather than detecting the location of IP address (often unreliable due to NAT and proxying) you could check the default language the browser is set to. There are JQuery plugins to support this, such as http://keith-wood.name/localisation.html, or use server-side code to read the HTTP request header "HTTP_ACCEPT_LANGUAGE" to determine if you want to show the ES or EN site.

您可以检查浏览器设置的默认语言,而不是检测 IP 地址的位置(由于 NAT 和代理,通常不可靠)。有 JQuery 插件来支持这一点,比如http://keith-wood.name/localisation.html,或者使用服务器端代码读取 HTTP 请求头“HTTP_ACCEPT_LANGUAGE”来确定是否要显示 ES 或 EN地点。

回答by Daniel Vassallo

As for Google, your location is determined from your IP address. A request to google.com from outside the US returns an HTTP/1.1 302 Foundwhich redirects you to your country specific domain.

对于 Google,您的位置是根据您的 IP 地址确定的。从美国境外向 google.com 发出的请求会返回一个HTTP/1.1 302 Found,将您重定向到您所在国家/地区的特定域。

As also discussed on another post, doing these kinds of redirects can make SEO tricky and complicated. I suggest reading Matt Cutt's article (a Google software engineer) on how Google handles the 302 Redirect: SEO advice: discussing 302 redirects.

正如在另一篇文章中所讨论的那样,执行这些类型的重定向会使 SEO 变得棘手和复杂。我建议阅读Matt Cutt关于 Google 如何处理 302 重定向的文章(Google 软件工程师):SEO 建议:讨论 302 重定向

Different search engines handle the 302 redirect in a different way. With 302 redirects, you may risk having your original domain ignored by search engines.

不同的搜索引擎以不同的方式处理 302 重定向。使用 302 重定向,您可能会冒着搜索引擎忽略您的原始域的风险。



If you want to determine your users' location from their IP address, there are many off-the-shelf services which basically map most of the IP ranges to countries. You may want to check:

如果您想根据用户的 IP 地址确定用户的位置,有许多现成的服务基本上将大部分 IP 范围映射到国家/地区。您可能需要检查:



Another popular technique is to parse the Accept-Language HTTP header, which contains information about the user's language preferences. Mainstream browsers allow these language preferences to be modified by the user. You may read more about this technique from:

另一种流行的技术是解析 Accept-Language HTTP 标头,其中包含有关用户语言首选项的信息。主流浏览器允许用户修改这些语言首选项。您可以从以下位置阅读有关此技术的更多信息:



回答by dusoft

some website use IP address-based geo location, some us Accept language header (can be set in the browser). Anyway, from the usability point of view - always allow people to change the language and never display different content on the same IP (google and other search engines doesn't like it and it would be bad from SEO point of view).

一些网站使用基于IP地址的地理位置,一些我们接受语言标题(可以在浏览器中设置)。无论如何,从可用性的角度来看 - 始终允许人们更改语言并且永远不会在同一 IP 上显示不同的内容(谷歌和其他搜索引擎不喜欢它,从 SEO 的角度来看这会很糟糕)。

回答by zombat

These sites often use tools that are generically called "geolocation software".

这些站点通常使用通常称为“地理定位软件”的工具。

One of the most popular packages is the free GeoLite Countrydatabase offered by MaxMind. This will integrate into your application and provide IP-to-country lookups. With Apache, you will have some environment variables set called GEOIP_COUNTRY_CODE and GEOIP_COUNTRY_NAME.

最受欢迎的软件包之一是MaxMind提供的免费GeoLite Country数据库。这将集成到您的应用程序中并提供 IP 到国家/地区的查找。使用 Apache,您将设置一些名为 GEOIP_COUNTRY_CODE 和 GEOIP_COUNTRY_NAME 的环境变量。

All your application has to do after that is decide where your user should be or what settings they should have by default based on the country, and redirect or output appropriately.

在此之后,您的应用程序要做的就是根据国家/地区确定您的用户应该在哪里或他们应该具有哪些默认设置,并适当地重定向或输出。

回答by Marcel Panse

You can do a GET request to this URL: https://api.teletext.io/api/v1/geo-ipand it will respond with some country information in JSON format. See https://teletext.io/help/geo-location-servicefor usage.

你可以向这个 URL 发出 GET 请求:https: //api.teletext.io/api/v1/geo-ip,它会以 JSON 格式响应一些国家信息。有关用法,请参阅https://teletext.io/help/geo-location-service

If you want to implement it yourself: I've set this up by using AWS Cloudfront, Cloudfront adds a header (Cloudfront-Viewer-Country) to your request before it forwards it to the origin server that you can use. Make sure to whitelist the header in your Cloudfront distribution.

如果您想自己实现它:我已经使用 AWS Cloudfront 进行了设置,Cloudfront 在将请求转发到您可以使用的源服务器之前向您的请求添加了一个标头 (Cloudfront-Viewer-Country)。确保将 Cloudfront 分配中的标头列入白名单。

回答by Sachin Aware

I have created cloud distribution and white listed the CloudFront-Viewer-Country header. But when I accessed object using this distribution I did not see it in request header.

我创建了云分布并将 CloudFront-Viewer-Country 标头列入白名单。但是当我使用这个分布访问对象时,我没有在请求头中看到它。