LoginSignup
0
3

More than 3 years have passed since last update.

【Laravel 5.4】validationのロケールで、テーブルのフィールド名を日本語訳する方法

Last updated at Posted at 2017-06-08

まずバリデーションエラーを日本語化しておく

resources/lang/en を ja にコピーして中味を日本語訳するか
ここら辺(Laravelの日本語レポジトリの作成とか)から、一旦 5.3 用を入れて差分修正しておく。
config/app.php を編集して、ja のロケール設定もしておく。

フィールド名の定義

attributes に記述する。

resources/lang/ja/validation.php
    'attributes' => [
        'name' => '名前',
        'email' => 'eメール',
    ],

フィールド名を変換する

バリデーションエラーは勝手に変換してくれるけど、テーブルのフィールド名も定義した attributes の日本語訳で表示したい。
ローカライズは trans 関数を使う。
validation.php の attributes にある email を指定する場合は以下のようなキーを指定する。

trans('validation.attributes.email');

対応する訳があれば、その値(「eメール」とか)が返る。
訳がなければキーの値がそのまま('validation.attributes.email')返る。

例えばこんな関数を作ってラベルをまとめて日本語化したりすると
table にテーブル名を渡しただけで自動でテーブル一覧を表示したりして

TableController.php
    /**
     * フィールド名配列の内容をまとめて日本語化
     * @param array $fields フィールド名一覧
     * @return array 日本語化されたラベル
     */
    private function trans($fields) {
        $label = [];
        foreach ($fields as $field) {
            $key = 'validation.attributes.' . $field; // キーを作成する
            $value = trans($key);
            $label[$field] = ($key == $value) ? $field : $value;
        }
        return $label;
    }

    /**
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $table = $request->input('table', null);
        $title = 'テーブル ' . $table;
        $data = DB::table($table)->get();
        if ($data) {                                // データがあれば
            $fields = array_keys((array) $data[0]); // 最初のオブジェクトを配列に変換してキーを取得
        } else {
            $fields = null;
        }
        $label = $this->trans($fields);
        return view('table')->with([
            'title'  => $title,  // タイトル
            'fields' => $fields, // フィールド一覧
            'label'  => $label,  // 表示ラベル
            'data'   => $data,   // 表示データ
        ]);
    }

あとは、こんな感じでループを回せばテーブル一覧が表示できる。

table.blade.php
           :
@if ($data)
    @forelse ($data as $elem)
        @if ($loop->first)
        <table class="table table-bordered">
            <thead>
                <tr>
                @foreach ($fields as $field)
                    <th>{{ $label[$field] }}</th>
                @endforeach
                </tr>
            </thead>
            <tbody>
        @endif
                <tr>
                @foreach ($fields as $field)
                    <td>{{ $elem->$field }}</td>
                @endforeach
                </tr>
        @if ($loop->last)
            </tbody>
        </table>
        @endif
    @empty
    <p>データなし</a>
    @endforelse
@else
    <p>データなし</a>
@endif
           :

data が null なら「データなし」と表示。
data が一件もなければ「データなし」と表示。
ループの一回目にタイトル行を表示
ループの最後にテーブルのクローズタグを表示。
あとは data 行分、fields のカラム分を表示。

- 目次 -

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