| name | Rails Localization (i18n) - English & Arabic |
| description | Chief Content Copywriter with comprehensive internationalization skill for Ruby on Rails applications with proper English, Arabic, French and Spanish translations, RTL support, pluralization rules, date/time formatting, and culturally appropriate content adaptation. Use when: (1) Setting up i18n in Rails, (2) Adding RTL/Arabic support, (3) Implementing pluralization, (4) Formatting dates/currency, (5) Creating locale files. Trigger keywords: i18n, translations, localization, internationalization, locale, RTL, Arabic, multilingual, localize, translate |
| version | 1.1.0 |
Rails Localization Skill
Comprehensive guidance for implementing i18n in Ruby on Rails with English/Arabic support.
Localization Decision Tree
What localization task?
โ
โโ Setting up i18n from scratch?
โ โโ See "Project Setup" below
โ
โโ Adding Arabic/RTL support?
โ โโ See references/rtl-css.md + references/helpers.md
โ
โโ Need locale YAML files?
โ โโ See references/locale-files.md
โ
โโ Implementing pluralization?
โ โโ See "Arabic Pluralization" below
โ
โโ Creating views/forms?
โ โโ See references/view-components.md
โ
โโ Writing i18n tests?
โ โโ See references/testing.md
โ
โโ Currency/number formatting?
โโ See "Formatting Quick Reference" below
NEVER Do This
NEVER use direct translation for Arabic:
ar:
items:
one: "ุนูุตุฑ"
other: "ุนูุงุตุฑ"
ar:
items:
zero: "ุนูุงุตุฑ"
one: "ุนูุตุฑ"
two: "ุนูุตุฑุงู"
few: "ุนูุงุตุฑ"
many: "ุนูุตุฑูุง"
other: "ุนูุตุฑ"
NEVER hardcode user-facing strings:
flash[:notice] = "User created successfully"
flash[:notice] = t('users.created_successfully')
NEVER use physical CSS properties for layout:
margin-left: 1rem;
text-align: left;
margin-inline-start: 1rem;
text-align: start;
NEVER let numbers break in RTL:
<%# WRONG - Numbers display incorrectly %>
<span><%= amount %></span>
<%# RIGHT - Keep numbers LTR %>
<span dir="ltr"><%= amount %></span>
NEVER concatenate translated strings:
"Hello " + name + ", welcome!"
t('greetings.welcome', name: name)
Project Setup
1. Configure Application
config.i18n.available_locales = [:en, :ar]
config.i18n.default_locale = :en
config.i18n.fallbacks = { ar: [:ar, :en], en: [:en] }
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]
2. Application Controller
class ApplicationController < ActionController::Base
around_action :switch_locale
private
def switch_locale(&action)
locale = params[:locale] || cookies[:locale] || I18n.default_locale
I18n.with_locale(locale, &action)
end
def default_url_options
{ locale: I18n.locale }
end
end
3. Route Configuration
scope "(:locale)", locale: /en|ar/ do
resources :users
root "home#index"
end
get "locale/:locale", to: "locales#switch", as: :switch_locale
Arabic Localization Principles
Arabic localization requires cultural and linguistic adaptation, not direct translation:
| Aspect | Requirement |
|---|
| Direction | RTL (right-to-left) layout |
| Pluralization | 6 forms: zero, one, two, few (3-10), many (11-99), other (100+) |
| Gender | Nouns and adjectives have grammatical gender |
| Numbers | Eastern Arabic (ู ูกูขูฃูคูฅูฆูงูจูฉ) or Western (0123456789) |
| Calendar | Hijri calendar support may be needed |
| Formality | Different greeting/formality levels |
Arabic Pluralization Rules
| Count | Form | Arabic Example (days) |
|---|
| 0 | zero | ุตูุฑ ุฃูุงู
|
| 1 | one | ููู
ูุงุญุฏ |
| 2 | two | ููู
ุงู |
| 3-10 | few | ูฃ ุฃูุงู
|
| 11-99 | many | ูขู ููู
ูุง |
| 100+ | other | ูกู ู ููู
|
ar:
datetime:
distance_in_words:
x_days:
zero: "ุตูุฑ ุฃูุงู
"
one: "ููู
ูุงุญุฏ"
two: "ููู
ุงู"
few: "%{count} ุฃูุงู
"
many: "%{count} ููู
ูุง"
other: "%{count} ููู
"
Formatting Quick Reference
Currency
def localized_currency(amount, currency: 'SAR')
if I18n.locale == :ar
"#{number_with_precision(amount, precision: 2)} ุฑ.ุณ"
else
"SAR #{number_with_precision(amount, precision: 2)}"
end
end
Eastern Arabic Numerals
EASTERN_ARABIC = { '0'=>'ู ', '1'=>'ูก', '2'=>'ูข', '3'=>'ูฃ', '4'=>'ูค',
'5'=>'ูฅ', '6'=>'ูฆ', '7'=>'ูง', '8'=>'ูจ', '9'=>'ูฉ' }
def to_eastern_arabic(number)
number.to_s.gsub(/[0-9]/, EASTERN_ARABIC)
end
Direction Helpers
def rtl? = I18n.t('direction', default: 'ltr') == 'rtl'
def direction_class = rtl? ? 'rtl' : 'ltr'
def start_align = rtl? ? 'right' : 'left'
def end_align = rtl? ? 'left' : 'right'
Locale File Structure
config/locales/
โโโ en/
โ โโโ activerecord.en.yml
โ โโโ views.en.yml
โ โโโ controllers.en.yml
โโโ ar/
โ โโโ activerecord.ar.yml
โ โโโ views.ar.yml
โ โโโ controllers.ar.yml
โโโ defaults/
โ โโโ en.yml # Rails defaults
โ โโโ ar.yml
โโโ shared/
โโโ errors.en.yml
โโโ errors.ar.yml
RTL Layout Essentials
HTML Structure
<html lang="<%= I18n.locale %>" dir="<%= rtl? ? 'rtl' : 'ltr' %>">
<body class="<%= direction_class %>">
Form Field Directions
| Field Type | Direction |
|---|
| Email | Always dir="ltr" |
| Phone | Always dir="ltr" |
| URL | Always dir="ltr" |
| Numbers | Always dir="ltr" |
| Names | dir="auto" |
| Text content | dir="auto" |
CSS Logical Properties
| Physical | Logical |
|---|
margin-left | margin-inline-start |
margin-right | margin-inline-end |
padding-left | padding-inline-start |
text-align: left | text-align: start |
Best Practices Checklist
Quick Commands
rails i18n:missing
rails i18n:keys
rails i18n:export LOCALE=ar
References
Detailed patterns and examples in references/:
locale-files.md - Complete English/Arabic YAML locale files
helpers.md - LocalizationHelper and ArabicHelper with all methods
rtl-css.md - RTL stylesheet and Tailwind logical properties
view-components.md - Layout, forms, language switcher templates
testing.md - RSpec helpers and i18n test patterns
External Resources