Stars: 557
Forks: 160
Pull Requests: 104
Issues: 81
Watchers: 17
Last Updated: 2023-08-15 20:41:53
π Convert your Laravel messages and consume them in the front-end!
License: MIT License
Languages: PHP, JavaScript, Shell
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.
Translator class).Translator class. No need to learn a whole API.|
β Webpack user? Try the new and shiny Laravel localization loader for Webpack! |
composer require mariuzzo/laravel-js-localizationIn your Laravel app go to config/app.php and add the following service provider:
Mariuzzo\LaravelJsLocalization\LaravelJsLocalizationServiceProvider::classThe 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).
php artisan lang:jsphp artisan lang:js public/assets/dist/lang.dist.jsphp artisan lang:js -cphp artisan lang:js public/assets/dist/lang.dist.js -s themes/default/langphp artisan lang:js --jsonFirst, publish the default package's configuration file running:
php artisan vendor:publish --provider="Mariuzzo\LaravelJsLocalization\LaravelJsLocalizationServiceProvider"The configuration will be published to config/localization-js.php.
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.
To make only pagination.php and validation.php files to be included in build process:
<?php
return [
'messages' => [
'pagination',
'validation',
],
];Install gulp-shell and then run it directly in your gulpfile.js:
var shell = require('gulp-shell');
gulp.task('langjs', shell.task('php artisan lang:js -c public/js/messages.js'));Before Elixir 4.0:
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+:
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:
elixir(function(mix) {
mix.langjs();
});Add "webpack-shell-plugin-next" to package.json's "devDependencies" section.
Add the following to webpack.mix.js:
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:[]})
]
});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).
π Go to Lang.js documentation to see all available methods.
Lang.get('messages.home');Lang.get('messages.welcome', { name: 'Joe' });Lang.setLocale('es');Lang.has('messages.foo');Lang.choice('messages.apples', 10);choice method with replacementsLang.choice('messages.apples', 10, { name: 'Joe' });π Go to Lang.js documentation to see all available methods.
git checkout develop; git checkout -b feature-foo.You will need to have installed the following softwares.
After getting all the required softwares you may run the following commands to get everything ready:
Install PHP dependencies:
composer installInstall test dependencies:
composer test-installNow you are good to go! Happy coding!
This project uses PHPUnit. All tests are stored at tests directory. To run all tests type in your terminal:
composer testMade with β€οΈ by Rubens Mariuzzo.