mariuzzo/laravel-js-localization
Composer 安装命令:
composer require mariuzzo/laravel-js-localization
包简介
Laravel Localization in JavaScript
README 文档
README
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.
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
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 shiny 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:
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).
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:
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', ], ];
Using gulp (optional)
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'));
Using Laravel'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'))); }); 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(); });
Using Laravel'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:
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).
💁 Go to Lang.js documentation to see all available methods.
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.
Want to contribute?
- Fork this repository and clone it.
- Create a feature branch from develop:
git checkout develop; git checkout -b feature-foo. - Push your commits and create a pull request.
Prerequisites:
You will need to have installed the following softwares.
- Composer.
- PHP 5.5+.
Development setup
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!
Testing
This project uses PHPUnit. All tests are stored at tests directory. To run all tests type in your terminal:
composer test
Made with ❤️ by Rubens Mariuzzo.
mariuzzo/laravel-js-localization 适用场景与选型建议
mariuzzo/laravel-js-localization 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.06M 次下载、GitHub Stars 达 607, 最近一次更新时间为 2014 年 05 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「i18n」 「javascript」 「JS」 「laravel」 「localization」 「lang」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mariuzzo/laravel-js-localization 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mariuzzo/laravel-js-localization 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mariuzzo/laravel-js-localization 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Generates a Blade directive exporting all of your named Laravel routes. Also provides a nice route() helper function in JavaScript.
Easy to use i18n translation PHP class for multi-language websites
Caching and compression for Twig assets (JavaScript and CSS).
A pretty nice way to expose your translation messages to your JavaScript.
A custom URL rule class for Yii 2 which allows to create translated URL rules
Bureaux A Partager Edit - Parse, validate, manipulate, and display dates in PHP w/ i18n support. Inspired by moment.js
统计信息
- 总下载量: 4.06M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 609
- 点击次数: 27
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-05-01