Rails plugin: i18n
development, plugins, rails, ruby at June 6th, 2007 by 小影
An update from mod-i18n module. My first Ruby on Rails plugin. It is modified from Localization module. It do translation using YML based translation file, and choose language based on URL parameter.
Installation
script/plugin install http://mod-i18n.googlecode.com/svn/trunk/plugins/i18n/
1. Create Language File
Create a directory config/lang, under the directory, create yml files using the langugae name, e.g. config/lang/zh-HK.yml. In the file, it should contains string mapping, for example:
config/lang/zh-HK.yml:
Listing contacts: 聯絡人列表
Show: 顯示
Edit: 修改
Delete: 移除
2. Edit Application String
In the application, replace content String to following format:
Source Code: 'blah' => _('blah') 'testing 5' => _('testing %d', 5)
rhtml: <%= 'blah' %> => <%=_ 'blah' %> <%= 'testing 5' %> => <%= _('testing %d', 5) %>
3. Test the Applciation
Restart application. mod-i18n use post/get parameter 'lang' to determine the language (configurable through the constant PARAMETER_LANG in lib/i18n.rb).
Original URL: http://127.0.0.1/main/index URL for en-US: http://127.0.0.1/main/index?lang=en-US URL for zh-HK: http://127.0.0.1/main/index?lang=zh-HK
4. [OPTIONAL] Using Rails route
You may specify the language in URL. To do so, in config/route.rb, change the route to containing paramter 'lang'.
from:
map.connect ':controller/:action/:id.:format'
map.connect ':controller/:action/:id'
to:
map.connect ':lang/:controller/:action/:id.:format'
map.connect ':lang/:controller/:action/:id'
Original URL: http://127.0.0.1/main/index
URL for en-US: http://127.0.0.1/en-US/main/index
URL for zh-HK: http://127.0.0.1/zh-HK/main/index
相關連結:
- mod i18n project Homepage @ Google Code
- Updated @ 20070629
7 Responses to “Rails plugin: i18n”
By cnruby on Jul 9, 2007 | Reply
你的安装链有问题,应该是
http://mod-i18n.googlecode.com/svn/trunk/plugins/i18n/
By 小影 on Jul 13, 2007 | Reply
謝謝指正!己經修改了
By Anthony on Aug 18, 2007 | Reply
Thanks very much for sharing your I18n solution! I installed the plugin and modified a piece of RHTML to test it, but the plugin does not translate _(”..’) to my desired language. I noticed that the plugin loaded when I restarted the application. I just wonder if there is any extra plugin requried? Or do I need any extra configuration so that when a new http request arrives, the application will automatically call the plugin module. Many thx!
By 小影 on Aug 20, 2007 | Reply
There is no dependency. Just as specify in the example, if you have action /main/index, and you prepared language file of zh-HK, if you enter the URL: http://127.0.0.1/main/index?lang=zh-HK
You shall see the page getting translated. I’ll try to make a example project to demonstrate that… just a while.
By 小影 on Aug 21, 2007 | Reply
Hi Anthony,
Try to check out the example project here: http://mod-i18n.googlecode.com/svn/trunk/i18n-example/
It should give you idea how to use it.
By Deger on May 10, 2008 | Reply
Nice plug-in. Thank you very much.
Now the :lang is passing from the request, how to specify the language from server side. Please help.
By John Peter on Jun 28, 2008 | Reply
hi,
i downloaded the plugin but the language isn’t loaded in all controllers.
i inserted on routes.rb ” map.connect ”, :controller => “demo”, :lang => ‘en-US’” and only demo controller is translated.
how to translate all pages?