2017 © Pedro Peláez
 

library laravel-js-localization

Laravel Localization in JavaScript

image

mariuzzo/laravel-js-localization

Laravel Localization in JavaScript

  • Wednesday, July 4, 2018
  • by rmariuzzo
  • Repository
  • 17 Watchers
  • 250 Stars
  • 235,148 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 97 Forks
  • 10 Open issues
  • 29 Versions
  • 17 % Grown

The README.md

Laravel JS Localization - Convert you Laravel messages and use them in the front-end!

Laravel 5.5 Laravel 4.2 Latest Stable Version Total Downloads License, (*1)

This package convert all your localization messages from your Laravel app to JavaScript with a small library to interact with those messages following a very similar syntax you are familiar with., (*2)

Features

  • Support Laravel 4.2, 5.0, 5.1, 5.2, 5.3, 5.4, 5.5, 6.x, 7.x and 8.x!
  • Includes Lang.js (a thin library highly inspired on Laravel's Translator class).
  • Allow to specify desired lang files to be converted to JS.
  • Lang.js API is based on Laravel's Translator class. No need to learn a whole API.
:star: **Webpack user?** Try the new and shiny [**Laravel localization loader**](https://github.com/rmariuzzo/laravel-localization-loader) for Webpack!

Installation

composer require mariuzzo/laravel-js-localization

In your Laravel app go to config/app.php and add the following service provider:, (*3)

Mariuzzo\LaravelJsLocalization\LaravelJsLocalizationServiceProvider::class

Usage

The Laravel-JS-Localization package provides a command that generate the JavaScript version of all your messages found at: app/lang (Laravel 4) or resources/lang (Laravel 5) directory. The resulting JavaScript file will contain all your messages plus Lang.js (a thin library highly inspired on Laravel's Translator class)., (*4)

Generating JS messages

php artisan lang:js

Specifying a custom target

php artisan lang:js public/assets/dist/lang.dist.js

Compressing the JS file

php artisan lang:js -c

Specifying a custom source folder

php artisan lang:js public/assets/dist/lang.dist.js -s themes/default/lang

Output a JSON file instead.

php artisan lang:js --json

Configuration

First, publish the default package's configuration file running:, (*5)

php artisan vendor:publish --provider="Mariuzzo\LaravelJsLocalization\LaravelJsLocalizationServiceProvider"

The configuration will be published to config/localization-js.php., (*6)

You may edit this file to define the messages you need in your Javascript code. Just edit the messages array in the config file. Empty messages array will include all the language files in build., (*7)

To make only pagination.php and validation.php files to be included in build process:, (*8)

<?php

return [
    'messages' => [
        'pagination',
        'validation',
    ],
];

Using gulp (optional)

Install gulp-shell and then run it directly in your gulpfile.js:, (*9)

var shell = require('gulp-shell');

gulp.task('langjs', shell.task('php artisan lang:js -c public/js/messages.js'));

Using Laravel's elixir (optional)

Before Elixir 4.0:, (*10)

elixir.extend('langjs', function(path) {
    gulp.task('langjs', function() {
        gulp.src('').pipe(shell('php artisan lang:js ' + (path || 'public/js/messages.js')));
    });

    return this.queueTask('langjs');
});

Elixir 4.0+:, (*11)

var Task = elixir.Task;
elixir.extend('langjs', function(path) {
    new Task('langjs', function() {
        gulp.src('').pipe(shell('php artisan lang:js ' + (path || 'public/js/messages.js')));
    });
});

And use it like this:, (*12)

elixir(function(mix) {
    mix.langjs();
});

Using Laravel's Mix with Laravel 5.4+ (optional)

Add "webpack-shell-plugin-next" to package.json's "devDependencies" section., (*13)

Add the following to webpack.mix.js:, (*14)

const WebpackShellPluginNext = require('webpack-shell-plugin-next');

// Add shell command plugin configured to create JavaScript language file
mix.webpackConfig({
    plugins:
    [
        new WebpackShellPluginNext({onBuildStart:['php artisan lang:js --quiet'], onBuildEnd:[]})
    ]
});

Documentation

This is a quick documentation regarding Lang.js (the thin JavaScript library included by Laravel-JS-Localization). The Lang.js (a thin library highly inspired on Laravel's Translator class)., (*15)

💁 Go to Lang.js documentation to see all available methods., (*16)

Getting a message

Lang.get('messages.home');

Getting a message with replacements

Lang.get('messages.welcome', { name: 'Joe' });

Changing the locale

Lang.setLocale('es');

Checking if a message key exists

Lang.has('messages.foo');

Support for singular and plural message based on a count

Lang.choice('messages.apples', 10);

Calling the choice method with replacements

Lang.choice('messages.apples', 10, { name: 'Joe' });

💁 Go to Lang.js documentation to see all available methods., (*17)

Want to contribute?

  1. Fork this repository and clone it.
  2. Create a feature branch from develop: git checkout develop; git checkout -b feature-foo.
  3. Push your commits and create a pull request.

Prerequisites:

You will need to have installed the following softwares., (*18)

  • Composer.
  • PHP 5.5+.

Development setup

After getting all the required softwares you may run the following commands to get everything ready:, (*19)

  1. Install PHP dependencies:, (*20)

    composer install
    
  2. Install test dependencies:, (*21)

    composer test-install
    

Now you are good to go! Happy coding!, (*22)

Testing

This project uses PHPUnit. All tests are stored at tests directory. To run all tests type in your terminal:, (*23)

composer test
Made with :heart: by [Rubens Mariuzzo](https://github.com/rmariuzzo). [MIT license](LICENSE)

The Versions

04/07 2018
04/07 2018

dev-fix/a-komarev-github-username

dev-fix/a-komarev-github-username https://github.com/rmariuzzo/laravel-js-localization

Laravel Localization in JavaScript

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel javascript js i18n localization laravel 5 lang

23/11 2017
18/11 2017

dev-develop

dev-develop https://github.com/rmariuzzo/laravel-js-localization

Laravel Localization in JavaScript

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel javascript js i18n localization laravel 5 lang

18/11 2017
06/10 2017
03/09 2017
08/08 2017
04/07 2017
01/05 2017
01/05 2017

dev-feature/umd

dev-feature/umd https://github.com/rmariuzzo/laravel-js-localization

Laravel Localization in JavaScript

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel javascript js i18n localization laravel 5 lang

01/02 2017
01/02 2017

dev-feature/laravel-5-4-support

dev-feature/laravel-5-4-support https://github.com/rmariuzzo/laravel-js-localization

Laravel Localization in JavaScript

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel javascript js i18n localization laravel 5 lang

01/02 2017

dev-fix/tests

dev-fix/tests https://github.com/rmariuzzo/laravel-js-localization

Laravel Localization in JavaScript

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel javascript js i18n localization laravel 5 lang

13/12 2016

dev-improve-tests

dev-improve-tests https://github.com/rmariuzzo/laravel-js-localization

Laravel Localization in JavaScript

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel javascript js i18n localization laravel 5 lang

15/11 2016
15/11 2016
27/10 2016
17/10 2016
27/08 2016
27/08 2016
26/08 2016

dev-feature/improved-tests

dev-feature/improved-tests https://github.com/rmariuzzo/laravel-js-localization

Laravel Localization in JavaScript

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel javascript js i18n localization laravel 5 lang

25/08 2016

dev-feature/multiple-tests

dev-feature/multiple-tests https://github.com/rmariuzzo/laravel-js-localization

Laravel Localization in JavaScript

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel javascript js i18n localization laravel 5 lang

29/06 2015

v1.2.1

1.2.1.0 https://github.com/rmariuzzo/laravel-js-localization

Laravel Localization in JavaScript

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel javascript js i18n localization laravel 5 lang

16/06 2015

v1.2.0

1.2.0.0 https://github.com/rmariuzzo/laravel-js-localization

Laravel Localization in JavaScript

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel javascript js i18n localization laravel 5 lang

20/05 2014
15/05 2014
01/05 2014