Rails 2.2 i18nihower@handlino.com
The history of Rails I18n
Chaos & Painful
we have...
• Ruby Gettext
• DHH’s Localization
• Globalize
• Simple Localization
• Gibberish
Problems
• 一升級 Rails 就爛了
• 乏人維護,缺少支援
• 可恨的 Monkeypatches
Rails i18n project
• 共通的 API
• 可抽換 backend
• 盡量簡單
Rails 2.2
• Powerful I18n API
• Simple Backend
I18n API
簡單、一致
API methods
• I18n.translate :hello
• I18n.t :hello
• t :hello
• I18n.localize Time.now
• I18n.l Time.now
• l Time.now
Features
• Flexible keys
I18n.t :hello
I18n.t "hello"
• Scopes (aka namespaces)
hello: 哈囉
account:
hello: 你好
I18n.t “hello” # 哈囉
I18n.t "account.hello” # 你好
Features (cont.)
• Interpolation
message: "Thanks {{name}}!"
I18n.t :message, :name => "ihower"
# => "Thanks ihower!"
• Pluralization
# en-US
:days => {
:one => "one day"
:other => "{{count}} days"
}
I18n.t :days, :count => 1 # => "one day"
I18n.t :days, :count => 2 # => "2 days"
Backend
100% 可抽換
Simple Backend
• Shipped asYAML files
• You can also use plain Ruby files
Simple Backend
• 使用YAML 或 plain Ruby file
• 預設只有 en-US,請下載
http://github.com/svenfuchs/rails-i18n/
感謝 Tsechingho 提供中文版本
YAML sample# Chinese (Taiwan) translations for Ruby on Rails
# by tsechingho (http://github.com/tsechingho)
"zh-TW":
date:
formats:
default: "%Y-%m-%d"
short: "%b%d日"
long: "%Y年%b%d日"
day_names: [星期天, 星期一, 星期二, 星期三, 星期四, 星期五, 星期六]
abbr_day_names: [日, 一, 二, 三, 四, 五, 六]
month_names: [~, 一月, 二月, 三月, 四月, 五月, 六月, 七月, 八月, 九月, 十月, 十一月, 十二月]
abbr_month_names: [~, 1月, 2月, 3月, 4月, 5月, 6月, 7月, 8月, 9月, 10月, 11月, 12月]
order: [ :year, :month, :day ]
activerecord:
errors:
template:
header:
one: "有 1 個錯誤發生使得「{{model}}」無法被儲存。"
other: "有 {{count}} 個錯誤發生使得「{{model}}」無法被儲存。"
body: "下面欄位有問題:"
messages:
inclusion: "沒有包含在列表中"
exclusion: "是被保留的"
invalid: "是無效的"
confirmation: "不符合確認值"
accepted: "必须是可被接受的"
empty: "不能留空"
blank: "不能是空白字元"
too_long: "過長(最長是 {{count}} 個字)"
too_short: "過短(最短是 {{count}} 個字)"
wrong_length: "字數錯誤(必須是 {{count}} 個字)"
taken: "已經被使用"
not_a_number: "不是數字"
greater_than: "必須大於 {{count}}"
greater_than_or_equal_to: "必須大於或等於 {{count}}"
equal_to: "必須等於 {{count}}"
less_than: "必須小於 {{count}}"
less_than_or_equal_to: "必須小於或等於 {{count}}"
odd: "必須是奇數"
even: "必須是偶數"
Setup i18n
超簡單
environment.rb
# 所有 config/locales/*.rb,yml 將被載入
config.i18n.load_path <<
Dir[File.join(RAILS_ROOT, 'config', 'locales', '*.{rb,yml}')]
config.i18n.default_locale = :en
application.rb
class ApplicationController < ActionController::Base
before_filter :set_locale
def set_locale
session[:locale] = params[:locale] if params[:locale]
I18n.locale = session[:locale] || I18n.default_locale
end
end
view 提供切換連結
• http://localhost:3000/?locale=en
• http://localhost:3000/?locale=ja
• http://localhost:3000/?locale=zh-TW
• ....
可抽換 Backend?
Google translate backend
http://github.com/ihower/i18n_google_translate/
by ihower
environment.rb
I18n.backend = I18n::Backend::GoogleTranslate.new
I18n.default_locale = 'en'
Live demo!

More Related Content

PDF
楽しく学ぶ?API
PDF
Certbotで無料TLSサーバー
PDF
正規表現の先読みについて
PDF
Ruby on Rails 開發環境建置 for Mac
PPTX
Sublime Text 2 Tips & Tricks
PDF
Ruby on Rails : 簡介與入門
PDF
Ruby 程式語言入門導覽
PDF
Ruby 程式語言簡介
楽しく学ぶ?API
Certbotで無料TLSサーバー
正規表現の先読みについて
Ruby on Rails 開發環境建置 for Mac
Sublime Text 2 Tips & Tricks
Ruby on Rails : 簡介與入門
Ruby 程式語言入門導覽
Ruby 程式語言簡介

Viewers also liked (11)

PDF
nadoka さんの m17n 対応のベストプラクティス
PDF
hubot-slack v4移行時のハマりどころ #hubot_chatops
PDF
lilo.linux.or.jp を wheezy から jessie にあげた話
PDF
Ruby on Rails为什么这么红?
PPTX
Cognitive APIs and Conversational Interfaces
PDF
Python webinar 2nd july
PPTX
程式設計首日封
PDF
淺談 Startup 公司的軟體開發流程 v2
PDF
那些 Functional Programming 教我的事
PDF
リクルートテクノロジーズが語る 企業における、「AI/ディープラーニング」活用のリアル
PDF
Struggling with BIGDATA -リクルートおけるデータサイエンス/エンジニアリング-
nadoka さんの m17n 対応のベストプラクティス
hubot-slack v4移行時のハマりどころ #hubot_chatops
lilo.linux.or.jp を wheezy から jessie にあげた話
Ruby on Rails为什么这么红?
Cognitive APIs and Conversational Interfaces
Python webinar 2nd july
程式設計首日封
淺談 Startup 公司的軟體開發流程 v2
那些 Functional Programming 教我的事
リクルートテクノロジーズが語る 企業における、「AI/ディープラーニング」活用のリアル
Struggling with BIGDATA -リクルートおけるデータサイエンス/エンジニアリング-
Ad

Similar to Rails I18n 20081125 (9)

PDF
⼤語⾔模型 LLM 應⽤開發入⾨
PPT
Rails 初探
PDF
A brief introduction to Machine Learning
PDF
J Ruby和Rails 让Ruby语言融入Java项目
PPT
Rails ABC
PPT
Ruby 的快与慢
PPT
Introduce rails and rubymine
PDF
COSCUP 2010 - node.JS 於互動式網站之應用
PDF
Ruby Rails 老司機帶飛
⼤語⾔模型 LLM 應⽤開發入⾨
Rails 初探
A brief introduction to Machine Learning
J Ruby和Rails 让Ruby语言融入Java项目
Rails ABC
Ruby 的快与慢
Introduce rails and rubymine
COSCUP 2010 - node.JS 於互動式網站之應用
Ruby Rails 老司機帶飛
Ad

More from Wen-Tien Chang (20)

PDF
評估驅動開發 Eval-Driven Development (EDD): 生成式 AI 軟體不確定性的解決方法
PDF
RSpec on Rails Tutorial
PDF
RSpec & TDD Tutorial
PDF
ALPHAhackathon: How to collaborate
PDF
Git 版本控制系統 -- 從微觀到宏觀
PDF
Exception Handling: Designing Robust Software in Ruby (with presentation note)
PDF
Exception Handling: Designing Robust Software in Ruby
PDF
從 Classes 到 Objects: 那些 OOP 教我的事
PDF
Yet another introduction to Git - from the bottom up
PDF
A brief introduction to Vagrant – 原來 VirtualBox 可以這樣玩
PDF
Ruby 程式語言綜覽簡介
PDF
A brief introduction to SPDY - 邁向 HTTP/2.0
PDF
RubyConf Taiwan 2012 Opening & Closing
PDF
從 Scrum 到 Kanban: 為什麼 Scrum 不適合 Lean Startup
PDF
Git Tutorial 教學
PDF
RubyConf Taiwan 2011 Opening & Closing
PDF
BDD style Unit Testing
PDF
RSpec 讓你愛上寫測試
PDF
Git and Github
PDF
Service-Oriented Design and Implement with Rails3
評估驅動開發 Eval-Driven Development (EDD): 生成式 AI 軟體不確定性的解決方法
RSpec on Rails Tutorial
RSpec & TDD Tutorial
ALPHAhackathon: How to collaborate
Git 版本控制系統 -- 從微觀到宏觀
Exception Handling: Designing Robust Software in Ruby (with presentation note)
Exception Handling: Designing Robust Software in Ruby
從 Classes 到 Objects: 那些 OOP 教我的事
Yet another introduction to Git - from the bottom up
A brief introduction to Vagrant – 原來 VirtualBox 可以這樣玩
Ruby 程式語言綜覽簡介
A brief introduction to SPDY - 邁向 HTTP/2.0
RubyConf Taiwan 2012 Opening & Closing
從 Scrum 到 Kanban: 為什麼 Scrum 不適合 Lean Startup
Git Tutorial 教學
RubyConf Taiwan 2011 Opening & Closing
BDD style Unit Testing
RSpec 讓你愛上寫測試
Git and Github
Service-Oriented Design and Implement with Rails3

Rails I18n 20081125