新手问题 i18n 国际化问题

autumnwolf · 2014年03月19日 · 最后由 autumnwolf 回复于 2014年03月22日 · 2594 次阅读

本地化时候发生错误:ranslation missing: zh-CN.product.tite 如下:

application.rb

config.i18n.load_path += Dir[Rails.root.join('config', 'locales','*.{rb,yml}').to_s]
config.i18n.default_locale = :"zh-CN"

创建: /config/locales/zh-CN.yml

zh-CN:
  product:
    title:产品

视图 view:

<h1><%= I18n.t('product.title') %></h1>

如果把视图中代码换成 t,也就是快捷方式

<h1><%= t('product.title') %></h1>

视图就会显示"title"。 重启服务器了。昨天开始弄本地化的,网上找了下,感觉很简单,好折磨人。求助

好吧 静下来沉思了一会,把“title:产品”改成“title: 产品”多了个空格好用了。。。 yml!

理论上是后面的那个产品加双引号,而且对应到视图上,直接 t( ) 就可以了

product.title 建议安 ActiveRecord 的写法

#2 楼 @hanluner 这个没用双引号也可以,t( ) 现在也好用了

#3 楼 @swordray 你说的是 t(:product, :scope => :title ) 这样?

还发现个问题是看: “注意 YAML 格式的縮排必須使用兩個空隔,Tab 是不允許的。直接複製貼上可能會有問題,請重新縮排過。” http://ihower.tw/rails3/i18n.html 试了半天都是语法错误,但是缩排是一个空格就好用了。

#5 楼 @autumnwolf

# vi locales/models/product/zh-CN.yml
zh-CN:
  activerecord:
    models:
      product: 产品
    attributes:
      product:
        title: 标题

确定 <h1><%= 18n.t('product.title') %></h1>可用?我只用过 <h1><%= I18n.t('product.title') %></h1>

#7 楼 @swordray 嗯 但是我首行缩 1 个

zh-CN:
 activerecord:
   models:
     product: 产品
    attributes:
     product:
       title: 标题

是好用的。用两个会有错误不知道和版本是不是有关。

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