Skip to content

Commit

Permalink
optimize locales
Browse files Browse the repository at this point in the history
  • Loading branch information
yushine committed Jul 15, 2024
1 parent 97b8f08 commit 13da690
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 8 deletions.
16 changes: 14 additions & 2 deletions innopacks/common/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,18 @@ function locales(): mixed
}
}

if (! function_exists('setting_locale_code')) {
/**
* Get setting locale code.
*
* @return string
*/
function setting_locale_code(): string
{
return system_setting('front_locale', config('app.locale', 'en'));
}
}

if (! function_exists('front_locale_code')) {
/**
* Get current locale code.
Expand All @@ -200,7 +212,7 @@ function locales(): mixed
*/
function front_locale_code(): string
{
return session('locale') ?? system_setting('front_locale', config('app.locale'));
return session('locale') ?? setting_locale_code();
}
}

Expand All @@ -221,7 +233,7 @@ function locale_code(): string
}
}

return session('locale', system_setting('front_locale', $configLocale));
return session('locale', setting_locale_code());
}
}

Expand Down
8 changes: 7 additions & 1 deletion innopacks/common/src/Traits/Translatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,14 @@ public function translation(): mixed
{
$class = $this->getDescriptionModelClass();

return $this->hasOne($class, $this->getForeignKey(), $this->getKeyName())
$translation = $this->hasOne($class, $this->getForeignKey(), $this->getKeyName())
->where('locale', locale_code());
if ($translation->count()) {
return $translation;
}

return $this->hasOne($class, $this->getForeignKey(), $this->getKeyName())
->where('locale', setting_locale_code());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion innopacks/front/src/Middleware/SetFrontLocale.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function handle(Request $request, \Closure $next): mixed

$availableLocales = locales()->pluck('code')->toArray();
if (! in_array($currentLocale, $availableLocales)) {
$currentLocale = system_setting('front_locale', config('app.locale'));
$currentLocale = setting_locale_code();
}

if (env('APP_LOCALE_FORCE')) {
Expand Down
2 changes: 1 addition & 1 deletion innopacks/panel/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function panel_locale_code(): string
*/
function panel_session_locale(): string
{
return session('panel_locale', system_setting('front_locale', config('app.locale')));
return session('panel_locale', setting_locale_code());
}
}

Expand Down
12 changes: 12 additions & 0 deletions innopacks/panel/lang/en/locale.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
/**
* Copyright (c) Since 2024 InnoShop - All Rights Reserved
*
* @link https://www.innoshop.com
* @author InnoShop <[email protected]>
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

return [
'cannot_disable_default_locale' => 'Cannot disable default locale',
];
12 changes: 12 additions & 0 deletions innopacks/panel/lang/es/locale.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
/**
* Copyright (c) Since 2024 InnoShop - All Rights Reserved
*
* @link https://www.innoshop.com
* @author InnoShop <[email protected]>
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

return [
'cannot_disable_default_locale' => 'Cannot disable default locale',
];
12 changes: 12 additions & 0 deletions innopacks/panel/lang/zh_cn/locale.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
/**
* Copyright (c) Since 2024 InnoShop - All Rights Reserved
*
* @link https://www.innoshop.com
* @author InnoShop <[email protected]>
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

return [
'cannot_disable_default_locale' => '不能禁用默认语言',
];
4 changes: 2 additions & 2 deletions innopacks/panel/src/Controllers/LocaleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function uninstall(Request $request): RedirectResponse
throw new Exception('默认语言不能卸载');
}
TranslationService::getInstance()->deleteLocale($locale);
session('locale', system_setting('front_locale', config('app.locale')));
session('locale', setting_locale_code());

return redirect(panel_route('locales.index'))->with('success', trans('panel::common.uninstall_success'));
} catch (Exception $e) {
Expand All @@ -129,7 +129,7 @@ public function active(Request $request, int $id): JsonResponse
try {
$item = Locale::query()->findOrFail($id);
if ($item->code == system_setting('front_locale')) {
throw new Exception('默认语言不能禁用');
throw new Exception(trans('panel::locale.cannot_disable_default_locale'));
}

$item->active = $request->get('status');
Expand Down
2 changes: 1 addition & 1 deletion innopacks/panel/src/Services/TranslationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class TranslationService extends BaseService
public function createLocale($data): mixed
{
$language = LocaleRepo::getInstance()->create($data);
$defaultLocale = system_setting('front_locale', 'en');
$defaultLocale = setting_locale_code();
foreach ($this->getDescriptionModels() as $className) {
$items = $className::query()->where('locale', $defaultLocale)->get()->toArray();
foreach ($items as &$item) {
Expand Down

0 comments on commit 13da690

Please sign in to comment.