很早就知道国际化,就知道i18n,却不知道是什么原因。原来internationalization(国际化),这个单词的长度是20,然后取其首尾字母,中间省略的字母刚好18个。
选用了Locale作为国际化的解决方案。
1、首先是安装
gem install locale_rails
会自动的安装locale和locale_rails两个gem。
2、生成i18n的配置文件
# in config/initializer/locale.rb
# Tell the I18n library where to find your translations
I18n.load_path += Dir[ File.join(RAILS_ROOT, 'lib', 'locale', '*.{rb,yml}') ]
# Tell the supported locales. This is the one of additional setting by Ruby-Locale for Ruby on Rails.
# If supported_locales is not set, the locale information which is given by WWW browser is used.
# This setting is required if your application wants to restrict the locales.
I18n.supported_locales = Dir[ File.join(RAILS_ROOT, 'lib', 'locale', '*.{rb,yml}') ].collect{|v| File.basename(v, ".*")}.uniq
# Tell the default locale. If this value is not set, "en" is set.
# With this library, this value is used as the lowest priority locale
# (If other locale candidates are not found, this value is used).
I18n.default_locale = "en-US"
3、gem的引用
# config/environment.rb
Rails::Initializer.run do |config|
:
:
config.gem 'locale'
config.gem 'locale_rails'
end
app/controllers/application.rb中不需要定义set_locale。
4、基本知识
I18n.translate "hello"
I18n.localize Time.now
# 简写
I18n.t "hello"
I18n.l Time.now
多国语文件默认的放在config/localese文件夹下,假设你要支持中文和英语,那么你需要在这个文件夹下放置en.yml和zh.yml。
# zh-CN.yml
"zh-CN":
submit: '提交'
create: '创建'
#en.yml
en:
submit: 'Submit'
create: 'Create'
试图中更加简单,你可以直接调用t方法:
<%= t 'submit' %>
5、使用
你可以进入Console进行测试:
引用
> I18n.t 'submit'
=> "Submit"
> I18n.locale = 'zh'
=> "zh"
> I18n.t('submit')
=> "提交"
6、传递变量
有些时候,我们的字符串中可能需要包含变量,只需要将其放在两个大括号内就可以了:
# zh-CN.yml
"zh-CN":
hello: "你好, {{name}}"
打开console:
引用
> I18n.t 'hello', :name => 'Rails'
=> "你好,Rails!"
还可以进行单复数处理、时间和日期本地化、货币处理、ActiveRecord和route处理。
解决问题
1、undefined method set_app_language_tags错误
引用
c:/ruby/lib/ruby/gems/1.8/gems/locale_rails-2.0.5/lib/locale_rails/i18n.rb:28:in `supported_locales=': undefined method `set_app_language_tags' for I18n::Locale:Module (NoMethodError)
方法:http://github.com/mutoh/locale_rails/issues/#issue/2
2、translation_missing错误
千万要记住日文是ja,简体中文是zh-CN,为了这个郁闷了好久
参考:
http://guides.rubyonrails.org/i18n.html
http://github.com/svenfuchs/rails-i18n (多国语文件)
http://www.yotabanana.com/hiki/ruby-locale-rails-howto.html
http://www.letrails.cn/archives/rails-2-2-i18n-tutorials/
http://d.hatena.ne.jp/willnet/20100430/1272618929
分享到:
相关推荐
Ruby国际化和本地化(i18n)解决方案。 目前由@radar维护。 用法 滑轨 您将最常在Rails应用程序中使用此库。 用法的示例, 。 Ruby(无Rails) 如果要在没有Rails的情况下使用此库,则只需将i18n添加到Gemfile ...
有关e Ruby I18n的Ruby国际化和本地化(i18n)解决方案,请参见《 Rails指南》。 目前由@radar维护。 使用Rails最常见的是在Rails应用程序中使用此库。 有关用法的示例,请参见《 Rails指南》。 Ruby(不带Rails)...
I18nLinter ...只需在Ruby on Rails项目的文件夹中输入i18n_linter ,然后观察可以国际化的字符串即可。 注意:仅报告ruby文件中的字符串。 $ cd my/ruby_on_rails/project $ i18n_linter [options]
devise-i18n, 设计 gem的翻译 devise-i18n 设计"是一种基于warden的Rails 灵活... 国际化( aka i18n ) 是一个"计算机软件适应不同语言。区域差异和目标市场技术要求的方法"。在控制器。模型和其他领域中支持 i18n,但它
该软件包旨在帮助您创建Rails国际化密钥。 Sublime Text 2版本不再使用新功能进行更新; 您仍然可以从Package Control下载它,也可以在找到它。 可用命令 1.检查按键 如果要检查*.yml文件中存在哪些键,则可以打开...
I18n:Bamboo monkey 修补 Rails I18n 模块,并将强制所有对 I18n.translate (I18n.t) 和 I18n.localize (I18n.l) 的调用返回所有可用语言环境中最长的翻译或本地化值。 出于显而易见的原因(猴子补丁 :anxious_face...
devise-i18n-views确实增加了对视图国际化的支持,并提供了翻译。 先决条件 在没有情况下使用devise-i18n-views是没有意义的,因此请认为这是先决条件。 devise-i18n-views包含与Devise v2及更高版本兼容的视图。 ...
狂欢国际化 这是的国际化项目有关更多详细信息,请参见。 翻译愉快! 使用以下命令将此扩展名添加到您的Gemfile中:狂欢> = 3.1 gem 'spree_i18n' , github : 'spree-contrib/spree_i18n'Spree 3.0和Spree 2.x gem '...
国际化(aka i18n)是“使计算机软件适应目标市场的不同语言,地区差异和技术要求的手段”。 Devise在控制器,模型和其他区域中支持i18n,但不支持国际化视图。 devise-i18n添加了此支持。 Devise还不包括实际翻译...
humanize_boolean本身也支持i18n转换,因此可以在国际化的rails应用程序中使用。 i18n键是locale.boolean.yes和locale.boolean.no 安装 将此行添加到您的应用程序的Gemfile中: gem 'humanize_boolean' 然后执行...
I18n.t()函数调用,其格式为Ruby on Rails的i18n gem。 其他类型的函数调用很容易支持。 如果您希望获得对其他策略的支持,请提出问题或提出请求。 与玫瑰花结服务器一起使用 假设您正在配置的实例。 添加I18n.t()...
该项目移至
Globalize构建在I18n API in Ruby on Rails之上添加模型翻译至ActiveRecord模型
该库不使用gettext-至少可以说,它很麻烦-而是可以处理YAML文件中的翻译,这些翻译与Rails国际化系统所使用的文件兼容,因此您可以使用任何已经与兼容的第三方服务它们(例如 )。 它由和库提供支持,并通过Python...
brcep(我们建议使用 gem) 数据布丁涅罗brhelper brnumeros(我们建议使用 gem) string brcpfcnpj(我们建议使用 gem brI18n 注意:由于增加了Rails i18n支持,因此从巴西Rails中删除了brtraducao宝
它的语法类似于i18n,但是它是为复制更新而不是国际化而构建的。 它基于将内容移出代码库的想法而构建。安装1.将gem添加到您的Gemfile中: gem 'editmode' 并运行bundle install 。2.使用您的project_id创建一个...
基于Spring的MessageSource核心概念,提供了对国际化(i18n)的支持 基于Spring事务抽象概念,实现事务服务层 借助于功能强大的Groovy动态语言和领域特定语言(Domain Specific Language,DSL),以上那些特性变得...
基于Spring的MessageSource核心概念,提供了对国际化(i18n)的支持 基于Spring事务抽象概念,实现事务服务层 借助于功能强大的Groovy动态语言和领域特定语言(Domain Specific Language,DSL),以上那些特性变...
基于Spring的MessageSource核心概念,提供了对国际化(i18n)的支持 基于Spring事务抽象概念,实现事务服务层 借助于功能强大的Groovy动态语言和领域特定语言(Domain Specific Language,DSL),以上那些特性变得...