I18n   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 61
c 2
b 0
f 0
dl 0
loc 112
ccs 0
cts 59
cp 0
rs 10
wmc 18

5 Methods

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