|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\TranslationLoader; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
use Illuminate\Support\Arr; |
|
7
|
|
|
use Illuminate\Support\Facades\Cache; |
|
8
|
|
|
|
|
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 |
|
55
|
|
|
{ |
|
56
|
|
|
return "spatie.translation-loader.{$group}.{$locale}"; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param string $locale |
|
61
|
|
|
* |
|
62
|
|
|
* @return string |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getTranslation(string $locale): ?string |
|
65
|
|
|
{ |
|
66
|
|
|
if (! isset($this->text[$locale])) { |
|
67
|
|
|
$fallback = config('app.fallback_locale'); |
|
68
|
|
|
|
|
69
|
|
|
return $this->text[$fallback] ?? null; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $this->text[$locale]; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param string $locale |
|
77
|
|
|
* @param string $value |
|
78
|
|
|
* |
|
79
|
|
|
* @return $this |
|
80
|
|
|
*/ |
|
81
|
|
|
public function setTranslation(string $locale, string $value) |
|
82
|
|
|
{ |
|
83
|
|
|
$this->text = array_merge($this->text ?? [], [$locale => $value]); |
|
84
|
|
|
|
|
85
|
|
|
return $this; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function flushGroupCache() |
|
89
|
|
|
{ |
|
90
|
|
|
foreach ($this->getTranslatedLocales() as $locale) { |
|
91
|
|
|
Cache::forget(static::getCacheKey($this->group, $locale)); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
protected function getTranslatedLocales(): array |
|
96
|
|
|
{ |
|
97
|
|
|
return array_keys($this->text); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|