---
title: "rails2.2的i18n真是无爹妈的孩子啊"
author: "crazycode"
date: "2008-11-27T15:34:01Z"
tags: []
slug: "rails2-2-i18n"
draft: false
lastmod: "2008-11-27T15:48:58Z"
id: 280917
url: "https://crazycode.iteye.com/blog/280917"
---

今天试着升级到rails2.2，按http://blog.xdite.net/?p=785上的说明，拉了一个demo app下来，把里面的locals文件复制到项目目录:
git clone git://github.com/clemens/i18n_demo_app.git
cp config/locals ~/myapp/config -r

设置为使用zh-CN后，出错信息死活没办法出现中文，后来看文档，有这样一句：
Then ActiveRecord will look for messages in this order:
activerecord.errors.models.admin.attributes.title.blank
activerecord.errors.models.admin.blank
activerecord.errors.models.user.attributes.title.blank
activerecord.errors.models.user.blank
activerecord.errors.messages.blank

而在zh-CN.yml或zh-CN.rb文件中，这个message的路径是在
active_record.error_messages
估计这个demo写的比较早。

修改为activerecord.errors.messages后正常了。

看了一下rails最新源代码，在/activerecord/lib/active_record/locale/en.yml中，也是activerecord.errors.messages，之前比较

看来i18n的API变化后没人去统一一下，汗啊。

================
后来发现更好的解决方法，同时推荐一个插件i18n_generators：
   http://github.com/amatsuda/i18n_generators/tree/master
可以直接生成zh-CN的本地化文件。
安装这个plugin需要先sudo gem install gettext。

安装完成插件后，使用generate：
  script/generate i18n zh-CN
就会在config/locales目录生成zh-CN.yml和models_zh-CN.yml，其中models_zh-CN.yml中有所有model的字段，而且。。。已经翻译好了，当然需要手工再调整一下。