danieldoinov/languageintegration

The package introduces dynamic language change capability for a laravel instance.

dev-master 2017-11-03 11:37 UTC

This package is not auto-updated.

Last update: 2020-01-01 20:36:15 UTC


README

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

Provides functionality to dynamically change a Laravel current locale. Basically makes laravel multilingial from a users stand point.

Install

Via Composer

$ composer require danieldoinov/languageintegration

This package has integrated geo-location that uses the following package

Stevebauman\Location

It should be installed automatically with this one but just in case that it isn't you can install it by running

composer require stevebauman/location

You must add the providers in config/app.php

Stevebauman\Location\LocationServiceProvider::class,
DanielDoinov\LanguageIntegration\LanguageIntegrationServiceProvider::class

You can then publish the configuration files.

php artisan vendor:publish --provider="danieldoinov\LanguageIntegration\LanguageIntegrationProvider"
php artisan vendor:publish --provider="Stevebauman\Location\LocationServiceProvider"

Usage

What the package does is add one route and one middleware that handle the locale change. out of the box it will give you the following options for changing the locale:

http://yourdomain.com/locale/[lang]

Linking to that url will change the applications locale to the one that you specified. The [lang] variable must a valid locale code and must be present in the config/languages.php configuration file. After the change of locale the route will redirect back with any other input present.

You can create links that change the locale dynamically my adding a ?lang=[lang] to any route in you application.

The first time the application starts the middleware will look for a cookie with a preferred locale. If none is found it will geo-locate the user and switch to the configured locale based on country code.

You can configure all of that from config/languages.php as well as cookie name and route format.

Here is the default full configuration:

    'cookie_key' => 'current_locale',     
    'locale' => [
        'en' => 'English',
        'de' => 'Deutsche',
        'es' => 'Español',
        'ru' => 'Русский'
    ],
    'country_code_to_locale' => [
        'US' => 'en',
        'GB' => 'en',
        'CA' => 'en',
        'UM' => 'en',
         //spanish
        'ES' => 'es',
         //german
        'DE' => 'de',
         //Russion
        'RU' => 'ru',
    ],    
    'route' => '/locale/{lang}',
    'route_name' => 'setLocale'

Hope you like it and helps!

Credits

License

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