Language switcher UI for your Laravel App

Are your admins asking for multi-language support? Do they want a language switcher, to enjoy using their dashboard in their preferred...

Karan Datwani
Karan Datwani
Share:

Are your admins asking for multi-language support? Do they want a language switcher, to enjoy using their dashboard in their preferred language? Last question! Have you ever noticed a language switcher in our demo?

Yes, we have it as a free package - it's been available for a couple of months. And you can use it site-wide as well! Not only on the Backpack admin panel.

image

How to add a language switcher to your admin panel?

  1. Usual package installation
composer require backpack/language-switcher
  1. Add its middleware to backpack config config/backpack/base.php:
'middleware_class' => [
    ...
    \Backpack\LanguageSwitcher\Http\Middleware\LanguageSwitcherMiddleware::class,
],

Optionally, you may add the middleware to the web, api or other middleware groups where you may want to use the language switcher in app/Http/Kernel.php.

  1. Add the dropdown view to topbar_right_content.blade.php or wherever you need it:
// resources/views/vendor/backpack/theme-tabler/inc/topbar_right_content.blade.php
@include('backpack.language-switcher::language-switcher')
  1. To add the available languages of your app, you'll need to enable them in the Backpack CRUD config file config/backpack/crud.php
'locales' => [
    'en' => 'English',
    'pt' => 'Portuguese',
    'ro' => 'Romanian',
    ...

There you can set the array of locales your app uses. Keep in mind the default locale of your app should remain in config.app.locale and config.app.fallback_locale.

Done! It's placed & working in your admin panel.

How to add a language switcher to your main website?

If you wish to use the language switcher on other parts of your app, add the middleware in app/Http/Kernel.php. For instance, you can enable this for the whole web middleware group, or the api.

protected $middlewareGroups = [
    'web' => [
        ...
        \Backpack\LanguageSwitcher\Http\Middleware\LanguageSwitcherMiddleware::class,
    ],

Conclusion

Here, You discovered a language switcher package for your Laravel app. Use it in the Backpack admin panel or your website front, 5 minutes of effort is enough to understand and use. Thanks for reading.

Happy Language Switching✌️😎

Want to receive more articles like this?

Subscribe to our "Article Digest". We'll send you a list of the new articles, every week, month or quarter - your choice.

Reactions & Comments

What do you think about this?

Latest Articles

Wondering what our community has been up to?