- 浏览: 2076068 次
- 性别:
- 来自: NYC
文章分类
- 全部博客 (628)
- Linux (53)
- RubyOnRails (294)
- HTML (8)
- 手册指南 (5)
- Mysql (14)
- PHP (3)
- Rails 汇总 (13)
- 读书 (22)
- plugin 插件介绍与应用 (12)
- Flex (2)
- Ruby技巧 (7)
- Gem包介绍 (1)
- javascript Jquery ext prototype (21)
- IT生活 (6)
- 小工具 (4)
- PHP 部署 drupal (1)
- javascript Jquery sort plugin 插件 (2)
- iphone siri ios (1)
- Ruby On Rails (106)
- 编程概念 (1)
- Unit Test (4)
- Ruby 1.9 (24)
- rake (1)
- Postgresql (6)
- ruby (5)
- respond_to? (1)
- method_missing (1)
- git (8)
- Rspec (1)
- ios (1)
- jquery (1)
- Sinatra (1)
最新评论
-
dadadada2x:
user模型里加上 protected def email ...
流行的权限管理 gem devise的定制 -
Sev7en_jun:
shrekting 写道var pattern = /^(0| ...
强悍的ip格式 正则表达式验证 -
jiasanshou:
好文章!!!
RPM包rpmbuild SPEC文件深度说明 -
寻得乐中乐:
link_to其实就是个a标签,使用css控制,添加一个参数: ...
Rails在link_to中加参数 -
aiafei0001:
完全看不懂,不知所然.能表达清楚一点?
"$ is not defined" 的问题怎么办
Localizing your Rails 2.2 Application
Ok, so rant over, let’s see how you do it:
1. Preparation
First thing you want to do is set what your default language will be. In environment.rb set the flag:
You’re going to see i18n a lot - it stands for Internationalization (I -> nationalization (18 characters) -> n)
In Rails 2.2 there’s a new directory for handling the different locales: /config/locales. In here you put YAML files pertaining to each locale you have translated. The naming convention of each of these files follows the locale name (e.g: en.yml, it.yml etc).
2. Translate your application into your default language
Next (oddly enough) you’ll need to translate your entire application into English (or whatever language your app is already). This means finding all the places where you have fragments of English and replacing it with something that looks like this:
The t is shorthand for I18n.translate - a new method that handles translation. It takes as a parameter a string refering to a key in each of your locale yaml files. The yaml file can have nested keys, so, main.title finds the title key belonging to the main key in the yaml file. e.g.
You can use string interpolation as well. So if you want to translate something that looks like this:
You instead use the t method and pass along the variable you want to use:
And refer to this variable within your locale yaml file with double braces like this:
Ok, after you’ve translated your application into English (which is the most boring part of the process) you’re ready to translate it into other languages!
3. Translate your application into other languages
Your newly translated yaml file should hopefully look something like this (this is a copy of one from whatiwantforxmas.com:
The next step is sending it to your translator to…erm…translate it! Here’s what the above looks like in Italian:
Put this into the locales/config folder with a name that corresponds to the correct locale, and you’re on your way to a translated site!
4. Add ActiveRecord, Currency, Date and Time translations
There’s more to translation then just the phrases and text on your pages. There’s also:
This is the easiest bit to implement, thanks to Sven Fuchs and a bunch of translators. Look at the code at http://github.com/svenfuchs/rails-i18n/tree/master/rails/locale and you’ll find sample yaml files that contain all this stuff for tonnes of languages (including Australian, strewth!). Simply copy this code into your own locale files.
5. Provide a way to switch between different locales
So, now you’ll need to add the ability to switch locales.
The important magic variable that sets this is: I18n.locale. You want to set this to the locale string that your yaml file is named after. For instance:
For my application I added a before_filter in application.rb that sets the locale depending on:
* whether the user is logged in and has a locale
* whether the locale is stored in the session
* failing that, use the default_locale – although this step could be unnecessary, but makes me feel comfortable.
I also had a locales controller that allowed for an easy method to switch locales: I also included a locales controller for switching locality.
The beauty of this is that you can send links that represent different translations of your application:
6. Add tests to ensure your translation works
If a key is missing from a translation then rather irritatingly you’ll be greeted with something like this:
This looks pretty awful, so you’ll want to make sure that this doesn’t happen for each of your pages.
Here’s some sample functional test code for this:
This is a bit of a painfully slow way to test this though. (If someone can come up with a better way of testing this let me know!)
Localizing Tips
Localizing your app can be a frustrating experience. Here’s some tips to ease the pain:
Repeat yourself
Resist the urge to DRY up your yaml snippets and have them used in several different locations. Different languages could translate your snippets differently depending on context.
Provide context for your translators
Your translators need to know a little bit about the context that the text represents. Write comments in your yaml file to make it easier for them.
Avoid the pluralize method
Right now, you can’t trust the pluralize helper method if you’re using localization. (i.e. it tried to say that the plural of giorno (day) is giornos (it’s actually giorni) Instead, provide keys in your translation yaml files asking for the plural and singular words, and write a method for dealing with the plural.
Adopt some sort of convention in your yaml files for links that are embedded in other text
For example, if you wanted to write
Sign up if you want to join yoursite.com
I would separate this out in the yaml file like this:
Just because the “Sign up” appears at the front of the sentence in English, doesn’t make it so in another language.
What’s this Yaml thing?
Ideally you’ll want to get your translators to translate the yaml file. Beware though that yaml is a strange file format to non-technical people. Let your translators know here do get a file editor that they can use (SciTE for example. Grab it at http://prdownloads.sourceforge.net/scintilla/Sc177.exe)
But is it REALLY right
After getting the translation, walk through the website with the translator to make sure it’s REALLY right. Sometimes things translate differently when the context changes. I also noticed that some special characters sometimes got mixed up, so this is a good opportunity to spot those.
Further reading
* As usual, Ryan Bates is on the scene with an excellent RailsCast: http://railscasts.com/episodes/138-i18n
* “Can’t read, won’t buy - Why Language Matters on Global Websites”: http://www.lionbridge.com/lionbridge/en-us/kc/globalization/cant_read_wont_buy.htm
* Locale examples (where you can find sample yaml files for tonnes of languages): http://github.com/svenfuchs/rails-i18n/tree/master/rails/locale
http://advent2008.hackruby.com/past/2008/12/16/rails_22_internationalization.html
Ok, so rant over, let’s see how you do it:
1. Preparation
First thing you want to do is set what your default language will be. In environment.rb set the flag:
config.i18n.default_locale = :en
You’re going to see i18n a lot - it stands for Internationalization (I -> nationalization (18 characters) -> n)
In Rails 2.2 there’s a new directory for handling the different locales: /config/locales. In here you put YAML files pertaining to each locale you have translated. The naming convention of each of these files follows the locale name (e.g: en.yml, it.yml etc).
2. Translate your application into your default language
Next (oddly enough) you’ll need to translate your entire application into English (or whatever language your app is already). This means finding all the places where you have fragments of English and replacing it with something that looks like this:
t("snippet_name")
The t is shorthand for I18n.translate - a new method that handles translation. It takes as a parameter a string refering to a key in each of your locale yaml files. The yaml file can have nested keys, so, main.title finds the title key belonging to the main key in the yaml file. e.g.
main: title: "Some title"
You can use string interpolation as well. So if you want to translate something that looks like this:
<%= "Welcome to the site #{user.name}!" %>
You instead use the t method and pass along the variable you want to use:
t("title", :name => user.name)
And refer to this variable within your locale yaml file with double braces like this:
title: "Welcome to the site !"
Ok, after you’ve translated your application into English (which is the most boring part of the process) you’re ready to translate it into other languages!
3. Translate your application into other languages
Your newly translated yaml file should hopefully look something like this (this is a copy of one from whatiwantforxmas.com:
en: main_title: "What I want for Xmas" countdown: merry_xmas: "Merry Christmas!" days_until_xmas: "Only until Xmas" days: "days" day: "day" # snipped!
The next step is sending it to your translator to…erm…translate it! Here’s what the above looks like in Italian:
Put this into the locales/config folder with a name that corresponds to the correct locale, and you’re on your way to a translated site!
4. Add ActiveRecord, Currency, Date and Time translations
There’s more to translation then just the phrases and text on your pages. There’s also:
- time differences
- date differences
- currency differences
- all the text that comes with ActiveRecord validations
This is the easiest bit to implement, thanks to Sven Fuchs and a bunch of translators. Look at the code at http://github.com/svenfuchs/rails-i18n/tree/master/rails/locale and you’ll find sample yaml files that contain all this stuff for tonnes of languages (including Australian, strewth!). Simply copy this code into your own locale files.
5. Provide a way to switch between different locales
So, now you’ll need to add the ability to switch locales.
The important magic variable that sets this is: I18n.locale. You want to set this to the locale string that your yaml file is named after. For instance:
I18n.locale = :en # English! I18n.locale = :it # Italian!
For my application I added a before_filter in application.rb that sets the locale depending on:
* whether the user is logged in and has a locale
* whether the locale is stored in the session
* failing that, use the default_locale – although this step could be unnecessary, but makes me feel comfortable.
# in Application.rb before_filter :set_locale def set_locale I18n.locale = (current_user.locale if current_user) || session[:locale] || I18n.default_locale end
I also had a locales controller that allowed for an easy method to switch locales: I also included a locales controller for switching locality.
# locales_controller.rb class LocalesController < ApplicationController def show if current_user # if I'm logged in current_user.locale = params[:locale] # change my locality current_user.save end session[:locale] = params[:locale] redirect_to :back end end # routes.rb map.locales 'locales/:locale', :controller => 'locales', :action => 'show'
The beauty of this is that you can send links that represent different translations of your application:
www.yoursite.com/locales/it #=> Italian version of the site www.yoursite.com/locales/en #=> English version of the site
6. Add tests to ensure your translation works
If a key is missing from a translation then rather irritatingly you’ll be greeted with something like this:
<span class="translation_missing">en, missing_key</span>
This looks pretty awful, so you’ll want to make sure that this doesn’t happen for each of your pages.
Here’s some sample functional test code for this:
['it', 'en', 'de'].each do |locale| define_method "test_no_missing_translation_for_locale__#{locale}__" do @request.session[:locale] = locale.to_s get :index assert_select "span.translation_missing", false end end
This is a bit of a painfully slow way to test this though. (If someone can come up with a better way of testing this let me know!)
Localizing Tips
Localizing your app can be a frustrating experience. Here’s some tips to ease the pain:
Repeat yourself
Resist the urge to DRY up your yaml snippets and have them used in several different locations. Different languages could translate your snippets differently depending on context.
Provide context for your translators
Your translators need to know a little bit about the context that the text represents. Write comments in your yaml file to make it easier for them.
Avoid the pluralize method
Right now, you can’t trust the pluralize helper method if you’re using localization. (i.e. it tried to say that the plural of giorno (day) is giornos (it’s actually giorni) Instead, provide keys in your translation yaml files asking for the plural and singular words, and write a method for dealing with the plural.
Adopt some sort of convention in your yaml files for links that are embedded in other text
For example, if you wanted to write
Sign up if you want to join yoursite.com
I would separate this out in the yaml file like this:
sign_up: " if you want to join yoursite.com" sign_up_link: "Sign up" # this is the text used in the 'link' variable above
Just because the “Sign up” appears at the front of the sentence in English, doesn’t make it so in another language.
What’s this Yaml thing?
Ideally you’ll want to get your translators to translate the yaml file. Beware though that yaml is a strange file format to non-technical people. Let your translators know here do get a file editor that they can use (SciTE for example. Grab it at http://prdownloads.sourceforge.net/scintilla/Sc177.exe)
But is it REALLY right
After getting the translation, walk through the website with the translator to make sure it’s REALLY right. Sometimes things translate differently when the context changes. I also noticed that some special characters sometimes got mixed up, so this is a good opportunity to spot those.
Further reading
* As usual, Ryan Bates is on the scene with an excellent RailsCast: http://railscasts.com/episodes/138-i18n
* “Can’t read, won’t buy - Why Language Matters on Global Websites”: http://www.lionbridge.com/lionbridge/en-us/kc/globalization/cant_read_wont_buy.htm
* Locale examples (where you can find sample yaml files for tonnes of languages): http://github.com/svenfuchs/rails-i18n/tree/master/rails/locale
http://advent2008.hackruby.com/past/2008/12/16/rails_22_internationalization.html
发表评论
-
Destroying a Postgres DB on Heroku
2013-04-24 10:58 935heroku pg:reset DATABASE -
VIM ctags setup ack
2012-04-17 22:13 3259reference ctags --extra=+f --e ... -
alias_method_chain方法在3.1以后的替代使用方式
2012-02-04 02:14 3295alias_method_chain() 是rails里的一个 ... -
一些快速解决的问题
2012-01-19 12:35 1472问题如下: 引用Could not open library ... -
API service 安全问题
2011-12-04 08:47 1386这是一个长期关注的课题 rest api Service的 ... -
Module方法调用好不好
2011-11-20 01:58 1349以前说,用module给class加singleton方法,和 ... -
一个ajax和rails交互的例子
2011-11-19 01:53 1908首先,这里用了一个,query信息解析的包,如下 https: ... -
Rails 返回hash给javascript
2011-11-19 01:43 2277这是一个特别的,不太正统的需求, 因为,大部分时候,ajax的 ... -
关于Rubymine
2011-11-18 23:21 2267开个帖子收集有关使用上的问题 前一段时间,看到半价就买了。想 ... -
ruby中和javascript中,动态方法的创建
2011-11-18 21:01 1241class Klass def hello(*args) ... -
textmate快捷键 汇总
2011-11-16 07:20 8147TextMate 列编辑模式 按住 Alt 键,用鼠标选择要 ... -
Ruby面试系列六,面试继续面试
2011-11-15 05:55 2025刚才受到打击了,充分报漏了自己基础不扎实,不肯向虎炮等兄弟学习 ... -
说说sharding
2011-11-13 00:53 1492这个东西一面试就有人 ... -
rails面试碎碎念
2011-11-12 23:51 1946面试继续面试 又有问ru ... -
最通常的git push reject 和non-fast forward是因为
2011-11-12 23:29 17216git push To git@github.com:use ... -
Rails 自身的many to many关系 self has_many
2011-11-12 01:43 2738简单点的 #注意外键在person上people: id ... -
Rails 3下的 in place editor edit in place
2011-11-12 01:20 946第一个版本 http://code.google.com/p ... -
Heroku 的诡异问题集合
2011-11-11 07:22 1697开个Post记录,在用heroku过程中的一些诡异问题和要注意 ... -
SCSS 和 SASS 和 HAML 和CoffeeScript
2011-11-07 07:52 12960Asset Pipeline 提供了内建 ... -
Invalid gemspec because of the date format in specification
2011-11-07 02:14 2122又是这个date format的错误。 上次出错忘了,记录下 ...
相关推荐
rails-i18n, 用于收集 Ruby on Rails i18n 环境数据以及其他有趣的Rails 相关 i18n 内容的存储库 Rails 语言环境数据存储库 中心收集区域设置数据,以便在 ruby 上使用。 gem-安装添加到你的Gemfile:gem 'rails-i18n...
适用于JavaScript的Ruby on Rails i18n 该gem通过Rack中间件公开您的JSON序列化翻译,以便将它们与JavaScript结合使用。 安装 此宝石正在开发中,这些步骤可能会更改 # Gemfile gem 'rails-i18n-js', :git => '...
支持以下框架样式: 文字导轨-i18n安装将此行添加到您的应用程序的Gemfile中: gem 'rubocop-i18n' 然后执行: $ bundle或将其自己安装为: $ gem install rubocop-i18n用法在您的rubocop.yml : require: - ...
react-i18n 该模块与 gem集成在一起,并作为的包装器构建。 基本设定 安装模块 $ npm install --save i18n-js react-i18n 设置i18n-js 在Gemfile中 gem 'i18n-js' 您认为* .haml :javascript I18n.default...
用法const I18n = require ( '@fiverr/i18n' ) ;const translations = require ( './translations.json' ) ;const i18n = new I18n ( { translations } ) ; 选项类型描述translations 目的翻译结构的表示形式必须...
mail_form, 在 Rails 中使用 i18n 验证附件和请求信息,直接从表单发送电子邮件 MailForm Rails 3这个 gem 构建在 ActiveModel 之上,展示如何从 Rails 中提取验证。命名和 i18n,而不需要自己实现。本自述文件指的...
I18nRouting I18n_routing是Ruby on Rails的插件,可让您轻松地通过版本2.2以后的Rails中包含的I18n api转换路线。 所有必需的信息都可以在Wiki上找到: 如有疑问,请使用i18_routing谷歌论坛: 适用于Rails 2.3、...
安装 添加到您的Gemfile中: gem 'i18n-timezones'用法 时区将自动转换为当前语言环境。 甚至:default都将转换为当前语言环境。 time_zone_select :user , :time_zone , ActiveSupport :: TimeZone . us_
Rails 2.2引入了一个强大的国际化系统(i18n)。这使得开发者能够轻松地为应用程序添加多语言支持,从而满足全球用户的需求。i18n支持允许开发者轻松创建多语言界面,无需编写额外的复杂代码即可实现动态切换语言环境...
百济I18n 这是一个小库,可以在JavaScript上提供Rails I18n的翻译。 从借来的特征: 多元化日期/时间本地化号码本地化语言环境回退资产管道支持还有更多! :)用法安装通过NPM npm install baiji-i18n 运行npm ...
I18nLinter ...只需在Ruby on Rails项目的文件夹中输入i18n_linter ,然后观察可以国际化的字符串即可。 注意:仅报告ruby文件中的字符串。 $ cd my/ruby_on_rails/project $ i18n_linter [options]
您可以使用Gitter与我们聊天:Globalize建立在Ruby on Rails的I18n API上,以将模型转换添加到ActiveRecord模型。 换句话说,一种翻译实际用户生成内容的方法;例如, 您可以使用Gitter与我们聊天:Globalize建立在...
安装 gem install i18n_tools用法如果您在 Rails 中使用 bundler,请将以下内容添加到您的Gemfile : group :development do gem 'i18n_tools'end 如果您在 Rails 之外使用 bundler,请使用 gem 'i18n_tools' , :...
I18n:Bamboo monkey 修补 Rails I18n 模块,并将强制所有对 I18n.translate (I18n.t) 和 I18n.localize (I18n.l) 的调用返回所有可用语言环境中最长的翻译或本地化值。 出于显而易见的原因(猴子补丁 :anxious_face...
MailForm库是专门为简化这一过程而设计的,它允许开发者直接从Rails的表单中发送邮件,并且提供了I18n(国际化)支持、验证功能以及添加附件和请求信息的能力。这个库由Plataformatec开发,其最新版本为bd43996。 ...
在Rails应用中,Ruby-i18n被深度集成,提供了更丰富的功能,如视图辅助方法、路由本地化等。 10. **其他扩展和工具** Ruby-i18n社区还提供了许多扩展和工具,如`i18n-tasks`用于管理翻译任务,`i18n-spec`用于...
devise-i18n, 设计 gem的翻译 devise-i18n 设计"是一种基于warden的Rails 灵活认证方案"。 国际化( aka i18n ) 是一个"计算机软件适应不同语言。区域差异和目标市场技术要求的方法"。在控制器。模型和其他领域中支持...
vim-localorie Localorie是一个Vim插件,可以更轻松地使用Rails i18n语言环境文件。查找翻译将光标放在Rails视图或控制器中的i18n键上,调用localorie#translate()用该键的所有翻译内容填充localorie#translate()或...
如果要在没有Rails的情况下使用此库,则只需将i18n添加到Gemfile : gem 'i18n' 然后使用一些翻译和默认语言环境配置I18n: I18n . load_path << Dir [ File . expand_path ( "config/locales" ) + "/*....
i18n-tasks可以与使用ruby 任何项目一起使用(Rails中的默认设置)。 将i18n任务添加到Gemfile中: gem 'i18n-tasks' , '~> 0.9.33' 复制默认: $ cp $( i18n-tasks gem-path ) /templates/config/i18n-tasks....