Skip to content

Commit

Permalink
feat: 多语言设置区分模块
Browse files Browse the repository at this point in the history
  • Loading branch information
slowlyo committed Jun 15, 2024
1 parent cde5e6c commit d84f38e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
12 changes: 5 additions & 7 deletions src/Controllers/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,14 @@ public function noContentResponse(): JsonResponse|JsonResource

public function settings(): JsonResponse|JsonResource
{
$module = Admin::currentModule(true);
$prefix = $module ? $module . '_' : '';

$localeOptions = Admin::config('admin.layout.locale_options') ?? [
'en' => 'English',
'zh_CN' => '简体中文',
];

$locale = settings()->get('admin_locale', config('app.locale'));
$locale = settings()->getByModule('admin_locale', config('app.locale'));

if($locale == 'null'){
if ($locale == 'null') {
$locale = 'zh_CN';
}

Expand All @@ -48,7 +45,7 @@ public function settings(): JsonResponse|JsonResource
'login_captcha' => Admin::config('admin.auth.login_captcha'),
'locale_options' => map2options($localeOptions),
'show_development_tools' => Admin::config('admin.show_development_tools'),
'system_theme_setting' => Admin::setting()->get($prefix . 'system_theme_setting'),
'system_theme_setting' => settings()->getByModule('system_theme_setting'),
'enabled_extensions' => Extension::query()->where('is_enabled', 1)->pluck('name')?->toArray(),
]);
}
Expand All @@ -65,8 +62,9 @@ public function saveSettings(Request $request)
$data = $request->all();
$currentModule = Admin::currentModule(true);

$distinguishingModule = ['system_theme_setting', 'admin_locale'];
foreach ($data as $key => $value) {
if ($key == 'system_theme_setting' && $currentModule) {
if (in_array($key, $distinguishingModule) && $currentModule) {
$data[$currentModule . '_' . $key] = $value;
unset($data[$key]);
}
Expand Down
25 changes: 21 additions & 4 deletions src/Services/AdminSettingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ public function all()
/**
* 获取设置项
*
* @param string $key 设置项key
* @param string $key 设置项key
* @param mixed|null $default 默认值
* @param bool $fresh 是否直接从数据库获取
* @param bool $fresh 是否直接从数据库获取
*
* @return mixed|null
*/
Expand All @@ -114,12 +114,29 @@ public function get(string $key, mixed $default = null, bool $fresh = false)
return $value ?? $default;
}

/**
* 获取模块设置项
*
* @param string $key
* @param mixed|null $default
* @param bool $fresh
*
* @return mixed|null
*/
public function getByModule(string $key, mixed $default = null, bool $fresh = false)
{
$module = Admin::currentModule(true);
$prefix = $module ? $module . '_' : '';

return $this->get($prefix . $key, $default, $fresh);
}

/**
* 获取设置项中的某个值
*
* @param string $key 设置项key
* @param string $key 设置项key
* @param string $path 通过点号分隔的路径, 同Arr::get()
* @param $default
* @param $default
*
* @return array|\ArrayAccess|mixed|null
*/
Expand Down

0 comments on commit d84f38e

Please sign in to comment.