Switch the app locale from Vue

Set the locale from an allow-list. A query string is not a locale until you have checked it.

For your AI agent

Copies a prompt. Paste it into the AI agent you already have open.

Read https://laravel-vuejs.space/laravel-app-locale-vue.md
Set the locale from an allow-list. A query string is not a locale until you have checked it.
Do not pass the raw query string into setLocale.
Do not store the locale only in localStorage.
Show the diff. Do not commit.
On this page · 3 sections
  1. The fix
  2. What to avoid
  3. FAQ

People searching “Laravel locale Vue” call App::setLocale($request->query('locale')) with no list. The i18n guide is the Vue strings. This page is the one line that picks the language for Laravel’s own translator.

TL;DR

  • Set the locale from an allow-list.
  • A query string is not a locale until you have checked it.
  • Do not pass the raw query string into setLocale.
  • Do not store the locale only in localStorage.

Who it's for: Laravel + Vue developers

Applies to: Laravel 13 · Vue 3

The fix#

php
$locale = $request->string('locale')->toString();

if (in_array($locale, ['en', 'fr', 'ar'], true)) {
    $request->user()->update(['locale' => $locale]);
    App::setLocale($locale);
}

Share the current locale if the layout needs it. Validation messages then use that language on the next response.

What to avoid#

Gotcha

Do not pass the raw query string into setLocale. An unknown locale falls through to odd file lookups. Do not store the locale only in localStorage. The server renders the flash and the mail, and it cannot see that key.

FAQ#

How do I switch the app locale from Vue?

Set the locale from an allow-list. A query string is not a locale until you have checked it.

What goes wrong with Laravel locale Vue?

Many developers call App::setLocale($request->query('locale')) with no list. Do not pass the raw query string into setLocale.

Found this useful?

Ask questions, share what you built.

Join thousands of Laravel and Vue developers in our Facebook group, or get new posts in your Telegram feed. Sharing this article helps too.

Keep reading

Newsletter

Get the best of Laravel & Vue, once a month.

One short email with new guides, release notes worth reading and hand-picked community links. No spam, unsubscribe any time.