Rails I18n 20081125

Wen-Tien Chang
Wen-Tien Chang😆 👨🏻‍💻 📚 🚀 💰
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!
1 of 23

Recommended

楽しく学ぶ?API by
楽しく学ぶ?API楽しく学ぶ?API
楽しく学ぶ?APItreby
1.4K views52 slides
Certbotで無料TLSサーバー by
Certbotで無料TLSサーバーCertbotで無料TLSサーバー
Certbotで無料TLSサーバーKazuhiro Nishiyama
412 views50 slides
正規表現の先読みについて by
正規表現の先読みについて正規表現の先読みについて
正規表現の先読みについてKazuhiro Nishiyama
1K views8 slides
Ruby on Rails 開發環境建置 for Mac by
Ruby on Rails 開發環境建置 for MacRuby on Rails 開發環境建置 for Mac
Ruby on Rails 開發環境建置 for MacMarsZ Chen
4.7K views83 slides
Sublime Text 2 Tips & Tricks by
Sublime Text 2 Tips & TricksSublime Text 2 Tips & Tricks
Sublime Text 2 Tips & TricksRhys Wynne
1.3K views16 slides
Ruby on Rails : 簡介與入門 by
Ruby on Rails : 簡介與入門Ruby on Rails : 簡介與入門
Ruby on Rails : 簡介與入門Wen-Tien Chang
75.3K views51 slides

More Related Content

Viewers also liked

nadoka さんの m17n 対応のベストプラクティス by
nadoka さんの m17n 対応のベストプラクティスnadoka さんの m17n 対応のベストプラクティス
nadoka さんの m17n 対応のベストプラクティスKazuhiro Nishiyama
1.1K views19 slides
hubot-slack v4移行時のハマりどころ #hubot_chatops by
hubot-slack v4移行時のハマりどころ #hubot_chatopshubot-slack v4移行時のハマりどころ #hubot_chatops
hubot-slack v4移行時のハマりどころ #hubot_chatopsknjcode
6K views28 slides
lilo.linux.or.jp を wheezy から jessie にあげた話 by
lilo.linux.or.jp を wheezy から jessie にあげた話lilo.linux.or.jp を wheezy から jessie にあげた話
lilo.linux.or.jp を wheezy から jessie にあげた話Kazuhiro Nishiyama
2.2K views57 slides
Ruby on Rails为什么这么红? by
Ruby on Rails为什么这么红?Ruby on Rails为什么这么红?
Ruby on Rails为什么这么红?Nathan Chen
1.3K views45 slides
Cognitive APIs and Conversational Interfaces by
Cognitive APIs and Conversational InterfacesCognitive APIs and Conversational Interfaces
Cognitive APIs and Conversational InterfacesPavel Veller
1.6K views41 slides
Python webinar 2nd july by
Python webinar 2nd julyPython webinar 2nd july
Python webinar 2nd julyVineet Chaturvedi
4.9K views24 slides

Viewers also liked(11)

nadoka さんの m17n 対応のベストプラクティス by Kazuhiro Nishiyama
nadoka さんの m17n 対応のベストプラクティスnadoka さんの m17n 対応のベストプラクティス
nadoka さんの m17n 対応のベストプラクティス
Kazuhiro Nishiyama1.1K views
hubot-slack v4移行時のハマりどころ #hubot_chatops by knjcode
hubot-slack v4移行時のハマりどころ #hubot_chatopshubot-slack v4移行時のハマりどころ #hubot_chatops
hubot-slack v4移行時のハマりどころ #hubot_chatops
knjcode6K views
lilo.linux.or.jp を wheezy から jessie にあげた話 by Kazuhiro Nishiyama
lilo.linux.or.jp を wheezy から jessie にあげた話lilo.linux.or.jp を wheezy から jessie にあげた話
lilo.linux.or.jp を wheezy から jessie にあげた話
Kazuhiro Nishiyama2.2K views
Ruby on Rails为什么这么红? by Nathan Chen
Ruby on Rails为什么这么红?Ruby on Rails为什么这么红?
Ruby on Rails为什么这么红?
Nathan Chen1.3K views
Cognitive APIs and Conversational Interfaces by Pavel Veller
Cognitive APIs and Conversational InterfacesCognitive APIs and Conversational Interfaces
Cognitive APIs and Conversational Interfaces
Pavel Veller1.6K views
程式設計首日封 by 政斌 楊
程式設計首日封程式設計首日封
程式設計首日封
政斌 楊800 views
淺談 Startup 公司的軟體開發流程 v2 by Wen-Tien Chang
淺談 Startup 公司的軟體開發流程 v2淺談 Startup 公司的軟體開發流程 v2
淺談 Startup 公司的軟體開發流程 v2
Wen-Tien Chang38.8K views
那些 Functional Programming 教我的事 by Wen-Tien Chang
那些 Functional Programming 教我的事那些 Functional Programming 教我的事
那些 Functional Programming 教我的事
Wen-Tien Chang27.6K views
リクルートテクノロジーズが語る 企業における、「AI/ディープラーニング」活用のリアル by Recruit Technologies
リクルートテクノロジーズが語る 企業における、「AI/ディープラーニング」活用のリアルリクルートテクノロジーズが語る 企業における、「AI/ディープラーニング」活用のリアル
リクルートテクノロジーズが語る 企業における、「AI/ディープラーニング」活用のリアル
Recruit Technologies30.1K views
Struggling with BIGDATA -リクルートおけるデータサイエンス/エンジニアリング- by Recruit Technologies
Struggling with BIGDATA -リクルートおけるデータサイエンス/エンジニアリング-Struggling with BIGDATA -リクルートおけるデータサイエンス/エンジニアリング-
Struggling with BIGDATA -リクルートおけるデータサイエンス/エンジニアリング-

Similar to Rails I18n 20081125

⼤語⾔模型 LLM 應⽤開發入⾨ by
⼤語⾔模型 LLM 應⽤開發入⾨⼤語⾔模型 LLM 應⽤開發入⾨
⼤語⾔模型 LLM 應⽤開發入⾨Wen-Tien Chang
5.4K views94 slides
Rails 初探 by
Rails 初探Rails 初探
Rails 初探满佳 林
376 views12 slides
A brief introduction to Machine Learning by
A brief introduction to Machine LearningA brief introduction to Machine Learning
A brief introduction to Machine LearningWen-Tien Chang
10.9K views145 slides
J Ruby和Rails 让Ruby语言融入Java项目 by
J Ruby和Rails 让Ruby语言融入Java项目J Ruby和Rails 让Ruby语言融入Java项目
J Ruby和Rails 让Ruby语言融入Java项目George Ang
1.2K views41 slides
Rails ABC by
Rails ABCRails ABC
Rails ABCqtlove
434 views29 slides
Ruby 的快与慢 by
Ruby 的快与慢Ruby 的快与慢
Ruby 的快与慢vincent253
1.5K views28 slides

Similar to Rails I18n 20081125(9)

⼤語⾔模型 LLM 應⽤開發入⾨ by Wen-Tien Chang
⼤語⾔模型 LLM 應⽤開發入⾨⼤語⾔模型 LLM 應⽤開發入⾨
⼤語⾔模型 LLM 應⽤開發入⾨
Wen-Tien Chang5.4K views
Rails 初探 by 满佳 林
Rails 初探Rails 初探
Rails 初探
满佳 林376 views
A brief introduction to Machine Learning by Wen-Tien Chang
A brief introduction to Machine LearningA brief introduction to Machine Learning
A brief introduction to Machine Learning
Wen-Tien Chang10.9K views
J Ruby和Rails 让Ruby语言融入Java项目 by George Ang
J Ruby和Rails 让Ruby语言融入Java项目J Ruby和Rails 让Ruby语言融入Java项目
J Ruby和Rails 让Ruby语言融入Java项目
George Ang1.2K views
Rails ABC by qtlove
Rails ABCRails ABC
Rails ABC
qtlove434 views
Ruby 的快与慢 by vincent253
Ruby 的快与慢Ruby 的快与慢
Ruby 的快与慢
vincent2531.5K views
Introduce rails and rubymine by Calvin Huang
Introduce rails and rubymineIntroduce rails and rubymine
Introduce rails and rubymine
Calvin Huang771 views
COSCUP 2010 - node.JS 於互動式網站之應用 by ericpi Bi
COSCUP 2010 - node.JS 於互動式網站之應用COSCUP 2010 - node.JS 於互動式網站之應用
COSCUP 2010 - node.JS 於互動式網站之應用
ericpi Bi1.7K views
Ruby Rails 老司機帶飛 by Wen-Tien Chang
Ruby Rails 老司機帶飛Ruby Rails 老司機帶飛
Ruby Rails 老司機帶飛
Wen-Tien Chang2.1K views

More from Wen-Tien Chang

RSpec on Rails Tutorial by
RSpec on Rails TutorialRSpec on Rails Tutorial
RSpec on Rails TutorialWen-Tien Chang
4.9K views95 slides
RSpec & TDD Tutorial by
RSpec & TDD TutorialRSpec & TDD Tutorial
RSpec & TDD TutorialWen-Tien Chang
3K views105 slides
ALPHAhackathon: How to collaborate by
ALPHAhackathon: How to collaborateALPHAhackathon: How to collaborate
ALPHAhackathon: How to collaborateWen-Tien Chang
2.5K views50 slides
Git 版本控制系統 -- 從微觀到宏觀 by
Git 版本控制系統 -- 從微觀到宏觀Git 版本控制系統 -- 從微觀到宏觀
Git 版本控制系統 -- 從微觀到宏觀Wen-Tien Chang
23.6K views109 slides
Exception Handling: Designing Robust Software in Ruby (with presentation note) by
Exception Handling: Designing Robust Software in Ruby (with presentation note)Exception Handling: Designing Robust Software in Ruby (with presentation note)
Exception Handling: Designing Robust Software in Ruby (with presentation note)Wen-Tien Chang
8.8K views111 slides
Exception Handling: Designing Robust Software in Ruby by
Exception Handling: Designing Robust Software in RubyException Handling: Designing Robust Software in Ruby
Exception Handling: Designing Robust Software in RubyWen-Tien Chang
11.9K views111 slides

More from Wen-Tien Chang(20)

ALPHAhackathon: How to collaborate by Wen-Tien Chang
ALPHAhackathon: How to collaborateALPHAhackathon: How to collaborate
ALPHAhackathon: How to collaborate
Wen-Tien Chang2.5K views
Git 版本控制系統 -- 從微觀到宏觀 by Wen-Tien Chang
Git 版本控制系統 -- 從微觀到宏觀Git 版本控制系統 -- 從微觀到宏觀
Git 版本控制系統 -- 從微觀到宏觀
Wen-Tien Chang23.6K views
Exception Handling: Designing Robust Software in Ruby (with presentation note) by Wen-Tien Chang
Exception Handling: Designing Robust Software in Ruby (with presentation note)Exception Handling: Designing Robust Software in Ruby (with presentation note)
Exception Handling: Designing Robust Software in Ruby (with presentation note)
Wen-Tien Chang8.8K views
Exception Handling: Designing Robust Software in Ruby by Wen-Tien Chang
Exception Handling: Designing Robust Software in RubyException Handling: Designing Robust Software in Ruby
Exception Handling: Designing Robust Software in Ruby
Wen-Tien Chang11.9K views
從 Classes 到 Objects: 那些 OOP 教我的事 by Wen-Tien Chang
從 Classes 到 Objects: 那些 OOP 教我的事從 Classes 到 Objects: 那些 OOP 教我的事
從 Classes 到 Objects: 那些 OOP 教我的事
Wen-Tien Chang14K views
Yet another introduction to Git - from the bottom up by Wen-Tien Chang
Yet another introduction to Git - from the bottom upYet another introduction to Git - from the bottom up
Yet another introduction to Git - from the bottom up
Wen-Tien Chang26.2K views
A brief introduction to Vagrant – 原來 VirtualBox 可以這樣玩 by Wen-Tien Chang
A brief introduction to Vagrant – 原來 VirtualBox 可以這樣玩A brief introduction to Vagrant – 原來 VirtualBox 可以這樣玩
A brief introduction to Vagrant – 原來 VirtualBox 可以這樣玩
Wen-Tien Chang35.2K views
Ruby 程式語言綜覽簡介 by Wen-Tien Chang
Ruby 程式語言綜覽簡介Ruby 程式語言綜覽簡介
Ruby 程式語言綜覽簡介
Wen-Tien Chang9.5K views
A brief introduction to SPDY - 邁向 HTTP/2.0 by Wen-Tien Chang
A brief introduction to SPDY - 邁向 HTTP/2.0A brief introduction to SPDY - 邁向 HTTP/2.0
A brief introduction to SPDY - 邁向 HTTP/2.0
Wen-Tien Chang20.7K views
RubyConf Taiwan 2012 Opening & Closing by Wen-Tien Chang
RubyConf Taiwan 2012 Opening & ClosingRubyConf Taiwan 2012 Opening & Closing
RubyConf Taiwan 2012 Opening & Closing
Wen-Tien Chang14.2K views
從 Scrum 到 Kanban: 為什麼 Scrum 不適合 Lean Startup by Wen-Tien Chang
從 Scrum 到 Kanban: 為什麼 Scrum 不適合 Lean Startup從 Scrum 到 Kanban: 為什麼 Scrum 不適合 Lean Startup
從 Scrum 到 Kanban: 為什麼 Scrum 不適合 Lean Startup
Wen-Tien Chang24.5K views
RubyConf Taiwan 2011 Opening & Closing by Wen-Tien Chang
RubyConf Taiwan 2011 Opening & ClosingRubyConf Taiwan 2011 Opening & Closing
RubyConf Taiwan 2011 Opening & Closing
Wen-Tien Chang1.9K views
RSpec 讓你愛上寫測試 by Wen-Tien Chang
RSpec 讓你愛上寫測試RSpec 讓你愛上寫測試
RSpec 讓你愛上寫測試
Wen-Tien Chang30.2K views
Service-Oriented Design and Implement with Rails3 by Wen-Tien Chang
Service-Oriented Design and Implement with Rails3Service-Oriented Design and Implement with Rails3
Service-Oriented Design and Implement with Rails3
Wen-Tien Chang4.5K views

Rails I18n 20081125