drewneon/lang4js
Composer 安装命令:
composer require drewneon/lang4js
包简介
Lang4JS extends the core localization functionalities of Laravel to be appplicable in Javascript.
README 文档
README
Localization, one of the core features of Laravel, separates language strings from views, so as to serve a robust foundation for multilingual projects. However, trans(), trans_choice() or the newly-introduced __(), supported by Blade template engine of different version of Laravel, are not usable in Javascript. This issue is definitely inevitable for developing any serious multilingual projects and just what Lang4JS is addressing. When Lang4JS is installed as a package in your Laravel application, you are able to use trans(), trans_choice() and __() in Javascript with the same syntax just as in Blade templates, the language strings defined in files under resources/lang folder are retrieved correspondently and displayed right to the users. Lang4JS is very easy to use and lightweight, merely a 3Kb controller file and a 4Kb minified JS file.
Compatibility
Lang4JS is compatible with Laravel 4.2+.
Installation
- Via Composer
$ composer require drewneon/lang4js
- Then add the service provider in
config/app.php:
DrewNeon\Lang4JS\Lang4JSServiceProvider::class,
- And publish the asset file, i.e.
lang4js.min.js:
$ php artisan vendor:publish --tag=public --force
- Finally, add a route in
routes/web.php:
Route::post('/lang4js', '\DrewNeon\Lang4JS\Lang4JSController@lang4js');
Usage
- Language Strings
Lang4JS retrieves language strings directly from your language files resides in resources/lang folder. Please add lines supposed to be used in your Javascript as those for your view blades.
- Load Javascript Libraries
Lang4JS requires [jQuery] (http://jquery.com) library, you have to load it before the lang4js.min.js. So in any view blades you want to apply multilingual strings in Javascript, please insert these two lines:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <script src="/vendor/lang4js/lang4js.min.js"></script>
3) Absolute Path
Both post('/lang4js', in route and src="/vendor/lang4js/lang4js.min.js" in view uses absolute path for the convenience to include the script from any URL. This means you have to point your domain to the public folder in your project, instead of the root folder, otherwise, a 404 error will occur or modify the paths according to your own environment.
4) CSRF Token
Lang4JS utilizes Ajax requests to retrieve language strings, and this requires CSRF Token in Laravel. There are two ways to set CSRF Token in your webpages.
- If the webpage contains a form, you may have probably had the line
{{ csrf_field() }}in there. If not, add the line somewhere between<form>...</form>. - If the webpage is not for form submission, please add
<meta name="csrf-token" content="{{ csrf_token() }}">into<head>...</head>.
In terms of security of you website, it's a good practice to always have CSRF Token presented in all webpages, which can be easily done by adding the meta tag for CSRF Token in the head sub-view for all views.
- Use Language Keys in Javascript
Now in your Javascript, feel free to use trans(), trans_choice() as well as __() newly introduced in Laravel 5.4. Please refer to the Localization document according to your Laravel version for more information. For example:
trans('messages.welcome')or__('messages.welcome')translates to the string defined in the line'welcome' => ''of the fileresources/lang/[locale]/messages.php;trans_choice('messages.apples', 10)translates to the plural part of the line'apples' => '|'inresources/lang/[locale]/messages.php.
The only difference in syntax
When using the place-holder feature of trans() or __(), the syntax ['name' => 'dayle'] does NOT work for Javascript, please write {'name' : 'dayle'} instead.
Some Issues
trans(),trans_choice()or__()in your Javascript does NOT work before the DOM is ready, and will halt your entire Javascript. Please use them in functions.- It's also a fatal error when there's no
trans()nortrans_choice()nor__()at all in your Javascript. To fix this, just remove<script src="/vendor/lang4js/lang4js.min.js"></script>from you view. - When the locale string requires newlines, please always use
<br>for HTML and\nfor Javascript. If you want to show\nas is in Javascript, please wrap it in double quotes, i.e."\n".
Change log
Please see CHANGELOG for more information on what has changed recently.
Testing
$ composer test
Contributing
Please see CONTRIBUTING and CONDUCT for details.
Security
If you discover any security related issues, please email drewneon@gmail.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
drewneon/lang4js 适用场景与选型建议
drewneon/lang4js 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 28 次下载、GitHub Stars 达 0, 最近一次更新时间为 2017 年 02 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「javascript」 「laravel」 「localization」 「multilingual」 「Lang4JS」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 drewneon/lang4js 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 drewneon/lang4js 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 drewneon/lang4js 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Generates a Blade directive exporting all of your named Laravel routes. Also provides a nice route() helper function in JavaScript.
Package for convenient work with Laravel's localization features
Caching and compression for Twig assets (JavaScript and CSS).
Field for number with restricted count of digits and decimal places
A pretty nice way to expose your translation messages to your JavaScript.
Deepl Automated translations for Laravel lang files
统计信息
- 总下载量: 28
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 10
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-02-20