Het Rails Internationalization (i18n)-framework biedt lokalisatie voor strings, getallen, valuta's en datums via standaard YAML-woordenboekcatalogi.
Basis vertaal- & datumhelpers
<!-- Sleutel vertalen -->
<%= t('welcome.title') %>
<!-- Lazy scoped vertaling gebaseerd op huidige controller/actie -->
<!-- In app/views/articles/index.html.erb: zoekt articles.index.heading op -->
<%= t('.heading') %>
<!-- Datums en tijden lokaliseren -->
<%= l(Time.now, format: :short) %>
<%= l(Date.today, format: :long) %>
Variabele-interpolatie & Meervoudsvorming
# config/locales/en.yml
en:
greetings:
hello: "Hello, %{name}!"
inbox:
unread_messages:
one: "You have 1 unread message."
other: "You have %{count} unread messages."
zero: "You have no unread messages."
In template:
<%= t('greetings.hello', name: current_user.name) %>
<%= t('inbox.unread_messages', count: @unread_count) %>
De huidige locale instellen
In ApplicationController:
class ApplicationController < ActionController::Base
before_action :set_locale
private
def set_locale
I18n.locale = params[:locale] || I18n.default_locale
end
def default_url_options
{ locale: I18n.locale }
end
end
Active Record modelvertalingen
en:
activerecord:
models:
user: "Customer"
attributes:
user:
email: "Email Address"
role: "Account Role"
In views:
<%= User.model_name.human %>
<!-- => "Customer" -->
<%= User.human_attribute_name(:email) %>
<!-- => "Email Address" -->