
【Rails】gemを使わない日本語化の設定
Railsを使っていて、日本語で出てほしいところが英語になっちゃう時ってありませんか?
バリデーションエラーの際に結構イライラしますよね😱
今回はgemも使わずに最低限の日本語化になることを目指します!
はじめに
そもそもRailsは外国人の方が作成しているので、デフォルトだと英語になってしまいます...(Rubyは日本人の方が作ったんですけどね🙄)
なので、日本語の辞書みたいなものを入れて読み込ませてやる必要があります。
動作環境
| パソコン | macOS (Intel, BigSur) |
|---|---|
| Ruby | 2.7.4 |
| Rails | 6.1.4 |
バージョンは参考程度です。基本的にはどのバージョンでも動くと思います!
日本語化の仕組み
Railsの国際化(i18n)機能を使うことで、アプリケーション内のテキストを多言語対応できます。
日本語化には以下の2つの設定が必要です。
- 日本語翻訳ファイル(ja.yml)の配置 - エラーメッセージや日付フォーマットなどの翻訳定義
- デフォルトロケールの設定 - アプリケーション全体で日本語を使用する設定
詳しくはRails 国際化(I18n)APIを参照してください。
手順1: ja.ymlファイルの作成
まずは日本語の翻訳ファイル(ja.yml)をダウンロードします。
rails-i18nリポジトリから公式の翻訳ファイルを取得できます。
cd [Railsプロジェクトのディレクトリ]
curl -o ./config/locales/ja.yml https://raw.githubusercontent.com/svenfuchs/rails-i18n/master/rails/locale/ja.yml上記のcurlコマンドでconfig/localesディレクトリにja.ymlが作成されます。
ファイルの中身は下記のようになっていたらOKです。
---
ja:
activerecord:
errors:
messages:
record_invalid: "バリデーションに失敗しました: %{errors}"
restrict_dependent_destroy:
has_one: "%{record}が存在しているので削除できません"
has_many: "%{record}が存在しているので削除できません"
date:
abbr_day_names:
- 日
- 月
- 火
- 水
- 木
- 金
- 土
abbr_month_names:
-
- 1月
- 2月
- 3月
- 4月
- 5月
- 6月
- 7月
- 8月
- 9月
- 10月
- 11月
- 12月
day_names:
- 日曜日
- 月曜日
- 火曜日
- 水曜日
- 木曜日
- 金曜日
- 土曜日
formats:
default: "%Y/%m/%d"
long: "%Y年%m月%d日(%a)"
short: "%m/%d"
month_names:
-
- 1月
- 2月
- 3月
- 4月
- 5月
- 6月
- 7月
- 8月
- 9月
- 10月
- 11月
- 12月
order:
- :year
- :month
- :day
datetime:
distance_in_words:
about_x_hours:
one: 約1時間
other: 約%{count}時間
about_x_months:
one: 約1ヶ月
other: 約%{count}ヶ月
about_x_years:
one: 約1年
other: 約%{count}年
almost_x_years:
one: 1年弱
other: "%{count}年弱"
half_a_minute: 30秒前後
less_than_x_seconds:
one: 1秒以内
other: "%{count}秒未満"
less_than_x_minutes:
one: 1分以内
other: "%{count}分未満"
over_x_years:
one: 1年以上
other: "%{count}年以上"
x_seconds:
one: 1秒
other: "%{count}秒"
x_minutes:
one: 1分
other: "%{count}分"
x_days:
one: 1日
other: "%{count}日"
x_months:
one: 1ヶ月
other: "%{count}ヶ月"
x_years:
one: 1年
other: "%{count}年"
prompts:
second: 秒
minute: 分
hour: 時
day: 日
month: 月
year: 年
errors:
format: "%{attribute}%{message}"
messages:
accepted: を受諾してください
blank: を入力してください
confirmation: と%{attribute}の入力が一致しません
empty: を入力してください
equal_to: は%{count}にしてください
even: は偶数にしてください
exclusion: は予約されています
greater_than: は%{count}より大きい値にしてください
greater_than_or_equal_to: は%{count}以上の値にしてください
inclusion: は一覧にありません
invalid: は不正な値です
less_than: は%{count}より小さい値にしてください
less_than_or_equal_to: は%{count}以下の値にしてください
model_invalid: "バリデーションに失敗しました: %{errors}"
not_a_number: は数値で入力してください
not_an_integer: は整数で入力してください
odd: は奇数にしてください
other_than: は%{count}以外の値にしてください
present: は入力しないでください
required: を入力してください
taken: はすでに存在します
too_long: は%{count}文字以内で入力してください
too_short: は%{count}文字以上で入力してください
wrong_length: は%{count}文字で入力してください
template:
body: 次の項目を確認してください
header:
one: "%{model}にエラーが発生しました"
other: "%{model}に%{count}個のエラーが発生しました"
helpers:
select:
prompt: 選択してください
submit:
create: 登録する
submit: 保存する
update: 更新する
number:
currency:
format:
delimiter: ","
format: "%n%u"
precision: 0
separator: "."
significant: false
strip_insignificant_zeros: false
unit: 円
format:
delimiter: ","
precision: 3
separator: "."
significant: false
strip_insignificant_zeros: false
human:
decimal_units:
format: "%n %u"
units:
billion: 十億
million: 百万
quadrillion: 千兆
thousand: 千
trillion: 兆
unit: ""
format:
delimiter: ""
precision: 3
significant: true
strip_insignificant_zeros: true
storage_units:
format: "%n%u"
units:
byte: バイト
eb: EB
gb: GB
kb: KB
mb: MB
pb: PB
tb: TB
percentage:
format:
delimiter: ""
format: "%n%"
precision:
format:
delimiter: ""
support:
array:
last_word_connector: "、"
two_words_connector: "、"
words_connector: "、"
time:
am: 午前
formats:
default: "%Y年%m月%d日(%a) %H時%M分%S秒 %z"
long: "%Y/%m/%d %H:%M"
short: "%m/%d %H:%M"
pm: 午後上記ファイルが日本語へ変換してくれる辞書的な役割ですね!
手順2: config/application.rbの設定
次に、config/application.rbを編集してデフォルトロケールを日本語に設定します。
module MyApp
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 6.1
config.time_zone = "Tokyo"
config.active_record.default_timezone = :local
config.i18n.default_locale = :ja
# Don't generate system test files.
config.generators.system_tests = nil
end
end6〜8行目を追加してください。これで基本的なエラーメッセージなどが日本語になります。
手順3: モデル・カラムの日本語化
一般的な文章は日本語になりましたが、バリデーションエラーのモデル名やカラム名はまだ英語のままです。
これらも日本語にするには、ja.ymlにモデルとカラムの翻訳を追記します。
例えば、Userモデルがname(氏名)、email(メールアドレス)、tel_number(電話番号)を持つ場合:
---
ja:
activerecord:
errors:
# ...省略...
models:
user: ユーザー
attributes:
user:
name: 氏名
email: メールアドレス
tel_number: 電話番号
date:
# 以下略6〜12行目のように、models:でモデル名の翻訳を、attributes:でカラム名の翻訳を定義します。
以上でほとんど日本語化されています🥳
まとめ
今回はgemを使わずにRailsの日本語化を行う方法を紹介しました。
- ja.ymlファイルの作成 -
rails-i18nリポジトリから翻訳ファイルを取得 - application.rbの設定 - デフォルトロケールを
:jaに設定 - モデル・カラムの翻訳追加 - 必要に応じてja.ymlに追記
Railsの勉強をしたい方は、この本が参考になるかと思いますので、興味がある方はぜひ読んでみてください💪
今回の記事を参考に、日本人に優しいRailsライフを送ってください!

