支持插件多语言

This commit is contained in:
Edward Yang 2022-07-28 16:26:39 +08:00
parent 4721cb49b3
commit 8cf90c601c
4 changed files with 101 additions and 23 deletions

View File

@ -17,6 +17,8 @@ use Illuminate\Support\ServiceProvider;
class PluginServiceProvider extends ServiceProvider
{
private string $pluginBasePath = '';
/**
* Register the application services.
*
@ -36,30 +38,74 @@ class PluginServiceProvider extends ServiceProvider
public function boot()
{
$manager = app('plugin');
$plugins = $manager->getPlugins();
$bootstraps = $manager->getEnabledBootstraps();
$pluginBasePath = base_path('plugins');
$this->pluginBasePath = base_path('plugins');
foreach ($bootstraps as $bootstrap) {
$filePath = $bootstrap['file'];
$pluginCode = $bootstrap['code'];
if (file_exists($filePath)) {
$className = "Plugin\\{$pluginCode}\\Bootstrap";
if (method_exists($className, 'boot')) {
(new $className)->boot();
}
}
$routePath = "{$pluginBasePath}/{$pluginCode}/routes.php";
if (file_exists($routePath)) {
Route::prefix('plugin')
->middleware('web')
->group(function () use ($routePath) {
$this->loadRoutesFrom($routePath);
});
}
$this->loadViewsFrom("{$pluginBasePath}/{$pluginCode}/Views", $pluginCode);
$this->bootPlugin($bootstrap);
$this->loadRoutes($pluginCode);
$this->loadTranslations($pluginCode);
$this->loadViews($pluginCode);
}
}
/**
* 调用插件 Bootstrap::boot()
*
* @param $bootstrap
*/
private function bootPlugin($bootstrap)
{
$filePath = $bootstrap['file'];
$pluginCode = $bootstrap['code'];
if (file_exists($filePath)) {
$className = "Plugin\\{$pluginCode}\\Bootstrap";
if (method_exists($className, 'boot')) {
(new $className)->boot();
}
}
}
/**
* 加载插件路由
*
* @param $pluginCode
*/
private function loadRoutes($pluginCode)
{
$pluginBasePath = $this->pluginBasePath;
$routePath = "{$pluginBasePath}/{$pluginCode}/routes.php";
if (file_exists($routePath)) {
Route::prefix('plugin')
->middleware('web')
->group(function () use ($routePath) {
$this->loadRoutesFrom($routePath);
});
}
}
/**
* 加载多语言
*/
private function loadTranslations($pluginCode)
{
$pluginBasePath = $this->pluginBasePath;
$this->loadTranslationsFrom("{$pluginBasePath}/{$pluginCode}/Lang", $pluginCode);
}
/**
* 加载模板目录
*
* @param $pluginCode
*/
private function loadViews($pluginCode)
{
$pluginBasePath = $this->pluginBasePath;
$this->loadViewsFrom("{$pluginBasePath}/{$pluginCode}/Views", $pluginCode);
}
}

View File

@ -17,14 +17,14 @@ class Bootstrap
{
add_filter('header.categories', function ($data) {
$data[] = [
'name' => '插件链接',
'name' => trans('HeaderMenu::header.plugin_link'),
'url' => shop_route('home.index'),
'children' => [
[
"name" => "最新商品",
"name" => trans('HeaderMenu::header.latest_products'),
"url" => plugin_route('latest_products'),
], [
"name" => "百度",
"name" => trans('HeaderMenu::header.baidu'),
"url" => "https://www.baidu.com",
]
],

View File

@ -0,0 +1,16 @@
<?php
/**
* dd.php
*
* @copyright 2022 opencart.cn - All Rights Reserved
* @link http://www.guangdawangluo.com
* @author Edward Yang <yangjin@opencart.cn>
* @created 2022-07-28 16:19:06
* @modified 2022-07-28 16:19:06
*/
return [
'plugin_link' => 'Plugin Link',
'latest_products' => 'Latest Products',
'baidu' => 'Baidu',
];

View File

@ -0,0 +1,16 @@
<?php
/**
* dd.php
*
* @copyright 2022 opencart.cn - All Rights Reserved
* @link http://www.guangdawangluo.com
* @author Edward Yang <yangjin@opencart.cn>
* @created 2022-07-28 16:19:06
* @modified 2022-07-28 16:19:06
*/
return [
'plugin_link' => '插件链接',
'latest_products' => '最新商品',
'baidu' => '百度',
];