Files
citrus/app/Models/SiteTranslation.php

78 lines
2.0 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?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;
}
}