王信凱(Sky)
AmazingTutor CTO
WTT-0728
rails
i18n
config/application.rb
config.i18n.load_path += Dir[Rails.root.join('locales', '**', '*.{rb,yml}').to_s]
config.i18n.default_locale = :en
use default locale when translation missing
config.i18n.fallbacks = true
# or
config.i18n.fallbacks = "zh-TW"
but "zh-CN" fallback not include zh-TW …
irb(main):007:0> I18n.fallbacks["zh-CN"]
=> [:"zh-CN", :zh, :en]
set it in environments/production.rb
config.i18n.fallbacks = {'zh-CN' => 'zh-TW'}
# magic ~!!
irb(main):007:0> I18n.fallbacks["zh-CN"]
=> [:"zh-CN", :zh, :"zh-TW", :en]
app/config/locales/*.yml
# debug in rails console
irb(main):006:0> I18n.t("date.day_names")
=> ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
irb(main):007:0> I18n.t("date.day_names", locale: "zh-TW")
=> ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"]
# before
<%= e.created_at.strftime('%B') %> <%= e.created_at.strftime('%d') %>, <%= e.created_at.strftime('%Y') %>
=> "June 20, 2016"
# after
<%= l(e.created_at, format: :ymd) %>
=> "June 20, 2016"
# en.yml
en:
time:
am: am
formats:
default: "%a, %d %b %Y %H:%M:%S %z"
ymd: "%B %d, %Y"
app/config/locales/view.zh-TW.yml
zh-TW:
button:
save: "儲存"
update: "更新"
share:
with: "與"
book_now: "立即預約"
all: "全部"
common:
navigation:
site_language: "網站語言"
teacher_set: "教師資訊"
schedule: "授課日曆"
users:
profile:
connent: "Facebook Google 連結"
edit_profile: "修改個人資料"
sidebar:
scheduled: "已排課"
unschedule: "尚未排課"
past: "已上過"
class: "課程"
my_tutor: "我的教師"
feed_back: "教師回饋"
edit_profile: "修改個人資料"
change_password: "更改密碼"
I18n.t("button.save", locale: "zh-TW")
=> "儲存"
I18n.t("save", locale: "zh-TW", scope: "button")
=> "儲存"
# in Rails just use t()
I18n.locale = :en
t("button.save")
=> "Save"
zh-TW:
button:
save: "儲存"
update: "更新"
app/views/common/_navigation.html.erb
<%= t("common.navigation.site_language") %>
<%= t("site_language", scope: "common.navigation") %>
=> "網站語言"
zh-TW:
common:
navigation:
site_language: "網站語言"
app/views/common/_navigation.html.erb
use lazy lookup !!
<%= t(".site_language") %>
=> "網站語言"
zh-TW:
common:
navigation:
site_language: "網站語言"
ActiveRecord attributes
irb(main)> User.model_name.human
=> "使用者"
irb(main)> User.human_attribute_name(:birthday)
=> "生日"
# config/locales/zh-TW.yml zh-TW: activerecord: models: user: "使用者" attributes: user: birthday: "生日"
app/views/products/show.html.erb
<%= t('product_price', price: @product.price) %>
# config/locales/en.yml
en:
product_price: "$%{price}"
# config/locales/es.yml
es:
product_price: "%{price} €"
<div><%= t('welcome') %></div>
<div><%= raw t('welcome') %></div>
<div><%= t('hello_html') %></div>
<div><%= t('title.html') %></div>
# config/locales/en.yml
en:
welcome: <b>welcome!</b>
hello_html: <b>hello!</b>
title:
html: <b>title!</b>
views/personal_mailer/welcome_message.html.erb
<%= t("personal_mailer.welcome_message.content_html", user_name: "Sky") %>
zh-TW:
personal_mailer:
welcome_message:
content_html: |
<p>hello %{user_name}<p>
<p>welcome to Web Tech Topic<p>
open chrome console and type
navigator.language
=> "zh-TW"
navigator.languages
=> ["zh-TW", "zh", "en-US", "en"]
>> env["HTTP_ACCEPT_LANGUAGE"]
#chrome
=> "zh-TW,zh;q=0.8,en-US;q=0.6,en;q=0.4"
# safari
=> "zh-tw"
# Gemfile
gem 'rack-contrib', :github => "jcsky/rack-contrib"
# config.ru
require 'rack'
require 'rack/contrib'
use Rack::Locale