143 lines
3.8 KiB
PHP
143 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace Modules\Language\Services;
|
|
|
|
use App\Services\MediaService;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\Lang;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\App;
|
|
use Illuminate\Support\Facades\File;
|
|
use Modules\Language\Models\Language;
|
|
use Modules\Language\Models\LanguageProperty;
|
|
use Exception;
|
|
|
|
class LanguageService extends MediaService
|
|
{
|
|
public string $cacheKey;
|
|
|
|
public function __construct(Request $request)
|
|
{
|
|
$this->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]);
|
|
}
|
|
|
|
protected function getCacheKey(string $locale): string
|
|
{
|
|
return "{$this->cacheKey}_{$locale}";
|
|
}
|
|
|
|
public function forgetLanguageCache(string $locale): void
|
|
{
|
|
Cache::forget($this->getCacheKey($locale));
|
|
}
|
|
|
|
function setLanguageProperties(string $locale): void
|
|
{
|
|
$cacheKey = $this->getCacheKey($locale);
|
|
|
|
$cached = Cache::rememberForever($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();
|
|
}
|
|
}
|