JamesPlayer / laravel-js-localization

Simple, ease-to-use and flexible package for the Laravel web framework. Allows you to use localized messages of the Laravel webapp (see `resources/lang` directory) in your Javascript code.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

laravel-js-localization

Build Status Scrutinizer Code Quality Code Coverage Total Downloads

Simple, ease-to-use and flexible package for the Laravel web framework. Allows you to use localized messages of the Laravel webapp (see resources/lang directory) in your Javascript code. You may easily configure which messages you need to export.

Installation

Add the following line to the require section of your Laravel webapp's composer.json file:

    "require": {
        "andywer/js-localization": "dev-laravel-5"    // "dev-laravel-4.1", "dev-laravel-4.2" for Laravel 4
    }

Run composer update to install the package.

Finally add the following line to the providers array of your app/config/app.php file:

    'providers' => [
        /* ... */
        'JsLocalization\JsLocalizationServiceProvider'
    ]

Branches

Use the following branches according to the Laravel framework version you are using:

  • Laravel 4.0: laravel-4.0
  • Laravel 4.1: laravel-4.1
  • Laravel 4.2: laravel-4.2
  • Laravel 5.x: laravel-5

So use the appropriate branch in your composer.json file as shown by Installation. For example: Use dev-laravel-4.2 for Laravel 4.2.

Configuration

Run php artisan vendor:publish first. This command copies the package's default configuration to config/js-localization.php.

You may now edit this file to define the messages you need in your Javascript code. Just edit the messages array in the config file.

Example (exports all reminder messages):

<?php

return array(
    // Set the locales you use
    'locales' => ['en'],

    // Set the keys of the messages you want to use in javascript
    'messages' => [
        'reminder' => [
            'password', 'user', 'token'
        ]
    ]

    /*
     * in short:
     * 'messages' => ['reminder']
     *
     *
     * you could also use:
     *
     * 'messages' => [
     *     'reminder.password',
     *     'reminder.user',
     *     'reminder.token'
     * ]
     */
);

Important:

The messages configuration will be cached when the JsLocalizationController is used for the first time. After changing the messages configuration you will need to call php artisan js-localization:refresh to refresh that cache.

Usage

You just need to add the neccessary <script> tags to your layout. Here is an example blade view:

@include('js-localization::head')
<!doctype html>
<html lang="en">
    <head>
        <title>Test view</title>
        @yield('js-localization.head')
    </head>
    <body>
        <p>
            Here comes a translated message:
            <script type="text/javascript">
                document.write( Lang.get('reminder.user') );
            </script>
        </p>
    </body>
</html>

Features

You may use Lang.get(), Lang.has(), Lang.choice(), Lang.locale() and trans() (alias for Lang.get()) in your Javascript code. They work just like Laravel's Lang facade.

Variables in messages are supported. For instance: "This is my test string for :name.".

Pluralization is also supported, but does not care about the locale. It only uses the English pluralization rule ("singular text|plural text"). More complex pluralization quantifiers are not yet supported.

Service providers

Assume you are developing a laravel package that depends on this javascript localization features and you want to configure which messages of your package have to be visible to the JS code.

Fortunately that's pretty easy. Just listen to the JsLocalization.registerMessages event and use the JsLocalization\Facades\JsLocalizationHelper::addMessagesToExport() method. Like so:

<?php

use Illuminate\Support\ServiceProvider;
use JsLocalization\Facades\JsLocalizationHelper;

class MyServiceProvider extends ServiceProvider
{
    /* ... */

    public function register()
    {
        Event::listen('JsLocalization.registerMessages', function()
        {
            JsLocalizationHelper::addMessagesToExport(array(
                // list the keys of the messages here, similar
                // to the 'messages' array in the config file
            ));
        });
    }

    /* ... */
}

License

This software is released under the MIT license. See license.

About

Simple, ease-to-use and flexible package for the Laravel web framework. Allows you to use localized messages of the Laravel webapp (see `resources/lang` directory) in your Javascript code.

License:MIT License


Languages

Language:PHP 85.6%Language:JavaScript 14.4%