i18n-patterns について
Rails 8 開発者向けの国際化パターンガイドです。翻訳ファイルの整理、ロケール、書式設定、テストを扱い、ビュー、コントローラー、モデルでの実践例を示します。
主な機能
- 01ドメイン別のロケールファイル整理
- 02ビューでの遅延検索
- 03日付・時刻・数値のローカライズ
- 04ロケール切り替えとフォールバックの設定
- 05未使用・不足翻訳の検出
活用シーン
- 01Rails アプリに多言語 UI を追加
- 02モデルとビューの翻訳を一貫して管理
- 03翻訳範囲をテストし不足項目を確認
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ thibautbaissac/ rails_ai_agents --skill i18n-patternsインストール後、Claude Code で/i18n-patternsを入力して読み込まれたか確認します。
同梱ファイル
SKILL.md と一緒にインストールされる参考資料やスクリプト。
references/locale-files.mdtesting.mdusage-patterns.md詳細
Rails 8 の I18n パターン
概要
Rails I18n は国際化をサポートします。
- 翻訳の検索
- ロケール管理
- 日付/時刻/通貨の書式設定
- 複数形化のルール
- ビューでの遅延検索
クイックスタート
# config/application.rb
config.i18n.default_locale = :en
config.i18n.available_locales = [:en, :fr, :de]
config.i18n.fallbacks = true
プロジェクト構成
config/locales/
├── en.yml # English defaults
├── fr.yml # French defaults
├── models/
│ ├── en.yml # Model translations (EN)
│ └── fr.yml # Model translations (FR)
├── views/
│ ├── en.yml # View translations (EN)
│ └── fr.yml # View translations (FR)
├── mailers/
│ ├── en.yml # Mailer translations (EN)
│ └── fr.yml # Mailer translations (FR)
└── components/
├── en.yml # Component translations (EN)
└── fr.yml # Component translations (FR)
ロケールファイルの構成
ロケールファイルはドメインごとに整理します。models/、views/、mailers/、components/ を使用します。
- モデル:
activerecord.models、activerecord.attributes、activerecord.errors - ビュー: コントローラー名とアクションの下にネストします(例:
events.index.title) - 共有:
common.actions、common.messages、common.date.formats - コンポーネント:
components.<component_name>.<key>
モデル、ビュー、共有キー、コンポーネントの完全な YAML の例については、locale-files.md を参照してください。
使用パターン
主な原則
- ビューでは遅延検索を使用します。
t(".title")は"events.index.title"に解決されます - HTML マークアップを含む文字列には
_htmlサフィックスを使用します - 日付、時刻、数値には
I18n.tではなくI18n.l(ローカライズ)を使用します - モデル、サービス、プレゼンターでは、完全なキーのパスを指定して
I18n.tを使用します - 補間で動的な値を渡します。
t(".greeting", name: user.name)
ビューの場合
<h1><%= t(".title") %></h1>
<%= link_to t(".new_event"), new_event_path %>
<p><%= t(".welcome", name: current_user.name) %></p>
<p><%= t(".intro_html", link: link_to("here", help_path)) %></p>
コントローラーの場合
redirect_to @event, notice: t(".success")
モデル/プレゼンターの場合
I18n.t("activerecord.attributes.event/statuses.#{status}")
I18n.l(event_date, format: :long)
プレゼンター、コンポーネント、日付/通貨の書式設定、複数形化を含む完全な例については、usage-patterns.md を参照してください。
ロケールの切り替え
URL ベースのロケール
# config/routes.rb
Rails.application.routes.draw do
scope "(:locale)", locale: /en|fr|de/ do
resources :events
end
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
end
ユーザー設定のロケール
class ApplicationController < ActionController::Base
around_action :switch_locale
private
def switch_locale(&action)
locale = current_user&.locale || extract_locale_from_header || I18n.default_locale
I18n.with_locale(locale, &action)
end
def extract_locale_from_header
request.env['HTTP_ACCEPT_LANGUAGE']&.scan(/^[a-z]{2}/)&.first
end
end
I18n のテスト
spec/rails_helper.rbで翻訳の欠落時に例外を発生させますi18n-tasksgem を使用して、欠落しているキーや未使用のキーを検出します- レンダリングされたコンテンツを検証するビューの翻訳スペックを作成します
スペックの完全な例と i18n-tasks の設定については、testing.md を参照してください。
ベストプラクティス
推奨事項
# Use nested structure matching view paths
en:
events:
index:
title: Events
show:
title: Event Details
# Use interpolation for dynamic content
en:
greeting: "Hello, %{name}!"
# Use _html suffix for HTML content
en:
intro_html: "Welcome to <strong>our app</strong>"
非推奨事項
# Don't use flat keys
en:
events_index_title: Events # BAD
# Don't hardcode in views
<h1>Events</h1> # BAD - use t(".title")
# Don't concatenate translations
t("hello") + " " + t("world") # BAD
チェックリスト
- ロケールファイルがドメインごとに整理されている(モデル、ビューなど)
- ユーザーに表示するすべてのテキストで I18n を使用している
- ビューで遅延検索を使用している(t(".key"))
- 数えられる項目に複数形化を使用している
- 日付/通貨の書式設定がローカライズされている
- ロケールの切り替えが実装されている
- i18n-tasks が設定されている
- テストで翻訳の欠落を検出している
- フォールバックが設定されている
参考資料
- locale-files.md — モデル、ビュー、共有キー、コンポーネント向けの YAML ロケールファイルの例
- usage-patterns.md — ビュー、コントローラー、モデル、プレゼンター、コンポーネントでの使用例と書式設定
- testing.md — 翻訳の網羅性を確認する RSpec スペックと i18n-tasks
コメント
Markdown 対応