Laravel Localization in JavaScript
michele-angioni/laravel-js-lang-converter is a Laravel package for laravel localization in javascript.
It currently has 25 GitHub stars and 65.325 downloads on Packagist (latest version v3.1.0).
Install it with composer require michele-angioni/laravel-js-lang-converter.
Discover more Laravel packages by michele-angioni
or browse all Laravel packages to compare alternatives.
Last updated
Laravel Localization in JavaScript.
Laravel JS Lang Converter converts all your localization messages of your Laravel app to JavaScript, providing a small JavaScript library to interact with those messages in the fron end.
Most of the work has been developed in Mariuzzo's package
Laravel 5.5+ is supported. PHP 7.0 is required. For Laravel 5.1 - 5-4 versions, use the v2.x branch.
Add the following line to you composer.json file under require.
"michele-angioni/laravel-js-lang-converter": "~3.0"
and run composer update or composer install.
Then you need to add the Laravel JS Lang Converter service provider in your app/config/app.php file
'providers' => [
// ...
'MicheleAngioni\LaravelJsLangConverter\LaravelJsLangConverterServiceProvider',
// ...
],
In order to use some package features, you need to publish the config file through the artisan command php artisan vendor:publish. It will create the laravel_js_lang.php file in your config directory.
Now you are done!
This project comes with a command that generate the JavaScript version of all your messages found in resources/lang directory. The resulting JavaScript file will have the whole bunch of messages and a thin library similar to Laravel's Lang class.
Generating JS messages
php artisan lang:js
Specifying a custom target
php artisan lang:js public/assets/dist/lang.dist.js
Converting only some files
If you don't want to convert ALL your lang files, you can specify the files you want to be converted into your laravel_js_lang.php conf file. Under the files array, just add the list of your source files, like so:
'files' => [
'pagination',
'validation'
]
Compressing the JS file
php artisan lang:js -c
Use gulp to publish (optional):
Install gulp-shell from https://github.com/sun-zheng-an/gulp-shell with npm install --save-dev gulp-shell .
Create an extension for elixir in your gulpfile.js:
var shell = require('gulp-shell');
//......
var Task = elixir.Task;
elixir.extend('langjs', function(path, minimize) {
new Task('langjs', function() {
var command = "php artisan lang:js " + (path || "public/js/messages.js");
if (minimize) {
command += " -c";
}
return gulp.src("").pipe(shell(command));
});
});
gulp.task('langJs', shell.task('php artisan lang:js -c public/js/messages.js'));
Use the new elixir task:
elixir(function(mix) {
var path = "public/js";
var minimize = true;
mix.langjs(path, minimize);
});
This is the documentation regarding the thin JavaScript library. The library is highly inspired on Laravel's Lang class.
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' });
For more detailed information, take a look at the source: Lang.js.
Pull requests are welcome.
git checkout -b feature-foo.Prerequisites:
You need to have installed the following softwares.
After getting all the required software you may run the following commands to get everything ready:
composer install
npm install -g jasmine-node
npm install
Now you are good to go! Happy coding!
This project use Jasmine-Node and PHPUnit. All tests are stored at tests directory.
To run all JS tests type in you terminal:
npm test
To run all PHP tests type in your terminal:
vendor/bin/phpunit tests/