xanweb / c5-js-localization
Concrete5 本地化
v2.0.1
2021-10-11 09:27 UTC
Requires
- php: >=7.4
- symfony/polyfill-php80: ^1.20
- xanweb/c5-common: ^1.2
- xanweb/c5-helpers: ^2.0
README
从 PHP 传递翻译和任何其他信息到 JavaScript
安装
将库包含到 composer.json 中
composer require xanweb/c5-js-localization
用法
在包启动时注册 \Xanweb\C5\JsLocalization\ServiceProvider
或将其包含在 /application/config/app.php
中
对于后端
1- 在您的包启动时或在 /application/bootstrap/app.php
下添加监听器到 BackendAssetLocalizationLoad::NAME
use Xanweb\C5\JsLocalization\Event\BackendAssetLocalizationLoad; $this->app['director']->addListener(BackendAssetLocalizationLoad::NAME, function (BackendAssetLocalizationLoad $event) { $event->getAssetLocalization()->mergeWith([ 'i18n' => [ 'confirm' => t('Are you sure?'), 'maxItemsExceeded' => t('Max items exceeded, you cannot add any more items.'), 'pageNotFound' => t('Page not found'), 'colorPicker' => [ 'cancelText' => t('Cancel'), 'chooseText' => t('Choose'), 'togglePaletteMoreText' => t('more'), 'togglePaletteLessText' => t('less'), 'noColorSelectedText' => t('No Color Selected'), 'clearText' => t('Clear Color Selection'), ] ], 'editor' => [ 'initRichTextEditor' => $this->app['editor']->getEditorInitJSFunction(), ], ]); });
2- 将所需的资源包含到您的视图中
$view->requireAsset('javascript-localized', 'xw/backend');
3- 您现在可以在 JavaScript 文件中使用您的数据
alert(xw_backend.i18n.confirm); // Init Editor Example: xw_backend.editor.initRichTextEditor($('#myTextareaField'));
对于前端
1- 在您的包启动时或在 /application/bootstrap/app.php
下添加监听器到 FrontendAssetLocalizationLoad::NAME
use Xanweb\C5\JsLocalization\Event\FrontendAssetLocalizationLoad; $this->app['director']->addListener(FrontendAssetLocalizationLoad::NAME, function (FrontendAssetLocalizationLoad $event) { $event->getAssetLocalization()->mergeWith([ 'i18n' => [ 'message' => t('Are you sure?'), ], 'methods' => [ 'showMessage' => 'function(){ alert("Website: ' . Core::make('site')->getSite()->getSiteName() . '"); }', ], ]); });
2- 将所需的资源包含到您的视图中
$view->requireAsset('javascript-localized', 'xw/frontend');
3- 您现在可以在 JavaScript 文件中使用您的数据
alert(xw_frontend.i18n.message); // Execute function: xw_frontend.methods.showMessage();