0
0

More than 1 year has passed since last update.

【初心者向け】翻訳ファイル(YML)編集後から「InvalidLocaleData」のエラーになる

Posted at

■環境

・Windows10(64bit)
・Ruby 2.7.4
・Rails 5.2.6

■翻訳ファイル編集後から「InvalidLocaleData」のエラーになる

状況

Railsアプリケーションを国際化対応(i18n)するために、翻訳ファイルである「ja.yml」と「en.yml」を編集した。
しかし、その後から全ビューが以下のエラーで表示不可になった。

 I18n::InvalidLocaleData in 【コントローラ名】#【アクション名】
 Showing 【ビュー名】 where line #【行番号】 raised:
 can not load translations from 【アプリケーションの絶対パス】/config/locales/en.yml:
 #): did not find expected key while parsing a block mapping at line 【行番号】 column 【列番号】>

原因

ダブルクォーテーション(")で囲んだ文章内にダブルクォーテーションが入っていた

対処

ダブルクォーテーションをバックスラッシュでエスケープ

config/locales/en.yml
    verify_text: "If you want to delete your account, please type "delete"."
    
    verify_text: "If you want to delete your account, please type \"delete\"."

補足

エラー行番号は必ずその行に問題があるという意味ではなく、その行と同じインデント内のどこかに問題があることを意味している。
エラーメッセージだけで問題個所を特定できない場合は、以下のWebサイトを利用し、問題と思われる個所を1行ずつ削除し検証するのが有効である。

また、本事例の他にエラーのよくある原因は以下。
1)インデントを誤っている
本来ならば偶数の半角スペースでインデントするが、奇数になっていたり、数が多すぎたりしている。

en:
 activerecord: ←インデントが半角スペース2個ではなく1個になっている
    attributes:
        user: ←インデントが半角スペース2個ではなく4個になっている
          confirmation_sent_at: 'confirmation_sent_at'
          confirmation_token: 'confirmation_token'
          confirmed_at: 'confirmed_at'
          created_at: 'created_at'

2)シングルクォーテーション(')で囲んだ文章内にシングルクォーテーションが入っている
 (特にDeepLなどで日本語文を英文に翻訳→コピペしていると、don'tやcan'tが頻繁に含まれる)

0
0
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
0
0