为 Rails i18n 设置国家/地区表
在准备国际化我们的 Rails 应用程序时,我发现我们需要创建一个国家/地区表和模型,其中包含(除其他外)每个国家/地区的默认区域设置。我找到了一个 4 年前的资源 http://snippets.dzone.com/posts/show/1727< /a> 包含我想要的大部分内容,但没有语言环境。每个人都必须手动将这部分拼凑在一起,还是我忽略了一些将国家/地区映射到默认区域设置的更简单的方法。
PS 我们已经使用 GeoIpCountry 查找用户所在国家/地区,效果很好,但该表仅提供国家/地区名称和 2 个字母的缩写。我还想使用国家/地区来分配用户默认区域设置、货币和 3 字母缩写(第三方集成需要)
In preparation to internationalize our Rails app, I'm finding we need to create a countries table and model that would contain (among other things) the default locale for each country. I found a 4 year old resource http://snippets.dzone.com/posts/show/1727 that has most of what I want, but not locale. Does everybody have to piece this part together by hand or am I overlooking some easier way to map country to a default locale.
P.S. We already look up a users country using GeoIpCountry which works nicely, but that table only provides country name and 2-letter abbreviation. I also want to use country to assign the users default locale, currency, and 3-letter abbreviation (needed by a third party integration)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
识别用户区域设置的最佳方法是查找 HTTP 请求的 Accept-Language 设置。
例如,我的说:
Accept-Language: fr-BE
这为您提供了我的区域设置、我的语言(fr:法语)和我的国家/地区(BE:比利时)。
另外,请记住,基于国家/地区的区域设置识别在许多国家/地区都是错误的,例如我的国家:许多网站正确识别我的国家/地区(比利时)并假设默认区域设置:nl-BE,因为 60% 的比利时人说荷兰语 (nl)。因此,这些网站对 40% 的比利时游客(包括我)来说都是错误的。
他们的网站对我来说是一次糟糕的体验,因为它以不属于我的语言显示,而我必须做的第一件事就是寻找他们的小语言弹出菜单。
如果他们只是查找我的
Accept-Language
,他们就会立即以我的语言向我展示他们的网站。The best way to identify user's locale is to look up the HTTP request's Accept-Language setting.
e.g., mine says:
Accept-Language: fr-BE
Which gives you my locale, my language (fr: french) and my country (BE: Belgium).
Also, remember that locale identification based on country is wrong in many countries, such as mine: Many websites identify my country (Belgium) correctly and assume a default locale: nl-BE because 60% of belgian people speak Dutch (nl). Hence these sites have it wrong for 40% of belgian visitors (including me).
Their site is a bad experience for me because it displays in a language which is not mine and the first thing I must do is look for their tiny language popup menu.
Had they simply looked up my
Accept-Language
, they would have shown me their site in my language right away.看看:https://github.com/mm1/country-list
have a look at: https://github.com/mm1/country-list