如何让用户所在的国家/地区使用他的语言
我听说有很多依赖于 csv 或数据库等文件的方法 但我认为在我的网站上上传额外的数据库来做到这一点并不是一个好主意 我对外部供应商感觉良好 使用外部网站通过 IP 给出您的国家/地区是个好方法 或者它不好,因为服务器将等待外部提供商的响应,这会减慢网站速度?
i heard about many ways that depend of files like csv or database
but i think uploading an extra database on my site to do that is not good idea
i feel good about the external providers
is useing externial site that give your the country by the ip is good way
or its not good because the server will wait the response of the external provider and this will slow down the site?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看看这个:http://www.rubyquiz.com/quiz139.html
Look at this: http://www.rubyquiz.com/quiz139.html
无论如何,根据 IP 地址来选择语言实际上并不是一个好主意。如果我是从德国浏览的美国人,而且我的德语说得不太好怎么办?最符合标准的方法(我认为)是解析网络请求的 Accept-Language 标头,并使用它来设置用户的默认值,但总是为他们提供一种覆盖默认值的方法并选择他们的语言(然后您将其存储在他们的会话或用户首选项中)
It's actually not a great idea to base the language choice on the IP address anyway. What if I'm an American browsing from Germany, and I don't speak German very well? Your most standards-compliant way (I think) would be to parse the Accept-Language header of the web request, and use that to set a user's default, but always provide them a way to override the default and pick their language (which you'd then store in their session or user prefs)
我将使用 PHP 变量 $_SERVER['HTTP_ACCEPT_LANGUAGE'] ,在我的例子中,该变量保存此值 sk,cs;q=0.8,en-us;q=0.5,en;q =0.3。
这意味着,我的浏览器语言是“斯洛伐克语”。
我认为,这个选择更好。想象一下,您是英国人,但您正在某个地方度假。
您在那里使用笔记本...您的 IP 地址会告诉您的服务器,您在克罗地亚,并且您希望在他们的服务器中获取内容语言...但你的浏览器仍然说你是说英语的人...有区别;)
I would use PHP variable $_SERVER['HTTP_ACCEPT_LANGUAGE'] which, in my case, holds this value sk,cs;q=0.8,en-us;q=0.5,en;q=0.3.
That means, my browsers language is 'Slovak'.
I think, this option is better. Just imagine, that you are English, but you are on vacation somewhere.
You use your notebook over there... Your IP address would tell your server, that you are in Croatia and you'd like to get content in their language... But you browser still says you are english speaking person... There is the difference ;)
我不会为此使用 GeoIP - 有太多的情况会失败或产生错误的结果。
正如 @Paul 所说,HTTP Accept-Language 标头指定了浏览器中定义的用户语言首选项。您可以访问浏览器语言检测来查看浏览器的设置。
有关实际的示例,请参阅解析 Accept-Language 来检测用户的语言 。
另请记住,爬虫不使用 Accept-Language,因此确保您有一个使其可用的策略(例如每种语言内容的 URL)并包含在站点地图中非常重要。
另请参阅 Apache 模块 mod_negotiation 以进行内容选择。
I wouldn't use GeoIP for this - there are too many scenarios when it fails or produces the wrong results.
As @Paul says the HTTP Accept-Language header specifies the user's language preferences as defined in the browser. You can view what your browser is set to by visiting Browser Language Detection.
For a real worked example see Parse Accept-Language to detect a user's language.
Also remember that crawlers don't use Accept-Language so it is important to ensure that you have a strategy for making this available (e.g URLs for each language content) and include in sitemap.
Also see Apache Module mod_negotiation for content selection.