`
siuying
  • 浏览: 92510 次
  • 性别: Icon_minigender_1
  • 来自: 香港
最近访客 更多访客>>
社区版块
存档分类
最新评论

Rails plugin: i18n

    博客分类:
  • Tech
阅读更多

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 source http://mod-i18n.googlecode.com/svn/trunk

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
分享到:
评论
2 楼 ddddddddd 2007-11-04  
忘记说谢谢了。谢谢您共享这个插件。帮了我大忙
1 楼 ddddddddd 2007-11-03  
我下载并使用了你的插件,感觉效果很好,不向其他同样功能的那样庞大,够用了

使用过程中发现一个小问题,yml文件分组会出错

error_message:
  text_required:必填项目
  date_invalid: 无效的日期

这种会显示不出来

我把i18n.rb第33行替换成了
    while (translated.is_a?(Hash) && args.length > 0)
      translated = translated[args[0]]
      args.delete_at(0)
    end
    begin
      sprintf translated, *args 
    rescue StandardError => e
      return translated
    end 
  

用<%=_ "error_message", "text_required" %>就能够显示了
(我英文也写了一个yml文件)

相关推荐

    Mojolicious-Plugin-AdvancedMod:附加的功能

    名称 Mojolicious::Plugin::AdvancedMod - Mojolicious 的更多包子 版本 本文档涵盖 2014 年 1 月发布的 ... Rails 的类比:before_filter, after_filter 将请求参数转换为散列和多散列 加载 YAML/JSON 配置,封

    buoys:Ruby on Rails面包屑插件

    我喜欢并尊重它的想法,但是我想使用I18n创建像一样简单的面包屑库。 安装 在您的Gemfile中 gem 'buoys' 并运行: $ bundle install 例子 首先生成配置和示例文件。 $ bin / rails g buoys : install create ...

    2021-2022年收藏的精品资料软件工程师10个jQuery的语言翻译插件推荐.docx

    10. **jsperanto**:基于EJS等模板引擎,支持复数形式、内插和混合查找,提供JSON字典,兼容多种浏览器,且类似Rails的I18N,但无需后端支持。 这些jQuery插件极大地简化了Web开发者的工作,使他们能够快速为网站...

    软件工程师-10个jQuery的语言翻译插件推荐.pdf

    主要特性:复数、插及其混合查找支持、使用 XHR 得到 JSON 字典、JSlinted,QUnited、类似 Rails 的 I18N,但是需要 sans 后台、没有全球化的、支持 IE6+,Firefox3+,Safari 3+, Chrome, Opera9 +、需要 jQuery1.3.2+...

    Jfina 自搭框架

    7. **I18N(国际化)**:Jfina 提供了简单的国际化支持,开发者可以通过配置不同的语言包,实现多语言环境的应用。 8. **工具类(Util)**:Jfina 内置了一些常用的工具类,如DateUtil、StrUtil等,帮助开发者快速...

Global site tag (gtag.js) - Google Analytics