Passed
Push — main ( 60ecfc...80f61e )
by Thierry
01:51
created

Translator::lang()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 34
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 34
rs 10
1
<?php
2
3
namespace Lagdo\DbAdmin\Db;
4
5
use Lagdo\DbAdmin\Driver\TranslatorInterface;
6
7
use function is_array;
8
use function array_key_exists;
9
use function func_get_args;
10
use function array_shift;
11
use function str_replace;
12
use function vsprintf;
13
use function strtr;
14
use function number_format;
15
use function preg_split;
16
use function max;
17
use function microtime;
18
19
/**
20
 * Translator and language switcher
21
 *
22
 * Not used in a single language version
23
 */
24
class Translator implements TranslatorInterface
25
{
26
    /**
27
     * Available languages
28
     *
29
     * @var array
30
     */
31
    /*protected $languages = [
32
        'en' => 'English', // Jakub Vrána - https://www.vrana.cz
33
        'ar' => 'العربية', // Y.M Amine - Algeria - [email protected]
34
        'bg' => 'Български', // Deyan Delchev
35
        'bn' => 'বাংলা', // Dipak Kumar - [email protected]
36
        'bs' => 'Bosanski', // Emir Kurtovic
37
        'ca' => 'Català', // Joan Llosas
38
        'cs' => 'Čeština', // Jakub Vrána - https://www.vrana.cz
39
        'da' => 'Dansk', // Jarne W. Beutnagel - [email protected]
40
        'de' => 'Deutsch', // Klemens Häckel - http://clickdimension.wordpress.com
41
        'el' => 'Ελληνικά', // Dimitrios T. Tanis - [email protected]
42
        'es' => 'Español', // Klemens Häckel - http://clickdimension.wordpress.com
43
        'et' => 'Eesti', // Priit Kallas
44
        'fa' => 'فارسی', // mojtaba barghbani - Iran - [email protected], Nima Amini - http://nimlog.com
45
        'fi' => 'Suomi', // Finnish - Kari Eveli - http://www.lexitec.fi/
46
        'fr' => 'Français', // Francis Gagné, Aurélien Royer
47
        'gl' => 'Galego', // Eduardo Penabad Ramos
48
        'he' => 'עברית', // Binyamin Yawitz - https://stuff-group.com/
49
        'hu' => 'Magyar', // Borsos Szilárd (Borsosfi) - http://www.borsosfi.hu, [email protected]
50
        'id' => 'Bahasa Indonesia', // Ivan Lanin - http://ivan.lanin.org
51
        'it' => 'Italiano', // Alessandro Fiorotto, Paolo Asperti
52
        'ja' => '日本語', // Hitoshi Ozawa - http://sourceforge.jp/projects/oss-ja-jpn/releases/
53
        'ka' => 'ქართული', // Saba Khmaladze [email protected]
54
        'ko' => '한국어', // dalli - [email protected]
55
        'lt' => 'Lietuvių', // Paulius Leščinskas - http://www.lescinskas.lt
56
        'ms' => 'Bahasa Melayu', // Pisyek
57
        'nl' => 'Nederlands', // Maarten Balliauw - http://blog.maartenballiauw.be
58
        'no' => 'Norsk', // Iver Odin Kvello, mupublishing.com
59
        'pl' => 'Polski', // Radosław Kowalewski - http://srsbiz.pl/
60
        'pt' => 'Português', // André Dias
61
        'pt-br' => 'Português (Brazil)', // Gian Live - [email protected], Davi Alexandre [email protected], RobertoPC - http://www.robertopc.com.br
62
        'ro' => 'Limba Română', // .nick .messing - [email protected]
63
        'ru' => 'Русский', // Maksim Izmaylov; Andre Polykanine - https://github.com/Oire/
64
        'sk' => 'Slovenčina', // Ivan Suchy - http://www.ivansuchy.com, Juraj Krivda - http://www.jstudio.cz
65
        'sl' => 'Slovenski', // Matej Ferlan - www.itdinamik.com, [email protected]
66
        'sr' => 'Српски', // Nikola Radovanović - [email protected]
67
        'sv' => 'Svenska', // rasmusolle - https://github.com/rasmusolle
68
        'ta' => 'த‌மிழ்', // G. Sampath Kumar, Chennai, India, [email protected]
69
        'th' => 'ภาษาไทย', // Panya Saraphi, [email protected] - http://www.opencart2u.com/
70
        'tr' => 'Türkçe', // Bilgehan Korkmaz - turktron.com
71
        'uk' => 'Українська', // Valerii Kryzhov
72
        'vi' => 'Tiếng Việt', // Giang Manh @ manhgd google mail
73
        'zh' => '简体中文', // Mr. Lodar, vea - urn2.net - [email protected]
74
        'zh-tw' => '繁體中文', // http://tzangms.com
75
    ];*/
76
77
    /**
78
     * Current language
79
     *
80
     * @var string
81
     */
82
    protected $language;
83
84
    /**
85
     * Available translations
86
     *
87
     * @var array
88
     */
89
    protected $translations;
90
91
    /**
92
     * The constructor
93
     */
94
    public function __construct()
95
    {
96
        $this->setLanguage('en');
97
    }
98
99
    /**
100
     * Set the current language
101
     *
102
     * @param string $language
103
     *
104
     * @return void
105
     */
106
    public function setLanguage(string $language)
107
    {
108
        $this->language = $language;
109
        $this->translations = require __DIR__ . "/../../translations/$language.inc.php";
110
    }
111
112
    /**
113
     * Get the current language
114
     *
115
     * @return string
116
     */
117
    /*public function getLanguage()
118
    {
119
        return $this->language;
120
    }*/
121
122
    /**
123
     * Get a translated string
124
     *
125
     * @param string $string
126
     * @param mixed $number
127
     *
128
     * @return string
129
     */
130
    public function lang(string $string, $number = null): string
131
    {
132
        /*if (array_key_exists($string, $this->translations)) {
133
            $string = $this->translations[$string];
134
        }
135
        if (is_array($string)) {
136
            $pos = ($number == 1 ? 0
137
                // different forms for 1, 2-4, other
138
                : ($this->language == 'cs' || $this->language == 'sk' ? ($number && $number < 5 ? 1 : 2)
139
                // different forms for 0-1, other
140
                : ($this->language == 'fr' ? (!$number ? 0 : 1)
141
                // different forms for 1, 2-4 except 12-14, other
142
                : ($this->language == 'pl' ? ($number % 10 > 1 && $number % 10 < 5 && $number / 10 % 10 != 1 ? 1 : 2)
143
                // different forms for 1, 2, 3-4, other
144
                : ($this->language == 'sl' ? ($number % 100 == 1 ? 0 : ($number % 100 == 2 ? 1 :
145
                    ($number % 100 == 3 || $number % 100 == 4 ? 2 : 3)))
146
                // different forms for 1, 12-19, other
147
                : ($this->language == 'lt' ? ($number % 10 == 1 && $number % 100 != 11 ? 0 :
148
                    ($number % 10 > 1 && $number / 10 % 10 != 1 ? 1 : 2))
149
                // different forms for 1 except 11, 2-4 except 12-14, other
150
                : ($this->language == 'bs' || $this->language == 'ru' || $this->language == 'sr' || $this->language == 'uk' ?
151
                    ($number % 10 == 1 && $number % 100 != 11 ? 0
152
                    : ($number % 10 > 1 && $number % 10 < 5 && $number / 10 % 10 != 1 ? 1 : 2))
153
                : 1 // different forms for 1, other
154
            ))))))); // http://www.gnu.org/software/gettext/manual/html_node/Plural-forms.html
155
            $string = $string[$pos];
156
        }*/
157
        $args = func_get_args();
158
        array_shift($args);
159
        $format = str_replace("%d", "%s", $string);
160
        if ($format !== $string) {
161
            $args[0] = $this->formatNumber($number);
162
        }
163
        return vsprintf($format, $args);
164
    }
165
166
    /**
167
     * Format a decimal number
168
     *
169
     * @param int $number
170
     *
171
     * @return string
172
     */
173
    public function formatNumber(int $number): string
174
    {
175
        return strtr(number_format($number, 0, ".", $this->lang(',')),
176
            preg_split('~~u', $this->lang('0123456789'), -1, PREG_SPLIT_NO_EMPTY));
177
    }
178
179
    /**
180
     * Format elapsed time
181
     *
182
     * @param float $time Output of microtime(true)
183
     *
184
     * @return string
185
     */
186
    public function formatTime(float $time): string
187
    {
188
        return $this->lang('%.3f s', max(0, microtime(true) - $time));
189
    }
190
}
191