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