GithubHelp home page GithubHelp logo

youneselbarnoussi / nova-locale-switcher Goto Github PK

View Code? Open in Web Editor NEW

This project forked from anditsung/nova-locale-switcher

0.0 0.0 0.0 79 KB

A simple locale switcher for Nova without any concrete localization implementation

License: MIT License

JavaScript 8.78% PHP 41.03% Vue 50.19%

nova-locale-switcher's Introduction

Laravel Nova Locale Switcher Tool

This tool provides a simple header dropdown to quickly switch between locales. This tool does not have any concrete implementation, so is up to you of what happens when a locale is selected from the dropdown.

Nova Locale Switcher

Installation

composer require eolica/nova-locale-switcher

If you were using our tramuntana-studio/nova-locale-switcher package, please modify it accordingly.

Usage

We will demonstrate how tu use this tool with a simple example. First, you must register the tool in NovaServiceProvider under tools.

use Illuminate\Http\Request;

public function tools()
{
    return [
        \Eolica\NovaLocaleSwitcher\LocaleSwitcher::make()
            ->setLocales(config('nova.locales'))
            ->onSwitchLocale(function (Request $request) {
                $locale = $request->post('locale');

                if (array_key_exists($locale, config('nova.locales'))) {
                    $request->user()->update(['locale' => $locale]);
                }
            }),
    ];
}

The setLocales method expects an associative array, the key being the locale code and the value the name of the locale. In this example, we set the locales from de nova.php config file:

return [

    // Rest of Nova configuration

    ...

    /*
    |--------------------------------------------------------------------------
    | Nova Locales
    |--------------------------------------------------------------------------
    */

    'locales' => [
        'en' => 'English',
        'de' => 'Deutsch',
        'es' => 'Español',
    ],
];

For handling the locale switching we must pass a callable to the onSwitchLocale method, in this example we update the current user locale field (this is a custom field that we added to the model) with the locale that we receive from the Request object.

Now, for setting the application locale we can do it inside the boot method of the NovaServiceProvider by using the Nova::serving() method:

use Laravel\Nova\Events\ServingNova;

final class NovaServiceProvider extends NovaApplicationServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        parent::boot();

        Nova::serving(function (ServingNova $event) {
            $user = $event->request->user();

            if (array_key_exists($user->locale, config('nova.locales'))) {
                app()->setLocale($user->locale);
            }
        });
    }

    ...
}

The last step is to display the language selector. For that we must publish Nova's views executing the next command:

php artisan nova:publish

Now we go to the file /resources/views/vendor/nova/layout.blade.php and right after the dropdown that includes the user's partial we add the locale-switcher Vue component:

<!-- Content -->
<div>
    ...

    <dropdown class="ml-auto h-9 flex items-center dropdown-right">
        @include('nova::partials.user')
    </dropdown>

    <!-- HERE! -->
    <locale-switcher></locale-switcher>
</div>

And that should be it!

This is one of many solutions you can use, the important thing is that you can switch to another type of implementation without depending on an external package.

Changelog

Please see CHANGELOG for more information what has changed recently.

Security

If you discover a security vulnerability within this package, please send an email at [email protected] instead of using the issue tracker.

License

The MIT License (MIT). Please see License File for more information.

nova-locale-switcher's People

Contributors

youneselbarnoussi avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.