Test Failed
Push — main ( 3d651d...c1b661 )
by Rafael
05:29
created

Trans::setLang()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
dl 0
loc 7
rs 10
c 1
b 0
f 0
cc 2
nc 2
nop 1
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);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $locale seems to be never defined.
Loading history...
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');
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $translationDir seems to be never defined.
Loading history...
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)
0 ignored issues
show
Unused Code introduced by
The parameter $domain is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

53
    public static function trans($message, array $parameters = [], /** @scrutinizer ignore-unused */ $domain = 'messages', $locale = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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');
0 ignored issues
show
Bug introduced by
The constant Alxarafe\Lib\BASE_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
71
        foreach([$route.'/Lang',$route.'/Modules/Admin'] as $path) {
72
            dd([$path=>static::loadLang($lang, $path)]);
73
        }
74
        die($lang);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
75
    }
76
77
}