LoginSignup
41

More than 5 years have passed since last update.

Laravelのデフォルトログイン処理でエラーメッセージを日本語にしたい

Last updated at Posted at 2015-11-16

Laravel5を使っていて、デフォルトのログイン処理を使ってみたとき日本語化するに躓いたのでメモ

まず準備

本題じゃないのでだいぶ省略しますが
https://github.com/laravel/laravel
ここからapp/Http/routes.phpやresources/views/配下を拝借すればこんな感じになりますね。

スクリーンショット 2015-11-17 1.43.28.png

このLoginボタンを押下すると
app/Http/Controllers/Auth/AuthControllerのpostLoginアクションが呼ばれます。

デフォルトではuseしているAuthenticatesAndRegistersUsersトレイトが
postLoginアクションを実装しているので、何も書かなくてもバリデーションしたり認証したり、連続失敗時にしばらくログイン不可にするとかやってくれるようになります。

よし日本語化だ

ここまで分かったらviewはオリジナルなデザインに変えればよくって、
キレイに言語ファイルで変更したい場合はresources/lang/enをコピーしてjaを作り、config/app.phpにてロケールを指定します。

config/app.php
    'locale' => 'ja',

がしかし、単純にそれだけでは下図のように「The email field is required.」や「The password field is required.」は変わらないんですね。

スクリーンショット 2015-11-17 1.56.13.png

そうなんす。これってpostLoginアクション内で呼ばれているバリデーション処理が動的に組み立ててるメッセージなもんで
emailとかpasswordとかってテキストフォームのnameをそのまま使ってるし、、
最初は「もしかしてこれって変更したい場合は自前でログイン処理書けってことなのかー」って思ったりしました。

ちゃんと変更する方法あった。

resources/lang/ja/validation.php(enからjaにコピーしたやつ)内のcustomで指定した場合はそのメッセージが優先されるようになってた。
というわけで

resources/lang/ja/validation.php
    /*
    |--------------------------------------------------------------------------
    | Custom Validation Language Lines
    |--------------------------------------------------------------------------
    |
    | Here you may specify custom validation messages for attributes using the
    | convention "attribute.rule" to name the lines. This makes it quick to
    | specify a specific custom language line for a given attribute rule.
    |
    */

    'custom' => [
        'attribute-name' => [
            'rule-name' => 'custom-message',
        ],
        'email' => [
            'required' => 'メールアドレスを入力してください',
        ],
        'password' => [
            'required' => 'パスワードを入力してください',
        ],

スクリーンショット 2015-11-17 1.58.15.png

ふー 分かってしまえばどうってことなかった
(よくみたら英語で丁寧に説明書いてあるじゃんね。なんなら例まであるし)

もう少しDRYに(追記)

同僚に教えてもらったので追記します。
下記のように:attributeを使うことでパラメータ名も切り出す事が出来ました。めもめも

resources/lang/ja/validation.php

    'custom' => [
        'attribute-name' => [
            'rule-name' => 'custom-message',
        ],
        'email' => [
            'required' => ':attributeを入力してください',
        ],
        'password' => [
            'required' => ':attributeを入力してください',
        ],

    /*
    |--------------------------------------------------------------------------
    | Custom Validation Attributes
    |--------------------------------------------------------------------------
    |
    | The following language lines are used to swap attribute place-holders
    | with something more reader friendly such as E-Mail Address instead
    | of "email". This simply helps us make messages a little cleaner.
    |
    */

    'attributes' => [
        'email' => 'メールアドレス',
        'password' => 'パスワード',
    ],

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
41