LoginSignup
3
1

More than 3 years have passed since last update.

【Rails】I18n::InvalidLocaleData ロケールファイルを翻訳できない

Last updated at Posted at 2020-05-06

概要

application.rb
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}').to_s]

複数ロケールファイルを読み込ませる設定を記述しているが複数ロケールファイルを作成するとエラーがでる。

エラーメッセージ

[view]
Image from Gyazo

[log]

development.log
I18n::InvalidLocaleData - can not load translations from /Users/hogehogehoge/hogehoge/hogehogehoge/config/locales/model/ja.yml: #<Psych::SyntaxError: (<unknown>): mapping values are not allowed in this context at line 14 column 13>:

原因

空になってるja.ymlがあったこと。
ymlファイルに何も記述がないとエラー吐くっぽい。
エラー文を見てもymlファイルを認識できないというよりはymlファイルを翻訳できないみたいなニュアンスなのでja:だけ全てのロケールファイルに記述したら直った。

解決しなかったけど他に試したこと

  1. rails-i18nのバージョン固定
    Githubより

    gem 'rails-i18n', '~> 6.0.0' # For 6.0.0 or higher
    gem 'rails-i18n', '~> 5.1' # For 5.0.x, 5.1.x and 5.2.x
    gem 'rails-i18n', '~> 4.0' # For 4.0.x
    gem 'rails-i18n', '~> 3.0' # For 3.x
    gem 'rails-i18n', github: 'svenfuchs/rails-i18n', branch: 'master' # For 5.x
    gem 'rails-i18n', github: 'svenfuchs/rails-i18n', branch: 'rails-4-x' # For 4.x
    gem 'rails-i18n', github: 'svenfuchs/rails-i18n', branch: 'rails-3-x' # For 3.x

  2. springの再起動
    参考記事

  3. YAML記法の見直し

ディレクトリ名やインデントの数を見直す

追記

1 記法

ja.yml
ja:
  activerecord:
    models:
      user: ユーザー
      player: 選手
    attributes:
      user: 選手
        twitter_id: Twitter ID
        email: メールアドレス
        screen_name: 名前
        profile_image_url: アバター
        notification: メール通知

attributesの直データを間違って翻訳させようとすると同じエラー + syntaxエラー出る。

NG

attributes:
      user: 選手

OK

attributes:
      user:

2 初期化

ロケールファイルを新規作成した場合はサーバーを再起動させること

3
1
0

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
3
1