在 Django 应用程序中检查访问者的地理位置
我正在制作一个多语言 Django 网站。我希望该网站检查用户从哪个国家/地区访问该网站,并根据该情况决定使用哪种语言。
有什么好的工具可以用于此目的?
I am making a multilingual Django website. I want the site to check from which country the user is accessing the website, and according to that decide which language to use.
What would be a good tool to use for that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您的一位常规用户带着笔记本电脑到另一个国家旅行并访问您的网站时,无法理解任何内容并且无法更改语言,那会很糟糕。
我不建议检查国家/地区(可能通过 IP)。相反,请使用客户端浏览器发送的
Accept-Language
标头。您仍然应该保留让用户手动更改语言的选项(例如将首选项存储在会话中)。另一种选择是采用维基百科风格并通过 URL 设置语言(en.example.com、de.example.com 或 example.com/en/、example.com/de/)。
When one of your regular users travels with his laptop to another country and access your site, can't understand a thing and can't change the language, that would suck.
I wouldn't recommend checking for the country (by IP probably). Instead go for the
Accept-Language
header the browser of the client sends. You should still leave the option to let the user change the language manually (and store the preference in the session for example).Another option is going wikipedia style and setting the language by URL (en.example.com, de.example.com or example.com/en/, example.com/de/).
我在我的网站上遇到了类似的问题,我使用 MaxMind IP 免费数据库作为模型,它被称为作为中间件。
因此,当用户访问该网站时,中间件会通过他的 IP 检测他所在的国家/地区,并将他重定向到正确的语言
I had a similar problem on my site, and I used the MaxMind IP free database as a model and it's called as middleware.
So as the user goes to the site, the middleware detects his country by his IP and redirects him to the right language
有多种方法可以做到这一点,但一个好的解决方案是使用地理定位套件。根据您的预算,有免费和付费的解决方案。我过去曾使用 Digital Element 产品 (NetAcuity) 来处理此类事情,他们的产品解决方案是一流的。他们提供内部服务器解决方案(适用于高性能应用程序。每秒约 30k 查询)和适用于更典型应用程序的 Web 服务解决方案。他们拥有多种语言的 API,包括可以在 Django 中使用的 Python。它们将允许您根据用户的 IP 地址查询地理位置、主要和次要语言、连接速度等信息。根据我的经验,国家一级的地理精度非常准确。
还可以考虑的一个免费解决方案是 MaxMind,尽管我不太熟悉他们的产品。
There is more than one way to do this, but a good solution is to use a geotargeting suite. Depending on your budget, there are free and paid solutions. I've used Digital Element's product (NetAcuity) for this type of thing in the past, and their solutions are top-notch. They provide both a in house server solution (for high-performance applications. ~30k queries per second) and a web-service solution for more typical applications. They have API's in numerous languages including Python that you could use within Django. They will allow you to query for information such as geo location, primary and secondary language, connection-speed, etc. all based on the users IP address. Geo accuracy at the the country level is highly accurate based on my experience.
A free solution to also consider is MaxMind, though I'm not as familiar with their product.