Krato / locally by Krato1

Laravel 5 User Preferred Localization
17
1
3
Package Data
Maintainer Username: Krato1
Maintainer Contact: mohd.itcs@gmail.com (Mohammed Isa)
Package Create Date: 2016-12-07
Package Last Update: 2016-12-07
Language: PHP
License: MIT
Last Refreshed: 2024-03-26 15:01:50
Package Statistics
Total Downloads: 17
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 1
Total Watchers: 3
Total Forks: 0
Total Open Issues: 0

This is a fork to use removed package. Credits to Mohammed Isa

Laravel 5 User Preferred Localization

This package allows users to set the prefered site localization. Locales are based on ISO 639-1 standard. It automatically switch the site langage to the user preference. All you have to do is install it and set a locale for the user.

Installation

You can install the package via composer

composer require ericlagarda/locally

Then you must configure the service provider by adding this in config/app.php

'providers' => [
  ...
  Smartisan\Locally\LocallyServiceProvider::class,
];

Also, add the middleware to web group in Kernel.php

'web' => [
            ...
            \Smartisan\Locally\Http\Middlewares\LocallyMiddleware::class
        ],

And you must publish the migrations

php artisan vendor:publish --provider="Smartisan\Locally\LocallyServiceProvider" --tag="migrations"

Do not forget to migrate your tables

php artisan migrate

Finally, add the package trait in your User model.

class User
{
    use LocallyTrait;
}

Optionally, you can add a Laravel Facade and alias for it in config/app.php

'aliases' => [
  ...
  'Locally'   => \Smartisan\Locally\Facades\Locally::class
];

Usage

Set User Locale

If given language code is not exists, an exception will be thrown.

$user->setLocale('en');

Get User Prefered Locale

If user local is not set, the system default locale will be returned.

$user->getLocale(); //en

Remove User Prefered Locale

$user->removeLocale();

Supported Locales

An array of supported locales will be returned by scanning resources/lang folder.

Locally::getSupportedLocales();

Get Language Code / Name

Two helper functions to get language code or name

Locally::getLanguageCodeByName('english'); //en
Locally::getLanguageNameByCode('en'); //English

License

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