PHPackages                             ibakhsh/switch-locale - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Localization &amp; i18n](/categories/localization)
4. /
5. ibakhsh/switch-locale

ActiveLibrary[Localization &amp; i18n](/categories/localization)

ibakhsh/switch-locale
=====================

A Laravel Nova tool to switch the locale

0.4.0(6y ago)18MITVuePHP &gt;=7.1.0

Since Mar 21Pushed 6y agoCompare

[ Source](https://github.com/ibakhsh/switch-locale)[ Packagist](https://packagist.org/packages/ibakhsh/switch-locale)[ RSS](/packages/ibakhsh-switch-locale/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (3)Used By (0)

Laravel Nova Switch Locale Tool
===============================

[](#laravel-nova-switch-locale-tool)

For [Astrotomic's Laravel Translatable](https://docs.astrotomic.info/laravel-translatable/)

This tool provides a nice header dropdown to quickly switch between locales. Locale is cached per user and sets the main app locale.

It also provides a `Language` field which gives you the status of the translation on a specific resource.

**NOTE** If you use *Spatie's Laravel Translatable* then use [Laravel Nova Locale Anywhere Tool](https://github.com/slovenianGooner/locale-anywhere)

Installation
------------

[](#installation)

```
composer require ibakhsh/switch-locale

```

Usage
-----

[](#usage)

To begin using this, you must first register the tool in `NovaServiceProvider` under tools. The tool utilises [Astrotomic's Laravel Translatable](https://docs.astrotomic.info/laravel-translatable/) package.

```
public function tools()
{
    return [
        new SwitchLocale([
            "locales" => [
                "en" => "English",
                "de" => "German"
            ],
            "useFallback" => false,
            "customDetailToolbar" => false //optional
        ])
    ];
}

```

### Dropdown

[](#dropdown)

The package provides a switch for the languages that you have to insert into Nova's layout file. You can do that by overwriting the `layout.blade.php` file and insert it after the user dropdown.

```
…

    @include('nova::partials.user')

…

```

[![](/screens/dropdown.png)](/screens/dropdown.png)

### Resource status field

[](#resource-status-field)

You can then define the field in the resource.

```
use ibakhsh\SwitchLocale\Language;

    Language::make(__('Translation'))

```

[![](/screens/formField.png)](/screens/formField.png)[![](/screens/indexView.png)](/screens/indexView.png)[![](/screens/detailField.png)](/screens/detailField.png)

### Delete translation toolbar button

[](#delete-translation-toolbar-button)

The package provides a CustomDetailToolbar component that you can toggle via configuration. Optionally, you can also only grab the `` from the package and paste it into your own custom detail toolbar.

[![](/screens/toolbar.png)](/screens/toolbar.png)

Language Permissions
--------------------

[](#language-permissions)

If you want to allow users access to only certain languages this tool can also display which langauges they have access to.

[![](/screens/dropdownAccess.png)](/screens/dropdownAccess.png)

### Setup

[](#setup)

In order for this to work the user needs to have a `locale` property that returns an object of locale with boolean values for which the user has access to.

```
// User table migration

$table->string('locale')->default('{}');

```

```
// User Model app/User.php

protected $casts = [
    'email_verified_at' => 'datetime',
    'locale' => 'array'
];

public function allowedLocale() {
    return $this->allowedAllLocale() || $this->locale[app()->getLocale()];
}

public function allowedAllLocale() {
    return $this->isAdmin(); // As an example, admin is allowed all locale
}

```

```
// User Nova Resource app/Nova/User.php

use Laravel\Nova\Fields\BooleanGroup;
use ibakhsh\SwitchLocale\SwitchLocale;
…

    BooleanGroup::make(__('Allowed Locale'), 'locale')->options(SwitchLocale::getLocales()),

```

### Example Usage

[](#example-usage)

```
// PostPolicy
