- 论坛徽章:
- 0
|
步骤1:我们要把所有文件D:\works_rails\i18n_gettext\*\*.rb,D:\works_rails\i18n_gettext\*\*.rhtml 需要翻译的文字写成为规定格式为:_('原文')
例如,我们修改文件D:\works_rails\i18n_gettext\app\views\admin\list.rhtml_('Show')
原来的形式为‘Show’
下面的是link中的形式,其中的Chose Student就是要国际化的文字:
'new',:id=>@course.id %>
步骤2:修改文件D:\works_rails\i18n_gettext\Rakefile,增加二个命令updatepo和makemo:
代码如下:
task :makemo do
GetText.create_mofiles(true, "po", "locale")
end
desc "Update pot/po files to match new version."
task :updatepo do
MY_APP_TEXT_DOMAIN = "itu"
MY_APP_VERSION = "itu 1.1.2"
GetText.update_pofiles(MY_APP_TEXT_DOMAIN,
Dir.glob("{app,lib}/**/*.{rb,rhtml}"),
MY_APP_VERSION)
end
步骤3:创建几个需要国际化的目录:
mkdir po\de
mkdir po\en
mkdir po\zh
步骤4: 安装gettext
get install gettext -r
选择(mswin32)的最新版本
步骤5: 在项目目录下运行 rake updatepo ,这里的updatepo 就是你刚才在Rakefile.rb中新增加的命令。运行后会自动找到项目中所有需要国际化的文字,也就是用 _()的括起来的。运行之后会生成一个后缀是pot的文件。
步骤6:用poEdit工具生成po文件。下载连接:
http://puzzle.dl.sourceforge.net/sourceforge/poedit/poedit-1.3.4-setup.exe
步骤6.1: 安装好后,进入菜单 -> 文件 -> 从POT文件更新类目,选择步骤5中产生的pot文件
步骤6.2: 选择好要使用的编码后进入下一步开始翻译。
步骤7:在项目目录下运行rake makemo生成最终项目需要的mo文件。
步骤8:在application.rb中写业务逻辑来实现国际化,如下例:
user=session[:user]
if user.location!=nil
location=Location.find(user.location)
my_lang=location.language
end
cookies["lang"] = my_lang
这里是根据session中的user信息选择语言。这些代码都是写在def cookie_lang(my_lang)这个方法中,before_filter{ |controller|
controller.cookie_lang("")
}
在运行controller前自动通过上面的代码调用。
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/26076/showart_259372.html |
|