1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* MIT License |
4
|
|
|
|
5
|
|
|
Copyright (c) 2018 Eridan Domoratskiy |
6
|
|
|
|
7
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy |
8
|
|
|
of this software and associated documentation files (the "Software"), to deal |
9
|
|
|
in the Software without restriction, including without limitation the rights |
10
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
11
|
|
|
copies of the Software, and to permit persons to whom the Software is |
12
|
|
|
furnished to do so, subject to the following conditions: |
13
|
|
|
|
14
|
|
|
The above copyright notice and this permission notice shall be included in all |
15
|
|
|
copies or substantial portions of the Software. |
16
|
|
|
|
17
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
18
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
19
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
20
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
21
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
22
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
23
|
|
|
SOFTWARE. */ |
24
|
|
|
|
25
|
|
|
namespace MaintenanceScreen\TranslatorProvider; |
26
|
|
|
|
27
|
|
|
use MaintenanceScreen\Translator; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Abstract provider of Translator instances |
31
|
|
|
* |
32
|
|
|
* @author ProgMiner |
33
|
|
|
*/ |
34
|
|
|
abstract class AbstractTranslatorProvider implements ITranslatorProvider { |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Makes Translator for language |
38
|
|
|
* |
39
|
|
|
* @param string $lang Language name |
40
|
|
|
* |
41
|
|
|
* @return Translator |
42
|
|
|
*/ |
43
|
|
|
protected abstract function _getTranslator(string $lang): Translator; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
public function getTranslator(string $lang): Translator { |
49
|
|
|
$lang = str_replace('-', '_', $lang); |
50
|
|
|
$args = func_get_args(); |
51
|
|
|
|
52
|
|
|
$exception = null; |
|
|
|
|
53
|
|
|
try { |
54
|
|
|
return call_user_func_array([$this, '_getTranslator'], $args); |
55
|
|
|
} catch (\Throwable $e) { |
56
|
|
|
$exception = $e; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if (false !== ($pos = strpos($lang, '_'))) { |
60
|
|
|
try { |
61
|
|
|
array_shift($args); |
62
|
|
|
array_unshift($args, substr($lang, 0, $pos)); |
63
|
|
|
|
64
|
|
|
return call_user_func_array([$this, 'getTranslator'], $args); |
65
|
|
|
} catch (\Throwable $e) {} |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
throw $exception; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
|
|
public function getPreferredTranslator(array $langs, $defaultLang = null): Translator { |
75
|
|
|
if (!is_null($defaultLang)) { |
76
|
|
|
$langs[] = $defaultLang; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$args = func_get_args(); |
80
|
|
|
array_shift($args); |
81
|
|
|
|
82
|
|
|
$exception = null; |
|
|
|
|
83
|
|
|
foreach ($langs as $lang) { |
84
|
|
|
try { |
85
|
|
|
array_shift($args); |
86
|
|
|
array_unshift($args, $lang); |
87
|
|
|
|
88
|
|
|
return call_user_func_array([$this, 'getTranslator'], $args); |
89
|
|
|
} catch (\Throwable $e) { |
90
|
|
|
$exception = $e; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
throw new \RuntimeException('No one language exists', 0, $e); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|