- Notifications
You must be signed in to change notification settings - Fork168
🌐 Convert your Laravel messages and consume them in the front-end!
License
rmariuzzo/Laravel-JS-Localization
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
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.
- Support Laravel 4.2, 5.0, 5.1, 5.2, 5.3, 5.4, 5.5, 6.x, 7.x and 8.x!
- IncludesLang.js (a thin library highly inspired on Laravel's
Translatorclass). - Allow to specify desired lang files to be converted to JS.
- Lang.js API is based on Laravel's
Translatorclass. No need to learn a whole API.
⭐Webpack user? Try the new and shinyLaravel localization loader for Webpack! |
composer require mariuzzo/laravel-js-localization
In your Laravel app go toconfig/app.php and add the following service provider:
Mariuzzo\LaravelJsLocalization\LaravelJsLocalizationServiceProvider::class
TheLaravel-JS-Localization package provides a command that generate the JavaScript version of all your messages found at:app/lang (Laravel 4) orresources/lang (Laravel 5) directory. The resulting JavaScript file will contain all your messages plusLang.js (a thin library highly inspired on Laravel'sTranslator class).
php artisan lang:js
php artisan lang:js public/assets/dist/lang.dist.js
php artisan lang:js -c
php artisan lang:js public/assets/dist/lang.dist.js -s themes/default/lang
php artisan lang:js --json
First, publish the default package's configuration file running:
php artisan vendor:publish --provider="Mariuzzo\LaravelJsLocalization\LaravelJsLocalizationServiceProvider"The configuration will be published toconfig/localization-js.php.
You may edit this file to define the messages you need in your Javascript code. Just edit themessages array in the config file.Empty messages array will include all the language files in build.
To make onlypagination.php andvalidation.php files to be included in build process:
<?phpreturn ['messages' => ['pagination','validation', ],];
Usinggulp (optional)
Installgulp-shell and then run it directly in yourgulpfile.js:
varshell=require('gulp-shell');gulp.task('langjs',shell.task('php artisan lang:js -c public/js/messages.js'));
UsingLaravel's elixir (optional)
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')));});returnthis.queueTask('langjs');});
Elixir 4.0+:
varTask=elixir.Task;elixir.extend('langjs',function(path){newTask('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();});
UsingLaravel's Mix with Laravel 5.4+ (optional)
Add "webpack-shell-plugin-next" to package.json's "devDependencies" section.
Add the following to webpack.mix.js:
constWebpackShellPluginNext=require('webpack-shell-plugin-next');// Add shell command plugin configured to create JavaScript language filemix.webpackConfig({plugins:[newWebpackShellPluginNext({onBuildStart:['php artisan lang:js --quiet'],onBuildEnd:[]})]});
This is a quick documentation regardingLang.js (the thin JavaScript library included byLaravel-JS-Localization). TheLang.js (a thin library highly inspired on Laravel'sTranslator 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);
Lang.choice('messages.apples',10,{name:'Joe'});
💁 Go to Lang.js documentation to see all available methods.
- Fork this repository and clone it.
- Create afeature branch from develop:
git checkout develop; git checkout -b feature-foo. - Push your commits and create a pull request.
You will need to have installed the following softwares.
- Composer.
- PHP 5.5+.
After getting all the required softwares you may run the following commands to get everything ready:
Install PHP dependencies:
composer install
Install test dependencies:
composer test-install
Now you are good to go! Happy coding!
This project uses PHPUnit. All tests are stored attests directory. To run all tests type in your terminal:
composertestMade with ❤️ byRubens Mariuzzo.
About
🌐 Convert your Laravel messages and consume them in the front-end!
Topics
Resources
License
Code of conduct
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Uh oh!
There was an error while loading.Please reload this page.