Add Site Translation Management: Created SiteTranslation resource with pages for listing, creating, and editing translations. Implemented localization support for translation keys and values, enhancing the admin panel's usability for managing site translations. Introduced a new migration for the site_translations table and updated helper functions for translation retrieval.

This commit is contained in:
Ümit Tunç
2025-12-30 23:41:32 +03:00
parent f381caaa39
commit 064ec327ba
13 changed files with 376 additions and 252 deletions
+77
View File
@@ -0,0 +1,77 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
class SiteTranslation extends Model
{
use HasFactory;
protected $fillable = [
'hash',
'key',
'translations',
];
protected $casts = [
'translations' => 'array',
];
protected static function boot()
{
parent::boot();
static::creating(function ($model) {
if (empty($model->hash)) {
$model->hash = md5($model->key);
}
});
static::updating(function ($model) {
// Key değiştiyse hash'i güncelle
if ($model->isDirty('key')) {
$model->hash = md5($model->key);
}
});
static::saved(function ($model) {
Cache::forget("site_translation_{$model->hash}");
});
}
/**
* Get translation for a specific key
*/
public static function getTranslation(string $key): string
{
$hash = md5($key);
$locale = app()->getLocale();
// Cache'ten veya DB'den al
// Cache süresi: 24 saat
$translation = Cache::remember("site_translation_{$hash}", 60 * 60 * 24, function () use ($hash, $key) {
return static::firstOrCreate(
['hash' => $hash],
['key' => $key]
);
});
// Eğer kayıt yeni oluşturulduysa ve key eksikse (concurrency nedeniyle), key'i set et
if ($translation->wasRecentlyCreated && empty($translation->translations)) {
// Yeni oluşturuldu, henüz çeviri yok.
// Varsayılan olarak key'i döndür.
return $key;
}
$translations = $translation->translations ?? [];
if (isset($translations[$locale]) && !empty($translations[$locale])) {
return $translations[$locale];
}
return $key;
}
}