aayaresko/laravel-language

Multilingual routes and language switcher for laravel 5

dev-master 2016-09-29 15:32 UTC

This package is not auto-updated.

Last update: 2020-11-13 22:24:14 UTC


README

Multilingual routes and language switcher for laravel 5

Installation

The preferred way to install extension is via composer. Check the composer.json for this extension's requirements and dependencies.

To install, either run

$ php composer.phar require aayaresko/laravel-language "*"

or add

"aayaresko/laravel-language": "*"

to the require section of your composer.json.

Configuration

After installing the Socialite library, register the aayaresko\language\ServiceProvider in your config/app.php configuration file:

'providers' => [
    // Other service providers...
    aayaresko\language\ServiceProvider::class,
],

Also, add the Language facade to the aliases array in your app configuration file:

'Language' => aayaresko\language\LanguageFacade::class,

Usage

Use Language::getLocale() method to add language prefix to your routes:

Route::group(['prefix' => Language::getLocale()], function () { 
    Route::get('/home', function () {
        return view('frontend.index');
    })->name('home');
});

Use Language::renderDropdownList() in your view-file to generate language dropdown list (note a exclamation marks):

{!! Language::renderDropdownList() !!}

This method takes 'locales' array which specified in you app.config file. It assumes that each 'locale' item key is a language 'code' and value is a 'visible_name'. You can control language item label content via Language::renderDropdownList() $label_template value:

{!! Language::renderDropdownList('{visible_name} ({code})') !!}

Label will look like this: 'English (en)' You can add additional items to any 'locale' item value and render that value in Language::renderDropdownList(). For example, if you added 'description' and your app.config looks something like this:

'locales' => [
    'en' => [
        'visible_name' => 'English',
        'description' => 'Some simple text'
    ], 
    'ru' => 'Русский',
]

You can render 'description' item:

{!! Language::renderDropdownList('{visible_name} ({code}) {description}') !!}

Label will look like this: 'English (en) Some simple text'

Customization

You can customize html of language dropdown list. Simply run php ./artisan vendor:publish --tag=language and edit dropdown.blade.php template in your resources/views/vendor/language directory.