|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* Copyright (C) 2024 Rafael San José <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* This program is free software; you can redistribute it and/or modify |
|
6
|
|
|
* it under the terms of the GNU General Public License as published by |
|
7
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
|
8
|
|
|
* any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* This program is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
* GNU General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU General Public License |
|
16
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace Alxarafe\Lib; |
|
20
|
|
|
|
|
21
|
|
|
use Alxarafe\Tools\Debug; |
|
22
|
|
|
use DebugBar\DebugBar; |
|
23
|
|
|
use Symfony\Component\Translation\Translator; |
|
24
|
|
|
use Symfony\Component\Translation\Loader\ArrayLoader; |
|
25
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
26
|
|
|
|
|
27
|
|
|
abstract class Trans |
|
28
|
|
|
{ |
|
29
|
|
|
public const FALLBACK_LANG = 'en'; |
|
30
|
|
|
|
|
31
|
|
|
protected static $translator; |
|
32
|
|
|
|
|
33
|
|
|
private static $translations = []; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* List of strings without translation, for debugging bar purposes. |
|
37
|
|
|
* |
|
38
|
|
|
* @var array |
|
39
|
|
|
*/ |
|
40
|
|
|
private static $missingStrings = []; |
|
41
|
|
|
|
|
42
|
|
|
private static $missingStringsDebug; |
|
43
|
|
|
|
|
44
|
|
|
public static function getInstance() |
|
45
|
|
|
{ |
|
46
|
|
|
if (!isset(static::$translator)) { |
|
47
|
|
|
static::initialize(); |
|
48
|
|
|
} |
|
49
|
|
|
return static::$translator; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Initializes the translator. |
|
54
|
|
|
* |
|
55
|
|
|
* @return bool |
|
56
|
|
|
*/ |
|
57
|
|
|
private static function initialize() |
|
58
|
|
|
{ |
|
59
|
|
|
if (isset(static::$translator)) { |
|
60
|
|
|
return true; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
self::$translator = new Translator(static::FALLBACK_LANG); |
|
64
|
|
|
return isset(static::$translator); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public static function getMissingStrings() |
|
68
|
|
|
{ |
|
69
|
|
|
static::$missingStringsDebug = []; |
|
|
|
|
|
|
70
|
|
|
$locale = static::$translator->getLocale(); |
|
71
|
|
|
foreach (static::$missingStrings as $name => $value) { |
|
|
|
|
|
|
72
|
|
|
if ($value['lang'] === $locale) { |
|
73
|
|
|
continue; |
|
74
|
|
|
} |
|
75
|
|
|
static::$missingStringsDebug[$name] = '(' . $value['lang'] . ') ' . $value['text']; |
|
76
|
|
|
} |
|
77
|
|
|
return static::$missingStringsDebug; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Return a text in the selected language. |
|
82
|
|
|
* |
|
83
|
|
|
* @param $message |
|
84
|
|
|
* @param array $parameters |
|
85
|
|
|
* @param $locale |
|
86
|
|
|
* @return mixed |
|
87
|
|
|
*/ |
|
88
|
|
|
public static function _($message, array $parameters = [], $locale = null) |
|
89
|
|
|
{ |
|
90
|
|
|
if (!isset(static::$translator)) { |
|
91
|
|
|
static::initialize(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
if (!isset($locale)) { |
|
95
|
|
|
$locale = static::$translator->getLocale(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
if (empty(static::$translations)) { |
|
|
|
|
|
|
99
|
|
|
static::$translations = static::getTranslations($locale ?? self::FALLBACK_LANG); |
|
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$params = []; |
|
103
|
|
|
foreach ($parameters as $name => $value) { |
|
104
|
|
|
$params['%' . $name . '%'] = $value; |
|
105
|
|
|
} |
|
106
|
|
|
return self::$translator->trans($message, $params, null, $locale); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
private static function getTranslations($lang) |
|
110
|
|
|
{ |
|
111
|
|
|
if (!isset(static::$translator)) { |
|
112
|
|
|
static::initialize(); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
if (!isset(static::$translations)) { |
|
|
|
|
|
|
116
|
|
|
$module = $_GET['module']; |
|
117
|
|
|
$main_route = realpath(constant('BASE_PATH') . '/../vendor/rsanjoseo/alxarafe/src'); |
|
118
|
|
|
$routes = [ |
|
119
|
|
|
$main_route . '/Lang', |
|
120
|
|
|
$main_route . '/Modules/' . $module . '/Lang', |
|
121
|
|
|
]; |
|
122
|
|
|
|
|
123
|
|
|
self::$translator->addLoader('array', new ArrayLoader()); |
|
124
|
|
|
foreach ($routes as $route) { |
|
125
|
|
|
if ($lang !== self::FALLBACK_LANG) { |
|
126
|
|
|
static::loadLang(self::FALLBACK_LANG, $route); |
|
127
|
|
|
} |
|
128
|
|
|
static::loadLang($lang, $route); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
self::$translator->addResource('array', static::$translations, $lang); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Loads language files for the specified path and language. |
|
137
|
|
|
* |
|
138
|
|
|
* @param string $lang |
|
139
|
|
|
* @param string $folder |
|
140
|
|
|
*/ |
|
141
|
|
|
private static function loadLang(string $lang, string $folder): void |
|
142
|
|
|
{ |
|
143
|
|
|
if (strlen($lang) > 2) { |
|
144
|
|
|
static::loadLang(substr($lang, 0, 2), $folder); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
$filename = $folder . '/' . $lang . '.yaml'; |
|
148
|
|
|
if (!file_exists($filename)) { |
|
149
|
|
|
Debug::message($filename . ' not found'); |
|
150
|
|
|
return; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
$translates = Yaml::parseFile($filename); |
|
154
|
|
|
if (!is_array($translates)) { |
|
155
|
|
|
Debug::message($filename . ' is not a valid YAML file'); |
|
156
|
|
|
return; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
foreach ($translates as $name => $translate) { |
|
160
|
|
|
static::$translations[$name] = $translate; |
|
|
|
|
|
|
161
|
|
|
static::$missingStrings[$name] = [ |
|
|
|
|
|
|
162
|
|
|
'lang' => $lang, |
|
163
|
|
|
'text' => $translate |
|
164
|
|
|
]; |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Return a text in the selected language. |
|
170
|
|
|
* Call directly to extra short function _ |
|
171
|
|
|
* |
|
172
|
|
|
* @param $message |
|
173
|
|
|
* @param array $parameters |
|
174
|
|
|
* @param $domain |
|
175
|
|
|
* @param $locale |
|
176
|
|
|
* @return mixed |
|
177
|
|
|
*/ |
|
178
|
|
|
public static function trans($message, array $parameters = [], $domain = 'messages', $locale = null) |
|
|
|
|
|
|
179
|
|
|
{ |
|
180
|
|
|
return static::_($message, $parameters, $locale); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Loads core and module lang files |
|
185
|
|
|
* |
|
186
|
|
|
* @param $lang |
|
187
|
|
|
* @return void |
|
188
|
|
|
*/ |
|
189
|
|
|
public static function setLang($lang) |
|
190
|
|
|
{ |
|
191
|
|
|
self::$translator->setLocale($lang); |
|
192
|
|
|
self::getTranslations($lang); |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
|