Во всяком случае, я использую другой пакет локализации под названием laravel-vue-i18n.
Мне удалось использовать это на Vue.js, но у меня возникли проблемы при настройке локали на основе URL-адреса. Как настроить мои маршруты/промежуточное программное обеспечение на использование локали, допускающей значение NULL (по умолчанию en)?
Файл web.php
Код: Выделить всё
// Can be nullable locale?
Route::middleware(['setLocale'])->prefix('{locale?}')->group(function () {
Route::resource('posts', PostController::class);
Route::resource('comments', CommentController::class);
});
Код: Выделить всё
class SetLocaleMiddleware
{
public function handle($request, Closure $next, $locale = 'en')
{
\Log::info($locale); // Always logs as 'en' even if I add 'ja' in the URL
\Log::info($request->route('locale')); // Locale or whatever is the text after localhost/ for some reason
if (!in_array($locale, ['en', 'ja'])) {
abort(400);
}
App::setLocale($locale);
return $next($request);
}
}
Код: Выделить всё
protected $middlewareAliases = [
'setLocale' => \App\Http\Middleware\SetLocaleMiddleware::class,
];
Код: Выделить всё
// Set application language to Japanese
http://localhost/ja
http://localhost/ja/posts
http://localhost/ja/comments
// Set application language to English as default
http://localhost
http://localhost/posts
http://localhost/comments
Подробнее здесь: https://stackoverflow.com/questions/777 ... rtia-vuejs
Мобильная версия