LoginSignup
0
0

More than 1 year has passed since last update.

laravel バリデーションエラーメッセージを日本語化する

Posted at

概要

  • laravelのバリデーションエラーメッセージを日本語化する方法を簡単にまとめる。

前提

  • 説明で実行するコマンドは特筆しない限り、前回実行したコマンドと同じディレクトリで実行するものとする。

方法

  1. アプリ名ディレクトリで 下記コマンドを実行して言語ファイルをコピーする。

    $ cp -r resources/lang/en resources/lang/ja
    
  2. アプリ名ディレクトリ/ resources/lang/ja/validation.phpを開く。

  3. 今回は試しに、「必須」と「日にちフォーマット」の2つのバリデーションメッセージを日本語化してみる。

  4. 下記の二箇所の記載を修正した。

    1. 修正前

      resources/lang/ja/validation.php
          'date' => 'The :attribute is not a valid date.',
          'required' => 'The :attribute field is required.',
      
    2. 修正後

      resources/lang/ja/validation.php
          'date' => '日時フォーマットに誤りがあります 正しい値を入力してください',
          'required' => ':attribute は必須です',
      
  5. 同クラスの最下部に記載されているattributesの配列に追記して項目名も日本語化する。

    resources/lang/ja/validation.php
    /*
    |--------------------------------------------------------------------------
    | Custom Validation Attributes
    |--------------------------------------------------------------------------
    |
    | The following language lines are used to swap our attribute placeholder
    | with something more reader friendly such as "E-Mail Address" instead
    | of "email". This simply helps us make our message more expressive.
    |
    */
    
    'attributes' => [
        'bladeから送っているときのnameの値' => '日本語での項目名'
    ],
    
  6. config/app.phpを開きlocateの内容を確認する

    1. 下記のようになっていればOK

      config/app.php
      /*
      |--------------------------------------------------------------------------
      | Application Locale Configuration
      |--------------------------------------------------------------------------
      |
      | The application locale determines the default locale that will be used
      | by the translation service provider. You are free to set this value
      | to any of the locales which will be supported by the application.
      |
      */
      
      'locale' => 'ja',
      
  7. 後はアプリ側でバリデーションエラーが出力されるように操作し、日本語化されていればOK

0
0
1

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