平台: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
分享到:
相关推荐
例如,`t('hello.world')`将查找并返回对应的翻译。如果找不到匹配的翻译,Rails会返回键本身作为默认值。 4. **动态翻译**:Rails还支持动态插入变量到翻译字符串中,如`t('user.welcome', name: 'John')`,这将在...
5. **动态切换语言**:Rails允许用户通过设置`I18n.locale`来切换语言,通常在会话中存储用户的语言首选项。 6. **时间、日期和数字的本地化**:Rails的i18n库还支持日期、时间、数字和其他格式的本地化。 总结来...
- **为Rails I18n作贡献**:鼓励社区成员为Rails的国际化功能贡献力量。 - **资源**:列出相关的资源链接。 - **作者**:列出指南的作者和贡献者。 #### ActionMailer基础 - **简介**:介绍ActionMailer模块的作用...
在Rails应用中,FastGettext可以很好地与`ActionView::Helpers::TranslationHelper`配合工作,无缝替换默认的I18n库,提高性能。 9. **自定义行为** FastGettext允许开发者自定义一些行为,比如改变默认的目录...
**8.2 国际化HelloWorld** 通过资源文件实现简单的国际化消息展示。 **示例代码**: ```java public String execute() { String message = getText("welcome.message"); return SUCCESS; } ``` **8.3 资源文件...