diff --git a/Modules/Language/app/Services/LanguageService.php b/Modules/Language/app/Services/LanguageService.php index a4fc5d44..6c4ba93c 100644 --- a/Modules/Language/app/Services/LanguageService.php +++ b/Modules/Language/app/Services/LanguageService.php @@ -15,6 +15,7 @@ use Exception; class LanguageService extends MediaService { public string $cacheKey; + protected array $groupNames = ['auth', 'button', 'common', 'dashboard', 'frontend', 'input', 'settings', 'table']; public function __construct(Request $request) { @@ -28,31 +29,9 @@ class LanguageService extends MediaService $appLangPath = storage_path('app/lang/default'); - $groupNames = ['auth','button','common','dashboard','frontend','input','settings']; - $groups = []; - foreach ($groupNames as $groupName) { - $repoGroupPath1 = base_path("lang/{$data['code']}/groups/{$groupName}.php"); - $repoGroupPath2 = base_path("lang/{$data['code']}/{$groupName}.php"); - - if (file_exists($repoGroupPath1)) { - $groups[$groupName] = require $repoGroupPath1; - continue; - } - - if (file_exists($repoGroupPath2)) { - $file = require $repoGroupPath2; - $groups[$groupName] = [ - [ - 'name' => ucfirst(str_replace('_', ' ', $groupName)), - 'slug' => $groupName, - 'properties' => $file, - ], - ]; - continue; - } - - $groups[$groupName] = require storage_path("app/lang/groups/{$groupName}.php"); + foreach ($this->groupNames as $groupName) { + $groups[$groupName] = $this->loadLanguageGroup($data['code'], $groupName); } $languages = Language::create($data); @@ -80,6 +59,65 @@ class LanguageService extends MediaService Language::find($id)->update($data); } + protected function loadLanguageGroup(string $locale, string $groupName): array + { + $defaultGroupPath = storage_path("app/lang/groups/{$groupName}.php"); + $defaultGroups = file_exists($defaultGroupPath) ? require $defaultGroupPath : []; + + $repoGroupPath = base_path("lang/{$locale}/groups/{$groupName}.php"); + if (file_exists($repoGroupPath)) { + return require $repoGroupPath; + } + + $flatFilePath = base_path("lang/{$locale}/{$groupName}.php"); + if (file_exists($flatFilePath)) { + $flatTranslations = require $flatFilePath; + return $this->mergeFlatTranslations($defaultGroups, $flatTranslations, $groupName); + } + + return $defaultGroups; + } + + protected function mergeFlatTranslations(array $defaultGroups, array $flatTranslations, string $groupName): array + { + if (empty($defaultGroups)) { + return [[ + 'name' => ucfirst(str_replace('_', ' ', $groupName)), + 'slug' => $groupName, + 'properties' => $flatTranslations, + ]]; + } + + $groups = []; + + foreach ($defaultGroups as $group) { + $properties = []; + foreach ($group['properties'] as $key => $value) { + if (array_key_exists($key, $flatTranslations)) { + $properties[$key] = $flatTranslations[$key]; + unset($flatTranslations[$key]); + } else { + $properties[$key] = $value; + } + } + + $groups[] = [ + ...$group, + 'properties' => $properties, + ]; + } + + if (! empty($flatTranslations)) { + $groups[] = [ + 'name' => ucfirst(str_replace('_', ' ', $groupName)) . ' Extra', + 'slug' => "{$groupName}_extra", + 'properties' => $flatTranslations, + ]; + } + + return $groups; + } + function defaultLanguage($id) { Language::where('is_default', true)->update(['is_default' => false]);