مرجع سريع

ورقة غش التدويل في Rails (i18n)

دليل مرجعي سريع للتدويل في Rails: الترجمات، التواريخ المترجمة، صيغ الجمع، والبحث ضمن نطاق وملفات قواميس YAML.

· تاريخ النشر

يوفر إطار التدويل في Rails (i18n) دعم التوطين للنصوص والأرقام والعملات والتواريخ عبر قواميس YAML المعيارية.

دوال الترجمة والتاريخ الأساسية

<!-- ترجمة مفتاح -->
<%= t('welcome.title') %>

<!-- ترجمة ضمن نطاق المتحكم/الإجراء الحالي تلقائياً -->
<!-- في app/views/articles/index.html.erb: يبحث عن articles.index.heading -->
<%= t('.heading') %>

<!-- توطين التواريخ والأوقات -->
<%= l(Time.now, format: :short) %>
<%= l(Date.today, format: :long) %>

إدراج المتغيرات وصيغ الجمع (Pluralization)

# 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."

في القالب:

<%= t('greetings.hello', name: current_user.name) %>
<%= t('inbox.unread_messages', count: @unread_count) %>

تعيين اللغة الحالية (Locale)

في 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

en:
  activerecord:
    models:
      user: "Customer"
    attributes:
      user:
        email: "Email Address"
        role: "Account Role"

في العرض:

<%= User.model_name.human %>
<!-- => "Customer" -->

<%= User.human_attribute_name(:email) %>
<!-- => "Email Address" -->

مراجع

موضوعات ذات صلة