1
|
|
|
<?php namespace Modules\Translation\Repositories\Eloquent; |
2
|
|
|
|
3
|
|
|
use Modules\Core\Repositories\Eloquent\EloquentBaseRepository; |
4
|
|
|
use Modules\Translation\Entities\TranslationTranslation; |
5
|
|
|
use Modules\Translation\Repositories\TranslationRepository; |
6
|
|
|
|
7
|
|
|
class EloquentTranslationRepository extends EloquentBaseRepository implements TranslationRepository |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @param string $key |
11
|
|
|
* @param string $locale |
12
|
|
|
* @return string |
13
|
|
|
*/ |
14
|
|
|
public function findByKeyAndLocale($key, $locale = null) |
15
|
|
|
{ |
16
|
|
|
$locale = $locale ?: app()->getLocale(); |
17
|
|
|
|
18
|
|
|
$translation = $this->model->whereKey($key)->with('translations')->first(); |
19
|
|
|
if ($translation && $translation->hasTranslation($locale)) { |
20
|
|
|
return $translation->translate($locale)->value; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
return ''; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function allFormatted() |
27
|
|
|
{ |
28
|
|
|
$allRows = $this->all(); |
29
|
|
|
$allDatabaseTranslations = []; |
30
|
|
|
foreach ($allRows as $translation) { |
31
|
|
|
foreach (config('laravellocalization.supportedLocales') as $locale => $language) { |
32
|
|
|
if ($translation->hasTranslation($locale)) { |
33
|
|
|
$allDatabaseTranslations[$locale][$translation->key] = $translation->translate($locale)->value; |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return $allDatabaseTranslations; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function saveTranslationForLocaleAndKey($locale, $key, $value) |
42
|
|
|
{ |
43
|
|
|
$translation = $this->findTranslationByKey($key); |
44
|
|
|
$translation->translateOrNew($locale)->value = $value; |
45
|
|
|
$translation->save(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function findTranslationByKey($key) |
49
|
|
|
{ |
50
|
|
|
return $this->model->firstOrCreate(['key' => $key]); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Update the given translation key with the given data |
55
|
|
|
* @param string $key |
56
|
|
|
* @param array $data |
57
|
|
|
* @return mixed |
58
|
|
|
*/ |
59
|
|
|
public function updateFromImport($key, array $data) |
60
|
|
|
{ |
61
|
|
|
$translation = $this->findTranslationByKey($key); |
62
|
|
|
$translation->update($data); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Set the given value on the given TranslationTranslation |
67
|
|
|
* @param TranslationTranslation $translationTranslation |
68
|
|
|
* @param string $value |
69
|
|
|
* @return void |
70
|
|
|
*/ |
71
|
|
|
public function updateTranslationToValue(TranslationTranslation $translationTranslation, $value) |
72
|
|
|
{ |
73
|
|
|
$translationTranslation->value = $value; |
|
|
|
|
74
|
|
|
$translationTranslation->save(); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.