Enable language switching for guest users

Changes:
- Move locale.update route outside auth middleware
- Update LocaleController to support both authenticated and guest users
  - Guest users: Save locale preference to session only
  - Authenticated users: Save to both session and database
- Add language switcher dropdown to header for all users
  - Display current language with globe icon
  - Show all 8 supported languages in dropdown
  - Highlight currently selected language with checkmark

This allows non-logged-in users to change the interface language,
improving accessibility for international visitors.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-11-30 12:30:35 +09:00
parent 5b83936c4b
commit 61d42d79f1
4 changed files with 153 additions and 6 deletions

View File

@@ -12,6 +12,20 @@ Markdown対応のナレッジベースアプリケーションです。Wiki風
- 👥 **ユーザー管理** - 管理者によるユーザーのCRUD操作 - 👥 **ユーザー管理** - 管理者によるユーザーのCRUD操作
- 🔒 **認証** - Laravel Breezeによるログイン/登録機能 - 🔒 **認証** - Laravel Breezeによるログイン/登録機能
- 🎨 **シンタックスハイライト** - コードブロックの自動ハイライト - 🎨 **シンタックスハイライト** - コードブロックの自動ハイライト
- 🌐 **多言語対応** - 8言語サポートユーザーごとに言語設定可能
## 対応言語
| 言語 | コード |
|------|--------|
| English | en |
| 日本語 | ja |
| Deutsch | de |
| Français | fr |
| Español | es |
| 简体中文 | zh-CN |
| 繁體中文 | zh-TW |
| 한국어 | ko |
## 技術スタック ## 技術スタック
@@ -49,6 +63,7 @@ Markdown対応のナレッジベースアプリケーションです。Wiki風
│ │ ├── Livewire/ # Livewireコンポーネント │ │ ├── Livewire/ # Livewireコンポーネント
│ │ ├── Models/ # Eloquentモデル │ │ ├── Models/ # Eloquentモデル
│ │ └── Services/ # ビジネスロジック │ │ └── Services/ # ビジネスロジック
│ ├── lang/ # 言語ファイルi18n
│ ├── resources/views/ │ ├── resources/views/
│ │ ├── layouts/ # レイアウト │ │ ├── layouts/ # レイアウト
│ │ ├── livewire/ # Livewireビュー │ │ ├── livewire/ # Livewireビュー
@@ -165,6 +180,62 @@ DocumentSeederを実行すると以下のドキュメントが作成されます
※ 既存のドキュメントがある場合、DocumentSeederはスキップされます。 ※ 既存のドキュメントがある場合、DocumentSeederはスキップされます。
## 本番環境へのデプロイ
### ⚠️ 重要: サブドメインを使用してください
このアプリケーションは **サブドメイン** でのデプロイを推奨します。
```
✅ 推奨: kb.example.com
❌ 非推奨: example.com/kb (サブディレクトリ)
```
**理由**: Livewire 3はサブディレクトリデプロイに完全対応していません。`/livewire/update` エンドポイントがサブディレクトリを考慮しないため、AJAX通信が失敗します。
### デプロイ手順
1. **サブドメインのDNS設定**
```
kb.example.com → サーバーIPアドレス
```
2. **Webサーバー設定**nginx例
```nginx
server {
listen 80;
server_name kb.example.com;
root /var/www/knowledge-base/src/public;
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
}
```
3. **環境変数の設定**
```env
APP_URL=https://kb.example.com
APP_ENV=production
APP_DEBUG=false
```
4. **本番用の最適化**
```bash
php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan optimize
```
## よく使うコマンド ## よく使うコマンド
```bash ```bash
@@ -181,6 +252,12 @@ docker exec kb_php npm [command]
# コンテナに入る # コンテナに入る
docker exec -it kb_php bash docker exec -it kb_php bash
# キャッシュクリア
docker exec kb_php php artisan config:clear
docker exec kb_php php artisan route:clear
docker exec kb_php php artisan view:clear
docker exec kb_php php artisan cache:clear
``` ```
## 管理者機能 ## 管理者機能
@@ -219,6 +296,15 @@ docker exec -it kb_php bash
[[Laravel/Livewire/Components]] も確認してください。 [[Laravel/Livewire/Components]] も確認してください。
``` ```
## 言語設定
ユーザーは「プロフィール」ページから使用言語を変更できます。
1. 右上のユーザー名をクリック
2. 「プロフィール」を選択
3. 「言語設定」セクションで言語を選択
4. 「保存」をクリック
## トラブルシューティング ## トラブルシューティング
### パーミッションエラー ### パーミッションエラー
@@ -243,6 +329,17 @@ docker compose up -d
`src/.env` の `DB_HOST` が `kb_mysql` になっているか確認してください。 `src/.env` の `DB_HOST` が `kb_mysql` になっているか確認してください。
### Livewireのエラー本番環境
「404 /livewire/update」エラーが出る場合は、サブディレクトリではなくサブドメインでデプロイしてください。
```bash
# キャッシュをクリア
php artisan config:clear
php artisan route:clear
php artisan cache:clear
```
## ライセンス ## ライセンス
MIT License MIT License

View File

@@ -10,6 +10,7 @@ class LocaleController extends Controller
{ {
/** /**
* Update the user's locale preference. * Update the user's locale preference.
* Works for both authenticated and guest users.
*/ */
public function update(Request $request) public function update(Request $request)
{ {
@@ -19,12 +20,14 @@ public function update(Request $request)
$locale = $validated['locale']; $locale = $validated['locale'];
// Save to user record // Save to session (works for both authenticated and guest users)
Auth::user()->update(['locale' => $locale]);
// Also save to session for immediate effect
$request->session()->put('locale', $locale); $request->session()->put('locale', $locale);
return redirect()->route('profile.edit')->with('success', __('messages.settings.language_updated')); // If authenticated, also save to user record for persistence
if (Auth::check()) {
Auth::user()->update(['locale' => $locale]);
}
return redirect()->back()->with('success', __('messages.settings.language_updated'));
} }
} }

View File

@@ -59,6 +59,51 @@ class="inline-flex items-center px-3 py-2 border border-gray-300 shadow-sm text-
</kbd> </kbd>
</button> </button>
<!-- Language Switcher (for all users) -->
<div x-data="{ open: false }" @click.away="open = false" class="relative">
<button
@click="open = !open"
class="flex items-center px-3 py-2 text-sm font-medium text-gray-700 hover:text-gray-900 focus:outline-none"
title="{{ __('messages.settings.change_language') }}"
>
<svg class="w-5 h-5 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 5h12M9 3v2m1.048 9.5A18.022 18.022 0 016.412 9m6.088 9h7M11 21l5-10 5 10M12.751 5C11.783 10.77 8.07 15.61 3 18.129"></path>
</svg>
@php
$currentLocale = app()->getLocale();
$locales = \App\Http\Middleware\SetLocale::SUPPORTED_LOCALES;
@endphp
<span class="hidden sm:inline">{{ $locales[$currentLocale] ?? 'English' }}</span>
<svg class="ml-1 h-4 w-4" fill="currentColor" viewBox="0 0 20 20">
<path fill-rule="evenodd" d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z" clip-rule="evenodd"></path>
</svg>
</button>
<div
x-show="open"
x-transition
class="absolute right-0 mt-2 w-48 bg-white rounded-md shadow-lg py-1 ring-1 ring-black ring-opacity-5 max-h-96 overflow-y-auto"
>
@foreach($locales as $code => $name)
<form method="POST" action="{{ route('locale.update') }}" class="inline-block w-full">
@csrf
<input type="hidden" name="locale" value="{{ $code }}">
<button
type="submit"
class="w-full text-left px-4 py-2 text-sm hover:bg-gray-100 {{ $currentLocale === $code ? 'bg-indigo-50 text-indigo-700 font-semibold' : 'text-gray-700' }}"
>
{{ $name }}
@if($currentLocale === $code)
<svg class="inline-block w-4 h-4 ml-2" fill="currentColor" viewBox="0 0 20 20">
<path fill-rule="evenodd" d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z" clip-rule="evenodd"></path>
</svg>
@endif
</button>
</form>
@endforeach
</div>
</div>
@auth @auth
<!-- User Dropdown --> <!-- User Dropdown -->
<div x-data="{ open: false }" @click.away="open = false" class="relative"> <div x-data="{ open: false }" @click.away="open = false" class="relative">

View File

@@ -22,11 +22,13 @@
return view('dashboard'); return view('dashboard');
})->middleware(['auth', 'verified'])->name('dashboard'); })->middleware(['auth', 'verified'])->name('dashboard');
// Locale switcher - available for all users (both authenticated and guest)
Route::post('/locale', [LocaleController::class, 'update'])->name('locale.update');
Route::middleware('auth')->group(function () { Route::middleware('auth')->group(function () {
Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit'); Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update'); Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy'); Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
Route::post('/locale', [LocaleController::class, 'update'])->name('locale.update');
}); });
// Admin routes // Admin routes