- 浏览: 2089399 次
- 性别:
- 来自: 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 956heroku pg:reset DATABASE -
VIM ctags setup ack
2012-04-17 22:13 3283reference ctags --extra=+f --e ... -
alias_method_chain方法在3.1以后的替代使用方式
2012-02-04 02:14 3328alias_method_chain() 是rails里的一个 ... -
一些快速解决的问题
2012-01-19 12:35 1492问题如下: 引用Could not open library ... -
API service 安全问题
2011-12-04 08:47 1405这是一个长期关注的课题 rest api Service的 ... -
Module方法调用好不好
2011-11-20 01:58 1379以前说,用module给class加singleton方法,和 ... -
一个ajax和rails交互的例子
2011-11-19 01:53 1933首先,这里用了一个,query信息解析的包,如下 https: ... -
Rails 返回hash给javascript
2011-11-19 01:43 2297这是一个特别的,不太正统的需求, 因为,大部分时候,ajax的 ... -
关于Rubymine
2011-11-18 23:21 2299开个帖子收集有关使用上的问题 前一段时间,看到半价就买了。想 ... -
ruby中和javascript中,动态方法的创建
2011-11-18 21:01 1275class Klass def hello(*args) ... -
textmate快捷键 汇总
2011-11-16 07:20 8180TextMate 列编辑模式 按住 Alt 键,用鼠标选择要 ... -
Ruby面试系列六,面试继续面试
2011-11-15 05:55 2055刚才受到打击了,充分报漏了自己基础不扎实,不肯向虎炮等兄弟学习 ... -
说说sharding
2011-11-13 00:53 1531这个东西一面试就有人 ... -
rails面试碎碎念
2011-11-12 23:51 1977面试继续面试 又有问ru ... -
最通常的git push reject 和non-fast forward是因为
2011-11-12 23:29 17283git push To git@github.com:use ... -
Rails 自身的many to many关系 self has_many
2011-11-12 01:43 2766简单点的 #注意外键在person上people: id ... -
Rails 3下的 in place editor edit in place
2011-11-12 01:20 969第一个版本 http://code.google.com/p ... -
Heroku 的诡异问题集合
2011-11-11 07:22 1722开个Post记录,在用heroku过程中的一些诡异问题和要注意 ... -
SCSS 和 SASS 和 HAML 和CoffeeScript
2011-11-07 07:52 12990Asset Pipeline 提供了内建 ... -
Invalid gemspec because of the date format in specification
2011-11-07 02:14 2166又是这个date format的错误。 上次出错忘了,记录下 ...
相关推荐
I18nRouting I18n_routing是Ruby on Rails的插件,可让您轻松地通过版本2.2以后的Rails中包含的I18n api转换路线。 所有必需的信息都可以在Wiki上找到: 如有疑问,请使用i18_routing谷歌论坛: 适用于Rails 2.3、...
Rails 2.2引入了一个强大的国际化系统(i18n)。这使得开发者能够轻松地为应用程序添加多语言支持,从而满足全球用户的需求。i18n支持允许开发者轻松创建多语言界面,无需编写额外的复杂代码即可实现动态切换语言环境...
Rails 2.2版本添加了对多国语言的支持,引入了国际化系统(i18n),允许开发者创建具备国际化特性的应用程序。这使得Rails应用能够更加容易地支持多种语言,适应国际化的需求。 #### Rails 线程安全及 Ruby 新版本...
本章节将详细介绍如何利用Rails进行敏捷开发,包括应用的生成、数据库配置、代码生成器的使用以及如何遵循约定规则来简化开发流程。 #### 2. 创建第一个Web应用 ##### 2.1 生成Rails应用及启动应用 - **代码生成器*...
#### 八、国际化(I18N) **8.1 在Struts2.0中国际化您的应用程序** Struts2支持国际化,通过定义资源文件来实现多语言支持。 **示例资源文件**: ```properties # messages_zh_CN.properties welcome.message=...