Completed
Pull Request — master (#357)
by Anton
03:23
created

Translator::initOptions()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 23
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12
Metric Value
dl 0
loc 23
ccs 0
cts 13
cp 0
rs 9.0856
cc 3
eloc 10
nc 4
nop 0
crap 12
1
<?php
2
/**
3
 * Bluz Framework Component
4
 *
5
 * @copyright Bluz PHP Team
6
 * @link https://github.com/bluzphp/framework
7
 */
8
9
/**
10
 * @namespace
11
 */
12
namespace Bluz\Translator;
13
14
use Bluz\Common\Exception\ConfigurationException;
15
use Bluz\Common\Options;
16
17
/**
18
 * Translator based on gettext library
19
 *
20
 * @package  Bluz\Translator
21
 * @author   Anton Shevchuk
22
 * @link     https://github.com/bluzphp/framework/wiki/Translator
23
 */
24
class Translator
25
{
26
    use Options;
27
28
    /**
29
     * Locale
30
     *
31
     * @var string
32
     * @link http://www.loc.gov/standards/iso639-2/php/code_list.php
33
     */
34
    protected $locale = 'en_US';
35
36
    /**
37
     * @var string text domain
38
     */
39
    protected $domain = 'messages';
40
41
    /**
42
     * @var string path to text domain files
43
     */
44
    protected $path;
45
46
    /**
47
     * Set domain
48
     *
49
     * @param  string $domain
50
     * @return self
51
     */
52 3
    public function setDomain($domain)
53
    {
54 3
        $this->domain = $domain;
55 3
        return $this;
56
    }
57
58
    /**
59
     * Set locale
60
     *
61
     * @param  string $locale
62
     * @return self
63
     */
64 3
    public function setLocale($locale)
65
    {
66 3
        $this->locale = $locale;
67 3
        return $this;
68
    }
69
70
    /**
71
     * Set path to l10n
72
     *
73
     * @param  string $path
74
     * @return self
75
     */
76 3
    public function setPath($path)
77
    {
78 3
        $this->path = $path;
79 3
        return $this;
80
    }
81
82
    /**
83
     * Initialization
84
     *
85
     * @return void
86
     * @throw  \Bluz\Config\ConfigException
87
     */
88
    protected function initOptions()
89
    {
90
        // Setup locale
91
        putenv('LC_ALL=' . $this->locale);
92
        putenv('LANG=' . $this->locale);
93
        putenv('LANGUAGE=' . $this->locale);
94
95
        // Windows workaround
96
        if (!defined('LC_MESSAGES')) {
97
            define('LC_MESSAGES', 6);
98
        }
99
100
        setlocale(LC_MESSAGES, $this->locale);
101
102
        // For gettext only
103
        if (function_exists('gettext')) {
104
            // Setup domain path
105
            $this->addTextDomain($this->domain, $this->path);
106
107
            // Setup default domain
108
            textdomain($this->domain);
109
        }
110
    }
111
112
    /**
113
     * Add text domain for gettext
114
     *
115
     * @param  string $domain of text for gettext setup
116
     * @param  string $path on filesystem
117
     * @return self
118
     * @throws ConfigurationException
119
     */
120 1
    public function addTextDomain($domain, $path)
121
    {
122
        // check path
123 1
        if (!is_dir($path)) {
124 1
            throw new ConfigurationException("Translator configuration path `$path` not found");
125
        }
126
127
        bindtextdomain($domain, $path);
128
129
        // @todo: hardcoded codeset
130
        bind_textdomain_codeset($domain, 'UTF-8');
131
132
        return $this;
133
    }
134
135
    /**
136
     * Translate message
137
     *
138
     * Simple example of usage
139
     * equal to gettext('Message')
140
     *     Translator::translate('Message');
141
     *
142
     * Simple replace of one or more argument(s)
143
     * equal to sprintf(gettext('Message to %s'), 'Username')
144
     *     Translator::translate('Message to %s', 'Username');
145
     *
146
     * @param  string $message
147
     * @param  string ...$text
148
     * @return string
149
     */
150 90
    public static function translate($message, ...$text)
151
    {
152 90
        if (empty($message)) {
153 1
            return $message;
154
        }
155
156 90
        if (function_exists('gettext')) {
157 90
            $message = gettext($message);
158 90
        }
159
160 90
        if (func_num_args() > 1) {
161 67
            $message = vsprintf($message, $text);
162 67
        }
163
164 90
        return $message;
165
    }
166
167
    /**
168
     * Translate plural form
169
     *
170
     * Example of usage plural form + sprintf
171
     * equal to sprintf(ngettext('%d comment', '%d comments', 4), 4)
172
     *     Translator::translatePlural('%d comment', '%d comments', 4, 4)
173
     *
174
     * Example of usage plural form + sprintf
175
     * equal to sprintf(ngettext('%d comment', '%d comments', 4), 4, 'Topic')
176
     *     Translator::translatePlural('%d comment to %s', '%d comments to %s', 4, 'Topic')
177
     *
178
     * @param  string  $singular
179
     * @param  string  $plural
180
     * @param  integer $number
181
     * @param  string  ...$text
182
     * @return string
183
     * @link   http://docs.translatehouse.org/projects/localization-guide/en/latest/l10n/pluralforms.html
184
     */
185 2
    public static function translatePlural($singular, $plural, $number, ...$text)
186
    {
187 2
        if (function_exists('ngettext')) {
188 2
            $message = ngettext($singular, $plural, $number);
189 2
        } else {
190
            $message = $singular;
191
        }
192
193 2
        if (func_num_args() > 3) {
194
            // first element is number
195 1
            array_unshift($text, $number);
196 1
            $message = vsprintf($message, $text);
197 1
        }
198
199 2
        return $message;
200
    }
201
}
202