0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Laravel】ナビゲーションリンクの日本語化

Last updated at Posted at 2025-04-23

何をしたい?

「//views/layouts/navigation.blade.php」内のナビゲーションを日本語化したい。

↓ ゴール
通常画面サイズ時
スクリーンショット 2025-04-23 18.04.21.png

スマホサイズ時
スクリーンショット 2025-04-23 18.05.20.png

手順

/views/profile/edit.blade.php から順に解説していきます。

① 言語のデフォルトを ja に変更

これを設定しておけば、翻訳ファイルの /lang/ja を自動的に読み込みます。

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

② ファイル作成

「/lang/ja/navigation.php」を作成(まだなかったら)。

touch lang/ja/navigation.php

③ 翻訳作成

基本ルールとして、「lang/ja/navigation.php」のkeyは 「小文字」と「_」で作成する。

lang/ja/navigation.php
<?php

return [

    'profile' => 'プロフィール',
    'log_out' => 'ログアウト',
];

④ navigation.php を編集

③で作成した翻訳のkeyを記載する。
そのkeyの前に翻訳が記載されているファイルであるauthをつける。

/resources/views/layouts/navigation.blade.php
# 編集前
<x-dropdown-link :href="route('profile.edit')">
    {{ __('Profile') }}
</x-dropdown-link>
# 編集後
<x-dropdown-link :href="route('profile.edit')">
    {{ __('navigation.profile') }}
</x-dropdown-link>

# 編集前
<x-dropdown-link :href="route('logout')"
        onclick="event.preventDefault();
                    this.closest('form').submit();">
    {{ __('Log Out') }}
</x-dropdown-link>
# 編集後
<x-dropdown-link :href="route('logout')"
        onclick="event.preventDefault();
                    this.closest('form').submit();">
    {{ __('navigation.log_out') }}
</x-dropdown-link>

# 編集前
<x-responsive-nav-link :href="route('profile.edit')">
    {{ __('Profile') }}
</x-responsive-nav-link>
# 編集後
<x-responsive-nav-link :href="route('profile.edit')">
    {{ __('navigation.profile') }}
</x-responsive-nav-link>

# 編集前
<x-responsive-nav-link :href="route('logout')"
        onclick="event.preventDefault();
                    this.closest('form').submit();">
    {{ __('Log Out') }}
</x-responsive-nav-link>
# 編集後
<x-responsive-nav-link :href="route('logout')"
        onclick="event.preventDefault();
                    this.closest('form').submit();">
    {{ __('navigation.log_out') }}
</x-responsive-nav-link>

結果

通常画面サイズ時
スクリーンショット 2025-04-23 18.04.21.png

スマホサイズ時
スクリーンショット 2025-04-23 18.05.20.png

補足: auth認証の日本語化の手順

補足: プロフィールの日本語化の手順

補足:

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?