angelo8828 / laravel-js-localization
Laravel包,用于为JavaScript代码提供本地化支持。这是https://github.com/andywer/laravel-js-localization的升级版和现代化版本。
Requires
- php: >=8.0
- illuminate/support: 10.*
Requires (Dev)
- mockery/mockery: ~1.0
- orchestra/testbench: 8.*
- phpunit/phpunit: ^9.6
This package is not auto-updated.
Last update: 2024-09-27 12:34:31 UTC
README
简单、易于使用且灵活的Laravel Web框架包。允许您在JavaScript代码中使用Laravel Webapp的本地化消息(请参阅resources/lang目录)。您可以轻松配置需要导出的消息。这是https://github.com/andywer/laravel-js-localization的升级版和现代化版本。
此版本直接从Andywer的JS Localization Helper for Laravel分叉而来。原始项目不再维护,且对于版本9.0的Laravel项目不再工作。
请查看这个比较工具来检查我做出的更改。请查看原始仓库以了解其工作方式。我不会更改或添加原始源代码的任何功能。
分支
安装
将以下行添加到您的Laravel Webapp的composer.json文件的require部分
"require": { "angelo8828/laravel-js-localization": "dev-laravel-10" // see table above }
运行composer update以安装包。
最后,将以下行添加到您的app/config/app.php文件的providers数组中
'providers' => [ /* ... */ JsLocalization\JsLocalizationServiceProvider::class ]
配置
首先运行php artisan vendor:publish。此命令将包的默认配置复制到config/js-localization.php。
您现在可以编辑此文件以定义需要在JavaScript代码中的消息。只需编辑配置文件中的messages数组。
示例(导出所有提醒消息)
<?php return [ // Set the locales you use 'locales' => ['en'], // Set the keys of the messages you want to use in javascript 'messages' => [ 'passwords' => [ 'password', 'user', 'token' ] ], /* * in short: * 'messages' => ['passwords'] * * * you could also use: * * 'messages' => [ * 'passwords.password', * 'passwords.user', * 'passwords.token' * ] */ // Set the keys of config properties you want to use in javascript. // Caution: Do not expose any configuration values that should be kept privately! 'config' => [ 'app.debug' ], // Disables the config cache if set to true, so you don't have to run `php artisan js-localization:refresh` // each time you change configuration files. // Attention: Should not be used in production mode due to decreased performance. 'disable_config_cache' => false, // Split up the exported messages.js file into separate files for each locale. // This is to ensue faster loading times so one doesn't have to load translations for _all_ languages. 'split_export_files' => true, ];
重要
当首次使用JsLocalizationController时,消息配置将被缓存。在更改消息配置后,您需要调用php artisan js-localization:refresh来刷新该缓存。这也影响您导出到JavaScript的配置属性,因为它们也被缓存。
使用方法
JavaScript的翻译资源可以是您的Laravel应用在运行时提供,也可以作为静态JavaScript文件预先生成,允许您直接从您的Web服务器或CDN提供,或将其包含在您的构建过程中。
运行时生成
您只需将必要的<script>标签添加到您的布局中。以下是一个示例blade视图
@include('js-localization::head')
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test view</title>
@yield('js-localization.head')
</head>
<body>
<p>
Here comes a translated message:
<script type="text/javascript">
document.write( Lang.get('reminder.user') );
</script>
</p>
</body>
</html>
请记住,最好不要将@yield('js-localization.head')放在<head>中,因为它包含发送前端部分的<script>标签。最佳实践是将其放在<body>的末尾,但在其他<script>标签之前。上面的示例简单地将它包含在头部,因为这是使用它的最简单形式。
静态生成
为了提高性能,可以生成包含所有生成字符串的静态JavaScript文件。这些文件可以作为静态文件直接提供,或作为您前端资产构建过程的一部分。
要指定资产输出目录,只需将 $storage_path 字符串设置在您的 config/js-localization.php 文件中即可(见 配置)。
/*
|--------------------------------------------------------------------------
| Define the target to save the exported messages to
|--------------------------------------------------------------------------
|
| Directory for storing the static files generated when using file storage.
|
*/
'storage_path' => public_path('vendor/js-localization/'),
然后可以使用 artisan 命令生成文件
php artisan js-localization:export
这将在您的目标目录中生成两个文件
messages.js包含您的翻译字符串config.js包含您导出的配置值
如果您想自动将 messages.js 文件分割成针对每个语种的单独 .js 文件,您可以在您的 config/js-localization.php 配置文件中将以下设置为 true
'split_export_files' => true,
这将在您的目标目录中生成以下文件
lang-{locale}.js包含一个语种的翻译字符串,如果将split_export_files配置选项设置为 true
请注意,每次编辑、添加或删除任何翻译字符串时,都需要使用 php artisan js-localization:export 重新生成文件。
功能
您可以在您的 JavaScript 代码中使用 Lang.get()、Lang.has()、Lang.choice()、Lang.locale() 和 trans()(Lang.get() 的别名)以及在您的 JavaScript 代码中传递配置属性。JavaScript 中也有 Config.get()。使用 config/js-localization.php 文件中的 config 字段配置要传递给客户端的配置属性。注意:不要导出任何安全敏感的属性,如数据库凭证等,因为它们将对所有使用您应用程序的人可见!
支持消息中的变量。例如:"This is my test string for :name."。
也支持复数形式,但不关心语种。它只使用英语复数规则("singular text|plural text")。更复杂的复数量词目前尚不支持。
服务提供商
假设您正在开发一个依赖于此 JavaScript 本地化功能的 Laravel 扩展包,并且您想配置哪些包的消息对 JS 代码可见。
幸运的是,这很简单。只需监听 JsLocalization.registerMessages 事件,并使用 JsLocalization\Facades\JsLocalizationHelper::addMessagesToExport() 方法。如下所示
<?php use Illuminate\Support\ServiceProvider; use JsLocalization\Facades\JsLocalizationHelper; class MyServiceProvider extends ServiceProvider { /* ... */ public function register() { Event::listen('JsLocalization.registerMessages', function() { JsLocalizationHelper::addMessagesToExport([ // list the keys of the messages here, similar // to the 'messages' array in the config file ]); }); } /* ... */ }
许可证
本软件根据 MIT 许可证发布。请参阅 许可证。