Rails I18n 20081125

23
Rails 2.2 i18n [email protected]
  • date post

    12-Sep-2014
  • Category

    Technology

  • view

    4.053
  • download

    2

description

 

Transcript of Rails I18n 20081125

Page 1: Rails I18n 20081125

Rails 2.2 [email protected]

Page 2: Rails I18n 20081125

The history of Rails I18n

Page 3: Rails I18n 20081125

Chaos & Painful

Page 4: Rails I18n 20081125

we have...

• Ruby Gettext

• DHH’s Localization

• Globalize

• Simple Localization

• Gibberish

Page 5: Rails I18n 20081125

Problems

• 一升級 Rails 就爛了

• 乏人維護,缺少支援• 可恨的 Monkeypatches

Page 6: Rails I18n 20081125

Rails i18n project

• 共通的 API

• 可抽換 backend

• 盡量簡單

Page 7: Rails I18n 20081125

Rails 2.2

• Powerful I18n API

• Simple Backend

Page 8: Rails I18n 20081125

I18n API簡單、一致

Page 9: Rails I18n 20081125

API methods

• I18n.translate :hello

• I18n.t :hello

• t :hello

• I18n.localize Time.now

• I18n.l Time.now

• l Time.now

Page 10: Rails I18n 20081125

Features• Flexible keys

I18n.t :helloI18n.t "hello"

• Scopes (aka namespaces)hello: 哈囉account: hello: 你好

I18n.t “hello” # 哈囉I18n.t "account.hello” # 你好

Page 11: Rails I18n 20081125

Features (cont.)

• Interpolationmessage: "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"

Page 12: Rails I18n 20081125

Backend100% 可抽換

Page 13: Rails I18n 20081125

Simple Backend

• Shipped as YAML files

• You can also use plain Ruby files

Page 14: Rails I18n 20081125

Simple Backend

• 使用 YAML 或 plain Ruby file

• 預設只有 en-US,請下載http://github.com/svenfuchs/rails-i18n/感謝 Tsechingho 提供中文版本

Page 15: Rails I18n 20081125

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: "必須是偶數"

Page 16: Rails I18n 20081125

Setup i18n超簡單

Page 17: Rails I18n 20081125

environment.rb

# 所有 config/locales/*.rb,yml 將被載入

config.i18n.load_path << Dir[File.join(RAILS_ROOT, 'config', 'locales', '*.{rb,yml}')]

config.i18n.default_locale = :en

Page 18: Rails I18n 20081125

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

Page 19: Rails I18n 20081125

view 提供切換連結

• http://localhost:3000/?locale=en

• http://localhost:3000/?locale=ja

• http://localhost:3000/?locale=zh-TW

• ....

Page 20: Rails I18n 20081125

可抽換 Backend?

Page 21: Rails I18n 20081125

Google translate backendhttp://github.com/ihower/i18n_google_translate/

by ihower

Page 22: Rails I18n 20081125

environment.rb

I18n.backend = I18n::Backend::GoogleTranslate.newI18n.default_locale = 'en'

Page 23: Rails I18n 20081125

Live demo!