LoginSignup
11
2

More than 3 years have passed since last update.

【Laravel初心者】Laravel8エラーメッセージの日本語化

Posted at

Laravel初学者です。
オリジナルアプリを作成しているのでその過程を記事にしています。

理解が曖昧なところも多いため、ご指摘等ありましたらご連絡いただければ幸いです。

今回はエラーメッセージの日本語化です。
先人の方々のおかげですごい簡単でした。
他にも方法はたくさんあるみたいですね。

環境

Version
PHP 7.4.14
Laravel 8.24.0
mysql 8.0.23
docker 20.10.2
docker-compose 1.27.4

エラーメッセージファイル作成

エラーメッセージはresources/lang配下のenディレクトリで定義されています。
このenディレクトリを中身そのままコピーします。
ディレクトリ名はjaにしましょう。

スクリーンショット 2021-02-02 19.59.02.png

上記のような配置になると思います。

日本語に翻訳

jaディレクトリ内の4つのファイルを見てみると中身は

resources/lang/ja/validation
<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Validation Language Lines
    |--------------------------------------------------------------------------
    |
    | The following language lines contain the default error messages used by
    | the validator class. Some of these rules have multiple versions such
    | as the size rules. Feel free to tweak each of these messages here.
    |
    */

    'accepted' => 'The :attribute must be accepted.',
    'active_url' => 'The :attribute is not a valid URL.',
    'after' => 'The :attribute must be a date after :date.',
    'after_or_equal' => 'The :attribute must be a date after or equal to :date.',
    'alpha' => 'The :attribute may only contain letters.',
    'alpha_dash' => 'The :attribute may only contain letters, numbers, dashes and underscores.',
    'alpha_num' => 'The :attribute may only contain letters and numbers.',
    'array' => 'The :attribute must be an array.',
    'before' => 'The :attribute must be a date before :date.',
    'before_or_equal' => 'The :attribute must be a date before or equal to :date.',
    'between' => [
        'numeric' => 'The :attribute must be between :min and :max.',
        'file' => 'The :attribute must be between :min and :max kilobytes.',
        'string' => 'The :attribute must be between :min and :max characters.',
        'array' => 'The :attribute must have between :min and :max items.',
],
//省略

こんな感じになっていると思います。

=>の左側がバリデーションのルール、右側が引っかかった場合のメッセージです。

読める人はこれ全部翻訳して直せばいいんですけど量も多いので大変です。

なので有志の方が翻訳してくれているものを使わせて頂きます。
私はこの方のgithubを使わせて頂きました。
ありがとうございます。

githubにアクセスすると

スクリーンショット 2021-02-02 20.46.16.png

上記のように4つのファイルがあると思います。
これを開き

スクリーンショット 2021-02-02 20.48.03.png

このソースコードをコピーし、自分のjaディレクトリのコピーしたファイルと同じ名前のファイルに上書きします。
4つのファイル同じように貼り付けしましょう。

自分でオリジナルの文言にしたい場合は変更してもOKです。

で、この状態だと不完全で
passwordは8文字以上にしてくださいとか
nameは必須ですみたいに中途半端な日本語になります。

なのでvalidation.phpに追記します。

resources/lang/ja/validation.php
//~省略
'attributes' => [], 

上記の部分を

resources/lang/ja/validation.php
'attributes' => [
    'name' => 'ユーザー名',
    'email' => 'メールアドレス',
    'password' => 'パスワード'
],

上記のように変更します。
これはテキストフィールドのname属性を日本語に変えているので、プロジェクトによって変えましょう。
例えばユーザー名じゃなくてニックネームとかにしたかったらそのように編集します。

言語ディレクトリを変更

config/app.phpを開きましょう。

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

上記のlocale

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

上記のようにjaにすることでjaのディレクトリを使う設定に変わります。

以上です。

11
2
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
11
2