支持插件多语言

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 class PluginServiceProvider extends ServiceProvider
{ {
private string $pluginBasePath = '';
/** /**
* Register the application services. * Register the application services.
* *
@ -36,11 +38,26 @@ class PluginServiceProvider extends ServiceProvider
public function boot() public function boot()
{ {
$manager = app('plugin'); $manager = app('plugin');
$plugins = $manager->getPlugins();
$bootstraps = $manager->getEnabledBootstraps(); $bootstraps = $manager->getEnabledBootstraps();
$pluginBasePath = base_path('plugins'); $this->pluginBasePath = base_path('plugins');
foreach ($bootstraps as $bootstrap) { foreach ($bootstraps as $bootstrap) {
$pluginCode = $bootstrap['code'];
$this->bootPlugin($bootstrap);
$this->loadRoutes($pluginCode);
$this->loadTranslations($pluginCode);
$this->loadViews($pluginCode);
}
}
/**
* 调用插件 Bootstrap::boot()
*
* @param $bootstrap
*/
private function bootPlugin($bootstrap)
{
$filePath = $bootstrap['file']; $filePath = $bootstrap['file'];
$pluginCode = $bootstrap['code']; $pluginCode = $bootstrap['code'];
if (file_exists($filePath)) { if (file_exists($filePath)) {
@ -49,7 +66,17 @@ class PluginServiceProvider extends ServiceProvider
(new $className)->boot(); (new $className)->boot();
} }
} }
}
/**
* 加载插件路由
*
* @param $pluginCode
*/
private function loadRoutes($pluginCode)
{
$pluginBasePath = $this->pluginBasePath;
$routePath = "{$pluginBasePath}/{$pluginCode}/routes.php"; $routePath = "{$pluginBasePath}/{$pluginCode}/routes.php";
if (file_exists($routePath)) { if (file_exists($routePath)) {
Route::prefix('plugin') Route::prefix('plugin')
@ -58,8 +85,27 @@ class PluginServiceProvider extends ServiceProvider
$this->loadRoutesFrom($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); $this->loadViewsFrom("{$pluginBasePath}/{$pluginCode}/Views", $pluginCode);
} }
} }
}

View File

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