1 | <?php |
||
9 | class LanguageLine extends Model |
||
10 | { |
||
11 | /** @var array */ |
||
12 | public $translatable = ['text']; |
||
13 | |||
14 | /** @var array */ |
||
15 | public $guarded = ['id']; |
||
16 | |||
17 | /** @var array */ |
||
18 | protected $casts = ['text' => 'array']; |
||
19 | |||
20 | public static function boot() |
||
21 | { |
||
22 | parent::boot(); |
||
23 | |||
24 | $flushGroupCache = function (self $languageLine) { |
||
25 | $languageLine->flushGroupCache(); |
||
26 | }; |
||
27 | |||
28 | static::saved($flushGroupCache); |
||
29 | static::deleted($flushGroupCache); |
||
30 | } |
||
31 | |||
32 | public static function getTranslationsForGroup(string $locale, string $group): array |
||
33 | { |
||
34 | return Cache::rememberForever(static::getCacheKey($group, $locale), function () use ($group, $locale) { |
||
35 | return static::query() |
||
36 | ->where('group', $group) |
||
37 | ->get() |
||
38 | ->reduce(function ($lines, self $languageLine) use ($group, $locale) { |
||
39 | $translation = $languageLine->getTranslation($locale); |
||
40 | |||
41 | if ($translation !== null && $group === '*') { |
||
42 | // Make a flat array when returning json translations |
||
43 | $lines[$languageLine->key] = $translation; |
||
44 | } elseif ($translation !== null && $group !== '*') { |
||
45 | // Make a nesetd array when returning normal translations |
||
46 | Arr::set($lines, $languageLine->key, $translation); |
||
47 | } |
||
48 | |||
49 | return $lines; |
||
50 | }) ?? []; |
||
51 | }); |
||
52 | } |
||
53 | |||
54 | public static function getCacheKey(string $group, string $locale): string |
||
58 | |||
59 | /** |
||
60 | * @param string $locale |
||
61 | * |
||
62 | * @return string |
||
63 | */ |
||
64 | public function getTranslation(string $locale): ?string |
||
74 | |||
75 | /** |
||
76 | * @param string $locale |
||
77 | * @param string $value |
||
78 | * |
||
79 | * @return $this |
||
80 | */ |
||
81 | public function setTranslation(string $locale, string $value) |
||
87 | |||
88 | public function flushGroupCache() |
||
94 | |||
95 | protected function getTranslatedLocales(): array |
||
99 | } |
||
100 |