|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\TranslationLoader; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Cache; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
7
|
|
|
|
|
8
|
|
|
class LanguageLine extends Model |
|
9
|
|
|
{ |
|
10
|
|
|
/** @var array */ |
|
11
|
|
|
public $translatable = ['text']; |
|
12
|
|
|
|
|
13
|
|
|
/** @var array */ |
|
14
|
|
|
public $guarded = ['id']; |
|
15
|
|
|
|
|
16
|
|
|
/** @var array */ |
|
17
|
|
|
protected $casts = ['text' => 'array']; |
|
18
|
|
|
|
|
19
|
|
|
public static function boot() |
|
20
|
|
|
{ |
|
21
|
|
|
parent::boot(); |
|
22
|
|
|
static::saved(function (LanguageLine $languageLine) { |
|
23
|
|
|
$languageLine->flushGroupCache(); |
|
24
|
|
|
}); |
|
25
|
|
|
|
|
26
|
|
|
static::deleted(function (LanguageLine $languageLine) { |
|
27
|
|
|
$languageLine->flushGroupCache(); |
|
28
|
|
|
}); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public static function getTranslationsForGroup(string $locale, string $group): array |
|
32
|
|
|
{ |
|
33
|
|
|
return Cache::rememberForever(static::getCacheKey($group, $locale), function () use ($group, $locale) { |
|
34
|
|
|
return static::query() |
|
35
|
|
|
->where('group', $group) |
|
36
|
|
|
->get() |
|
37
|
|
|
->reduce(function ($lines, LanguageLine $languageLine) use ($locale) { |
|
38
|
|
|
array_set($lines, $languageLine->key, $languageLine->getTranslation($locale)); |
|
39
|
|
|
|
|
40
|
|
|
return $lines; |
|
41
|
|
|
}) ?? []; |
|
42
|
|
|
}); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public static function getCacheKey(string $group, string $locale): string |
|
46
|
|
|
{ |
|
47
|
|
|
return "spatie.translation-loader.{$group}.{$locale}"; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param string $locale |
|
52
|
|
|
* |
|
53
|
|
|
* @return string |
|
54
|
|
|
*/ |
|
55
|
|
|
public function getTranslation(string $locale): string |
|
56
|
|
|
{ |
|
57
|
|
|
if(isset($this->text[$locale]) && !is_array($this->text[$locale])) |
|
58
|
|
|
return $this->text[$locale]; |
|
59
|
|
|
if(isset($this->text[config('app.fallback_locale')]) && !is_array($this->text[config('app.fallback_locale')])) |
|
60
|
|
|
return $this->text[config('app.fallback_locale')]; |
|
61
|
|
|
return ''; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param string $locale |
|
66
|
|
|
* @param string $value |
|
67
|
|
|
* |
|
68
|
|
|
* @return $this |
|
69
|
|
|
*/ |
|
70
|
|
|
public function setTranslation(string $locale, string $value) |
|
71
|
|
|
{ |
|
72
|
|
|
$this->text = array_merge($this->text ?? [], [$locale => $value]); |
|
73
|
|
|
|
|
74
|
|
|
return $this; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
protected function flushGroupCache() |
|
78
|
|
|
{ |
|
79
|
|
|
foreach ($this->getTranslatedLocales() as $locale) { |
|
80
|
|
|
Cache::forget(static::getCacheKey($this->group, $locale)); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
protected function getTranslatedLocales(): array |
|
85
|
|
|
{ |
|
86
|
|
|
return array_keys($this->text); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|