cacheKey = 'language_properties'; } function storeLanguage($data) { $langPath = base_path('lang'); $langDir = $langPath . "/" . $data['code']; $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"); } $languages = Language::create($data); foreach ($groups as $key => $group) { foreach ($group as $value) { LanguageProperty::create([ ...$value, 'group' => $key, 'language_id' => $languages->id, ]); } } $alreadyExists = is_dir($langDir); if (! $alreadyExists) { File::makeDirectory($langDir, 0777, true, true); File::copyDirectory($appLangPath, $langDir); } } function updateLanguage($id, $data) { Language::find($id)->update($data); } function defaultLanguage($id) { Language::where('is_default', true)->update(['is_default' => false]); Language::where('id', $id)->update(['is_default' => true]); } function setLanguageProperties(string $locale): void { $cached = Cache::rememberForever($this->cacheKey, function () use ($locale) { $language = Language::where('code', $locale) ->with('properties') ->first(); $groups = []; foreach ($language->properties as $property) { $groups[$property->group] = array_merge($groups[$property->group] ?? [], $property->properties); } $translations = []; foreach ($groups as $group => $properties) { foreach ($properties as $key => $value) { $translations[$group . '.' . $key] = $value; } } return $translations; }); Lang::addLines($cached, $locale); } function deleteLanguage($id) { $lang = Language::find($id); $langPath = base_path('lang'); $langDir = $langPath . "/" . $lang->code; if ($lang->is_default) { throw new Exception("Default language can't be deleted"); } if (is_dir($langDir)) { File::deleteDirectory($langDir); } $lang->delete(); } }