LoginSignup
29
24

More than 5 years have passed since last update.

Rails で他言語対応するために必要なこと

Last updated at Posted at 2014-05-28

#コメントをいただいて更新しました。

セットアップ

下記のgemをインストール。

gem 'rails-i18n'

使い方

各言語ごとの文言を config/locales/xx.yml に記載します。

config/locales/ja.yml
ja:
    view:
        edit: "編集"
        delete: "削除"
        back: "戻る"

画面に表示させる時には、下記のように t("path") で書きます。

index.html.haml
%td= link_to  t('view.edit'), edit_data_path(initial)
%td= link_to  t('view.delete'), data, :method => :delete, :data => { :confirm => 'Are you sure?' }

注意

i18n の gem だけインストールして使っている場合、エラーが起きた時にエラーメッセージが異なることがあります。例えばデフォルト言語を日本語にしていた場合など。

ActiveRecord::RecordInvalid: translation missing: ja.activerecord.errors.messages.record_invalid

このようなエラーメッセージが表示されて何が原因かわからなくなるのでrails-i18nをインストールしておくと便利です。

29
24
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
29
24