在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):andywer/laravel-js-localization开源软件地址(OpenSource Url):https://github.com/andywer/laravel-js-localization开源编程语言(OpenSource Language):PHP 86.9%开源软件介绍(OpenSource Introduction):laravel-js-localizationSimple, ease-to-use and flexible package for the Laravel web framework. Allows you to use localized messages of the Laravel webapp (see Branches
InstallationAdd the following line to the "require": {
"andywer/js-localization": "dev-laravel-6" // see table above
} Run Finally add the following line to the 'providers' => [
/* ... */
JsLocalization\JsLocalizationServiceProvider::class
] ConfigurationRun You may now edit this file to define the messages you need in your Javascript code. Just edit the Example (exports all reminder 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,
]; Important: The messages configuration will be cached when the JsLocalizationController is used for the first time. After changing the messages configuration you will need to call UsageThe translation resources for JavaScript can either be served by your Laravel app at run-time or they can be pre-generated as static JavaScript files, allowing you to serve them straight from your web server or CDN or to be included in your build process. Run-time generationYou just need to add the necessary @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> Remember it's best to not put the Static generationFor increased performance it is possible to generate static JavaScript files with all of your generated strings. These files can either be served directly as static files, or included as a part of your frontend asset build process. To specify the output directory for the assets, just set the
The files can then be generated using the artisan command:
This will generate two files in your target directory:
If you want to automatically split up the
This will in turn also generate the following file(s) in your target directory:
Remember that the files needs to be regenerated using FeaturesYou may use Lang.get(), Lang.has(), Lang.choice(), Lang.locale() and trans() (alias for Lang.get()) in your Javascript code. They work just like Laravel's Variables in messages are supported. For instance: Pluralization is also supported, but does not care about the locale. It only uses the English pluralization rule ( Service providersAssume you are developing a laravel package that depends on this javascript localization features and you want to configure which messages of your package have to be visible to the JS code. Fortunately that's pretty easy. Just listen to the <?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
]);
});
}
/* ... */
} LicenseThis software is released under the MIT license. See license. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论