1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Thinktomorrow\Squanto\Translators; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Schema; |
6
|
|
|
use Illuminate\Translation\Translator as LaravelTranslator; |
7
|
|
|
use Thinktomorrow\Squanto\Domain\LineKey; |
8
|
|
|
|
9
|
|
|
class SquantoTranslator extends LaravelTranslator implements Translator |
10
|
|
|
{ |
11
|
|
|
private $databaseTranslator; |
12
|
|
|
|
13
|
69 |
|
private $keyAsDefault = true; |
14
|
|
|
private $isDatabaseAlreadyMigrated = null; |
15
|
69 |
|
|
16
|
69 |
|
public function setKeyAsDefault($keyAsDefault = true) |
17
|
|
|
{ |
18
|
|
|
$this->keyAsDefault = $keyAsDefault; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Get the translation for the given key by following this priority chain: |
23
|
|
|
* |
24
|
|
|
* 1. Get from our cached translations |
25
|
|
|
* 2. Get from database |
26
|
|
|
* 3. Get from the /resources/lang |
27
|
|
|
* |
28
|
|
|
* @param string $key |
29
|
|
|
* @param array $replace |
30
|
|
|
* @param string $locale |
31
|
21 |
|
* @param bool $fallback |
32
|
|
|
* @return string |
33
|
21 |
|
*/ |
34
|
|
|
public function get($key, array $replace = array(), $locale = null, $fallback = true) |
35
|
21 |
|
{ |
36
|
12 |
|
$locale = $locale ?: $this->getLocale(); |
37
|
|
|
|
38
|
|
|
if($result = $this->getFromExcludedSource($key, $replace, $locale, $fallback)) { |
39
|
12 |
|
return $result; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if ($result = $this->getFromCache($key, $replace, $locale, $fallback)) { |
43
|
12 |
|
return $result; |
44
|
|
|
} |
45
|
12 |
|
|
46
|
|
|
if ($result = $this->getFromDatabase($key, $replace, $locale, $fallback)) { |
47
|
|
|
return $result; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$result = parent::get($key, $replace, $locale, $fallback); |
51
|
|
|
|
52
|
|
|
return ($this->keyAsDefault || $result !== $key) ? $result : null; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
21 |
|
* Get from excluded sources. This is used here to make retrieval of these |
57
|
|
|
* non-managed translations a lot faster by going straight to source |
58
|
21 |
|
* |
59
|
21 |
|
* @param $key |
60
|
|
|
* @param $replace |
61
|
|
|
* @param $locale |
62
|
21 |
|
* @param $fallback |
63
|
|
|
* @return array|null|string |
64
|
21 |
|
*/ |
65
|
|
|
private function getFromExcludedSource($key, $replace, $locale, $fallback) |
66
|
|
|
{ |
67
|
12 |
|
if(!LineKey::fromString($key)->isExcludedSource()) return null; |
68
|
|
|
|
69
|
12 |
|
return parent::get($key, $replace, $locale, $fallback); |
|
|
|
|
70
|
12 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
12 |
|
* Retrieve the translation from the squanto cache. |
74
|
|
|
* |
75
|
|
|
* @param $key |
76
|
|
|
* @param array $replace |
77
|
|
|
* @param null $locale |
78
|
|
|
* @return mixed|null |
79
|
|
|
*/ |
80
|
|
|
private function getFromCache($key, array $replace = array(), $locale = null, $fallback = true) |
81
|
|
|
{ |
82
|
|
|
if (false === strpos($key, 'squanto::')) { |
83
|
|
|
$key = 'squanto::'.$key; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$result = parent::get($key, $replace, $locale, $fallback); |
|
|
|
|
87
|
|
|
|
88
|
|
|
return ($result !== $key) ? $result : null; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
private function getFromDatabase($key, array $replace = array(), $locale = null, $fallback = true) |
92
|
|
|
{ |
93
|
|
|
/** |
94
|
|
|
* If database tables are not present we will soft ignore this call and delegate to the native |
95
|
|
|
*/ |
96
|
|
|
if (! $this->isDatabaseAlreadyMigrated()) { |
97
|
|
|
return null; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if (!isset($this->databaseTranslator)) { |
101
|
|
|
$this->databaseTranslator = app(DatabaseTranslator::class); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $this->databaseTranslator->get($key, $replace, $locale, $fallback); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Verify that SQUANTO migrations are already run and present in this environment |
109
|
|
|
* Allow for a soft install |
110
|
|
|
* |
111
|
|
|
* @return null |
112
|
|
|
*/ |
113
|
|
|
private function isDatabaseAlreadyMigrated() |
114
|
|
|
{ |
115
|
|
|
if (!is_null($this->isDatabaseAlreadyMigrated)) { |
116
|
|
|
return $this->isDatabaseAlreadyMigrated; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return ($this->isDatabaseAlreadyMigrated = Schema::hasTable('squanto_lines')); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.