webklex/translator

Flexible Laravel translator based on .CSV language files

1.0.5.1 2017-04-25 14:55 UTC

README

Latest Version on Packagist Software License Build Status Total Downloads

This is a translator package provides everything you need to get started with a quiet fast CSV file based translation service.

Install

Via Composer

$ composer require webklex/translator

Setup

Add the service provider to the providers array in config/app.php.

'providers' => [
    Webklex\Translator\Providers\TranslatorServiceProvider::class,
    Webklex\Translator\Providers\TranslatorBladeServiceProvider::class,
];

Middleware

You may want to use the middleware in order to control the global language setup inside app/Http/Kernel.php.

protected $routeMiddleware = [
    'translator' => Webklex\Translator\Middleware\TranslatorMiddleware::class,
];

Routes and language switching

If you want to change the system language by clicking on a link, you could use something like this:

Inside your controller:

/**
 * Change the current language
 *
 * @param string $locale
 * @return \Illuminate\Http\RedirectResponse
 */
public function changeLanguage($locale){
    if(in_array($locale, config('translator.available'))){
        Session::put('locale', $locale);
        Session::save();
        app()->setLocale($locale);
    }

    return redirect()->back();
}

Inside your routing file:

Route::get('/language/{locale}', 'YourControllerName@changeLanguage');

Publishing

You can publish everything at once

php artisan vendor:publish --provider="Webklex\Translator\Providers\TranslatorServiceProvider"

or you can publish groups individually.

php artisan vendor:publish --provider="Webklex\Translator\Providers\TranslatorServiceProvider" --tag="config"

Usage

This is a translator package provides everything you need to get started with a quiet fast CSV file based translation service. Your translation files will be stored by default in resources/lang/ your language code (e.g. en) /default.csv.

Access Translator by its Facade (Webklex\Translator\Facades\TranslatorFacade). Therefor you might want to add an alias to the aliases array within the config/app.php file.

'aliases' => [
    'Lang' => Webklex\Translator\Facades\TranslatorFacade::class
];

You registered the TranslatorBladeServiceProvider you can even use this easy shorthand directive.

@t('My translation')
@t('My translation', 'en')

If you are using something like my other package webklex/helpers you can use a helper function to make the access even easier.

Therefor create a new helper: php artisan make:helper translator and edit the app/Helpers/translator.php.

if (!function_exists('_t')) {

    /**
     * Shorthand translation
     * @param string $string
     * @param string $locale
     *
     * @return string
     */
    function _t($string, $locale = null)
    {
        return Webklex\Translator\Facades\TranslatorFacade::get($string, $locale);
    }
}

Change log

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

Security

If you discover any security related issues, please email github@webklex.com instead of using the issue tracker.

Credits

License

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