backpack/language-switcher

Language Switcher for Backpack

2.0.0 2024-01-17 13:50 UTC

This package is auto-updated.

Last update: 2024-04-17 14:26:38 UTC


README

Latest Version on Packagist Total Downloads The Whole Fruit Manifesto

This package adds a Language Switcher dropdown for projects using Backpack for Laravel.

Preview

265306871-abdb196f-1d41-4b14-9dc5-2ac7c64cc84d.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTMzNjQyOTgsIm5iZiI6MTcxMzM2Mzk5OCwicGF0aCI6Ii8xODM4MTg3LzI2NTMwNjg3MS1hYmRiMTk2Zi0xZDQxLTRiMTQtOWRjNS0yYWM3YzY0Y2M4NGQucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDQxNyUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA0MTdUMTQyNjM4WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9ZTI3ZGY1MTVlNzRjYzAwYTRhNGJjMGIwYTAzYjY0ZTJmY2M2Mzg4ZDUzZmM0ODVmMTI3ODkwYWNjNzUyZmI2YiZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.NqxqQEK9MO_ESjJYhZt9TaUo4X3okQxHoLL1M-SUNVY

Demo

Try it right now, in our online demo.

Installation

  1. In your Laravel project, install this package:
composer require backpack/language-switcher

# optional: publish the config file
php artisan vendor:publish --provider="Backpack\LanguageSwitcher\LanguageSwitcherServiceProvider" --tag="config"
  1. Add the 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:
@include('backpack.language-switcher::language-switcher')
  1. In order 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.

Usage

Can I show/hide the main label or flags?

Yes!
You can do it by sending special arguments to the component:

@include('backpack.language-switcher::LanguageSwitcher', [
    'flags' => true, // true by default, change it to hide flags
    'main_label' => false, // false by default, it may also be a string, for instance "Language"
])

Can I use this package outside of the backpack/admin scope?

Yes!
If you wish to use the language switcher on other parts of your app, you can do it by adding the middleware in app/Http/Kernel.php. You can, for instance, enable this for the whole web middleware group, or the api.

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

Can I customize the endpoint routes ?

Yes! You can do it by publishing the config file php artisan vendor:publish --provider="Backpack\LanguageSwitcher\LanguageSwitcherServiceProvider" --tag="config". There you can totally disable the package route and register your own, or change some behavior related with display urls.

Please take caution to protect the endpoint with throttling or any other security measure if you overwrite the routes. The default package route uses: 'throttle:60,1'

Notes

This package uses outhebox/blade-flags to get the flags representing languages/locales.

If you find any issue with any with it, like a missing or wrong flag, you can report directly to the maintainer.

Security

If you discover any security related issues, please email cristian.tabacitu@backpackforlaravel.com instead of using the issue tracker.

Credits

License

This project was released under MIT License, so you can install it on top of any Backpack & Laravel project. Please see the license file for more information.