I18n::has()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Suricate;
6
7
/**
8
 * Translation extension for Suricate
9
 *
10
 * @package Suricate
11
 * @author  Mathieu LESNIAK <[email protected]>
12
 *
13
 * @property string $locale
14
 */
15
16
class I18n extends Service
17
{
18
    protected $parametersList = ['locale'];
19
    private $baseLocaleDir = 'i18n';
20
    private $translations;
21
22
    /**
23
     * Get the list of installed languages for application
24
     * @return array Array of available languages of application
25
     */
26
    public function i18nList()
27
    {
28
        $langDir = app_path() . DIRECTORY_SEPARATOR . $this->baseLocaleDir;
29
30
        $langList = [];
31
        $iterator = new \DirectoryIterator($langDir);
32
33
        foreach ($iterator as $currentFile) {
34
            if (
35
                $currentFile->isDir() &&
36
                !$currentFile->isDot() &&
37
                is_readable(
38
                    $currentFile->getPathname() .
39
                        DIRECTORY_SEPARATOR .
40
                        'language.php'
41
                )
42
            ) {
43
                $langList[
44
                    $currentFile->getFilename()
45
                ] = $currentFile->getFilename();
46
            }
47
        }
48
        asort($langList);
49
50
        return $langList;
51
    }
52
53
    public function load($locale = null)
54
    {
55
        $filename =
56
            app_path() .
57
            DIRECTORY_SEPARATOR .
58
            $this->baseLocaleDir .
59
            DIRECTORY_SEPARATOR .
60
            $locale .
61
            DIRECTORY_SEPARATOR .
62
            'language.php';
63
64
        if (is_readable($filename)) {
65
            $this->locale = $locale;
66
            $this->translations = include $filename;
67
        } else {
68
            Suricate::Logger()->debug(
69
                sprintf('Missing translation file for %s', $this->locale)
70
            );
71
        }
72
    }
73
74
    public function get()
75
    {
76
        $args = func_get_args();
77
78
        if (isset($args[0])) {
79
            $str = $args[0];
80
81
            if ($this->translations === null) {
82
                $this->load($this->locale);
83
            }
84
85
            if (isset($this->translations[$str])) {
86
                if (isset($args[1])) {
87
                    array_shift($args);
88
                    return vsprintf($this->translations[$str], $args);
89
                } else {
90
                    return $this->translations[$str];
91
                }
92
            } else {
93
                return $str;
94
            }
95
        }
96
    }
97
98
    public function choice()
99
    {
100
        $args = func_get_args();
101
102
        $str = array_shift($args);
103
        $number = array_shift($args);
104
105
        if ($this->translations === null) {
106
            $this->load($this->locale);
107
        }
108
        if (isset($this->translations[$str])) {
109
            if (strpos($this->translations[$str], '|') !== false) {
110
                list($single, $plural) = explode(
111
                    '|',
112
                    $this->translations[$str]
113
                );
114
                if ($number > 1) {
115
                    return vsprintf($plural, $args);
116
                } else {
117
                    return vsprintf($single, $args);
118
                }
119
            } else {
120
                return $this->translations[$str];
121
            }
122
        } else {
123
            return $str;
124
        }
125
    }
126
127
    public function has($str)
128
    {
129
        return isset($this->translations[$str]);
130
    }
131
}
132