`
Hooopo
  • 浏览: 341550 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

I18n for Rails之hello world

阅读更多
平台:rails 2.3.2 ruby 1.8.6
1.
rails i18n -d mysql

2.
rake db:create

3.
ruby script/generate controller Home index

4.在浏览器http://localhost:3000/ ,基本坏境搭建完成,下面折腾I18n
5.编辑application.rb,加上这句:

before_filter :set_locale
def set_locale
  # if params[:locale] is nil then I18n.default_locale will be used
  I18n.locale = params[:locale]
end

6.views/home/index.html.erb:
<h1><%= t(:hello) %></h1>
<p><%= flash[:notice] %></p>




7.home_controller.rb
class HomeController < ApplicationController
  def index
    flash[:notice] = t(:hello)
  end

end



8.environment.rb
 config.i18n.default_locale = :cn


9.config/locales/en.yml
en:
  hello: "Hello world"


10.config/locales/cn.yml
cn:
  hello: "你好!"




参考:http://guides.rubyonrails.org/i18n.html
http://rails-i18n.org/wiki/wikipages/i18n-rails-guide
http://guides.rubyonrails.org/testing.html
http://www.iteye.com/topic/408107
  • 大小: 22.3 KB
  • 大小: 21 KB
0
0
分享到:
评论
1 楼 jinleileiking 2009-08-03  
消灭0回复!怎么把你的文章搞到圈子里。。?

相关推荐

    rails国际化

    例如,`t('hello.world')`将查找并返回对应的翻译。如果找不到匹配的翻译,Rails会返回键本身作为默认值。 4. **动态翻译**:Rails还支持动态插入变量到翻译字符串中,如`t('user.welcome', name: 'John')`,这将在...

    Struts2和Rails的国际化实现

    5. **动态切换语言**:Rails允许用户通过设置`I18n.locale`来切换语言,通常在会话中存储用户的语言首选项。 6. **时间、日期和数字的本地化**:Rails的i18n库还支持日期、时间、数字和其他格式的本地化。 总结来...

    Ruby on Rails 指南 v5.0.1 中文版

    - **为Rails I18n作贡献**:鼓励社区成员为Rails的国际化功能贡献力量。 - **资源**:列出相关的资源链接。 - **作者**:列出指南的作者和贡献者。 #### ActionMailer基础 - **简介**:介绍ActionMailer模块的作用...

    Ruby-FastGettextRuby国际化工具

    在Rails应用中,FastGettext可以很好地与`ActionView::Helpers::TranslationHelper`配合工作,无缝替换默认的I18n库,提高性能。 9. **自定义行为** FastGettext允许开发者自定义一些行为,比如改变默认的目录...

    Struts2.0框架技术详解

    **8.2 国际化HelloWorld** 通过资源文件实现简单的国际化消息展示。 **示例代码**: ```java public String execute() { String message = getText("welcome.message"); return SUCCESS; } ``` **8.3 资源文件...

Global site tag (gtag.js) - Google Analytics