|
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 Symfony\Component\Translation\Translator; |
|
22
|
|
|
use Symfony\Component\Translation\Loader\ArrayLoader; |
|
23
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
24
|
|
|
|
|
25
|
|
|
abstract class Trans |
|
26
|
|
|
{ |
|
27
|
|
|
protected static $translator; |
|
28
|
|
|
|
|
29
|
|
|
public static function _($message, array $parameters = [], $locale = null) |
|
30
|
|
|
{ |
|
31
|
|
|
if (!isset(static::$translator)) { |
|
32
|
|
|
static::initialize(); |
|
33
|
|
|
} |
|
34
|
|
|
return self::$translator->trans($message, $parameters, null, $locale); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
private static function initialize() |
|
38
|
|
|
{ |
|
39
|
|
|
if (self::$translator === null) { |
|
40
|
|
|
self::$translator = new Translator($locale); |
|
|
|
|
|
|
41
|
|
|
self::$translator->addLoader('array', new ArrayLoader()); |
|
42
|
|
|
|
|
43
|
|
|
// Cargar todos los archivos YAML del directorio de traducción |
|
44
|
|
|
$files = glob($translationDir . '/' . $locale . '/*.yaml'); |
|
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
foreach ($files as $file) { |
|
47
|
|
|
$messages = Yaml::parseFile($file); |
|
48
|
|
|
self::$translator->addResource('array', $messages, $locale); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public static function trans($message, array $parameters = [], $domain = 'messages', $locale = null) |
|
|
|
|
|
|
54
|
|
|
{ |
|
55
|
|
|
return static::_($message, $parameters, $locale); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
private static function loadLang($lang, $folder): array |
|
59
|
|
|
{ |
|
60
|
|
|
$result = []; |
|
61
|
|
|
if (strlen($lang) > 2) { |
|
62
|
|
|
$result = static::loadLang(strpos($lang, 0, 2), $folder); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return array_merge($result, Yaml::parseFile($folder . $lang . '.yaml')); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public static function setLang($lang) |
|
69
|
|
|
{ |
|
70
|
|
|
$route =realpath(BASE_PATH.'/../vendor/rsanjoseo/alxarafe/src'); |
|
|
|
|
|
|
71
|
|
|
foreach([$route.'/Lang',$route.'/Modules/Admin'] as $path) { |
|
72
|
|
|
dd([$path=>static::loadLang($lang, $path)]); |
|
73
|
|
|
} |
|
74
|
|
|
die($lang); |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
} |