Rails 还有王法吗?我们约好的呢?

Peter · 2015年01月26日 · 最后由 darkbaby123 回复于 2015年01月27日 · 3090 次阅读

Rails 广告里面就说“约定大于配置”,但有些地方还是看不懂。

比如 Discourse 的语言文件: https://github.com/discourse/discourse/blob/master/config/locales/client.zh_CN.yml#L8

本来中文的 code 是 zh-CN,但 discourse 不管是文件名还是里面的内容都是写的下划线 zh_CN

有没有人研究过这个问题,用中划线的确不方便,一个一般的项目怎么使用下划线呢?

symbol :zh_CN

這個..我都是跟 guides 裡面用雙字母縮寫的,比如 en es sc tc 啥的

#1 楼 @40hood #3 楼 @flowerwrong 我的问题不在 symbol

请看: https://github.com/svenfuchs/rails-i18n

Available Locales 标准是用zh-CN,不是下划线,如果用下划线会报错,因为 config/application.rb 里面定义的是中划线。如果在这定义下划线,那 rails-i18n 这样的 gem 又会报错。

我想知道 Discourse 用的下划线,项目还能跑起来,是怎么实现的。

特意研究过这个问题,其实问题出在 Rails 自身,而不是 Discourse 的问题。

Discourse 使用的是 Transifex 来做 i18n 的,但是 Transifex 使用的是 ISO 639-1,所以标准应该就是 zh_CN 而不是 Rails 中的 zh-CN。Transifex 作为一个工具来说使用标准并没有问题,Discourse 并没有做 Rails 到 Transifex 的转换,是遵循了标准,没有在迁就 Rails 做转化而已。

Transifex by default uses certain language codes as they're described by ISO 639-1. Since a lot of projects have their own mappings and are using different language codes than the ones described by the ISO standard, the transifex-client offers a quick and easy way to setup your custom one-to-one language mappings.

其实 Rails 为了图方便,大幅简化很多 ISO 标准相关的配置,不光在语言方面,在时区设置上也是如此,方便的同时,如果需要兼顾标准,就得自己做一个中间转换层,所以有利有弊。

@lgn21st 你这话让我想起以前碰到的时区问题了。Rails 的时区字符串是跟标准不同的,ActiveSupport::TimeZone 文档里提到了这个:

The TimeZone class serves as a wrapper around TZInfo::Timezone instances. It allows us to do the following:

  • Limit the set of zones provided by TZInfo to a meaningful subset of 146 zones.
  • Retrieve and display zones with a friendlier name (e.g., “Eastern Time (US & Canada)” instead of “America/New_York”)

TZInfo 文档里使用的才是标准的时区,Rails 做了简化处理。不过设置时区时既可以传 Rails 定义的简化版本也可以用 TZInfo 的标准版本。这点还是挺方便的。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号