
Rails i18n: Kumpletong Gabay sa Internationalization ng Ruby on Rails
Mula unang locale file hanggang production: i-configure ang Rails I18n, gamitin ang t() helper, hawakan ang CLDR pluralization, iwasan ang mga karaniwang pitfall, at magdagdag ng matatalinong locale fallback chain.
Unawain ang Arkitektura ng Rails I18n
May kasamang I18n gem ang Rails bilang built-in. Nasa config/locales/ ang mga translation file bilang YAML (default) o Ruby file. Nagbibigay ang framework ng t() helper (alias ng I18n.translate) sa views, controllers, models, at mailers. Simple ang Rails I18n ayon sa disenyo — hinahawakan nito ang basic translation, interpolation, at pluralization out of the box.
# Rails includes i18n out of the box via the i18n gem
# config/application.rb
module MyApp
class Application < Rails::Application
# Default locale
config.i18n.default_locale = :en
# Available locales
config.i18n.available_locales = [:en, :de, :ja, :es, :fr, :'pt-BR']
# Fallback to default locale when translation is missing
config.i18n.fallbacks = true
# Load translations from nested directories
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]
end
end# config/routes.rb
Rails.application.routes.draw do
scope "/:locale", locale: /en|de|ja|es|fr|pt-BR/ do
root "home#index"
resources :products
end
root "home#index"
end
# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
around_action :switch_locale
private
def switch_locale(&action)
locale = params[:locale] || I18n.default_locale
I18n.with_locale(locale, &action)
end
def default_url_options
{ locale: I18n.locale }
end
endI-configure ang Mga Setting ng Locale
Itakda ang default_locale, available_locales, at fallback behavior sa config/application.rb o sa isang initializer. I-configure ang locale detection sa inyong ApplicationController gamit ang before_action para itakda ang locale mula sa URL, session, cookie, o Accept-Language header.
# config/locales/en.yml
en:
nav:
home: "Home"
about: "About"
settings: "Settings"
greeting: "Hello, %{name}!"
cart:
item_count:
one: "%{count} item"
other: "%{count} items"
# config/locales/de.yml
de:
nav:
home: "Startseite"
about: "Über uns"
settings: "Einstellungen"
greeting: "Hallo, %{name}!"
cart:
item_count:
one: "%{count} Artikel"
other: "%{count} Artikel"Gamitin ang t() Helper
Available ang t() helper sa buong Rails — views, controllers, models, mailers, at jobs. Tumatanggap ito ng key, mga optional na interpolation variable, at mga opsyon tulad ng default values at scope. Sa views, sinusuportahan ng Rails ang lazy lookups na awtomatikong nag-i-scope ng mga key sa kasalukuyang controller at action.
# In views (ERB)
<h1><%= t('nav.home') %></h1>
<p><%= t('greeting', name: @user.name) %></p>
# In controllers
flash[:notice] = t('flash.product_created')
# In models
validates :name, presence: { message: I18n.t('errors.blank') }
# With HTML (safe)
<%= t('terms_html', link: link_to(t('terms_link'), '/terms')) %>Hawakan ang Pluralization
Gumagamit ang Rails I18n ng mga CLDR plural category: zero, one, two, few, many, other. Kailangan lang ng English ang one at other, pero mas maraming anyo ang kailangan ng ibang wika. Tukuyin ang mga plural translation bilang nested YAML key sa ilalim ng mga count category na kailangan ng inyong mga target na wika.
# In YAML:
en:
cart:
item_count:
zero: "No items"
one: "%{count} item"
other: "%{count} items"
# Arabic (6 forms):
ar:
cart:
item_count:
zero: "لا عناصر"
one: "عنصر واحد"
two: "عنصران"
few: "%{count} عناصر"
many: "%{count} عنصرًا"
other: "%{count} عنصر"
# Usage in views:
<%= t('cart.item_count', count: @cart.items.size) %>Magdagdag ng Mga Locale Fallback Chain
Ang built-in na I18n.fallbacks ng Rails ay nagbibigay lang ng basic fallback mula regional papunta sa default locale. Kapag may nawawalang key ang pt-BR user, English ang makikita imbes na pt-PT. Nagdadagdag ang rails-locale-chain ng configurable deep-merge chain para laging makita ng mga regional user ang pinakamalapit na available na pagsasalin.
# Lazy lookups use the controller/action as scope
# app/views/products/index.html.erb
# Instead of t('products.index.title'), just use:
<h1><%= t('.title') %></h1>
<p><%= t('.description') %></p>
# Rails looks up: products.index.title and products.index.description
# config/locales/en.yml
en:
products:
index:
title: "All Products"
description: "Browse our catalog"I-automate ang Mga Pagsasalin
Kapag kumpleto na ang Rails I18n setup ninyo, isalin ang inyong mga YAML locale file gamit ang AI. Sinusuportahan ng i18n Agent ang YAML nang native — ituro ito sa source locale file at bubuo ito ng lahat ng target language, habang pinapanatili ang mga nested key, interpolation variable, at plural form.
# Gemfile
gem 'rails-locale-chain'
# config/initializers/locale_chain.rb
Rails.application.config.i18n.fallbacks = {
'pt-BR': ['pt', 'en'],
'zh-Hant-TW': ['zh-Hant', 'zh', 'en'],
'es-419': ['es', 'en'],
}
# The gem deep-merges translations across the chain
# pt-BR -> pt -> en
# Missing keys in pt-BR are filled from pt, then enMga Karaniwang Pitfall
Mga Error na InvalidPluralizationData
Lazy Lookup sa Labas ng Views
Sinisira ng Mga Error sa YAML Syntax ang Lahat ng Pagsasalin
Subukan ang i18n Agent Ngayon
I-drop dito ang inyong translation file
JSON, YAML, PO, XML, CSV, Markdown, Properties
o i-click para mag-browse
Mga target language