1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gettext; |
4
|
|
|
|
5
|
|
|
class GettextTranslator extends BaseTranslator implements TranslatorInterface |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Constructor. Detects the current language using the environment variables. |
9
|
|
|
* |
10
|
|
|
* @param string $language |
11
|
|
|
*/ |
12
|
|
|
public function __construct($language = null) |
13
|
|
|
{ |
14
|
|
|
if (!function_exists('gettext')) { |
15
|
|
|
throw new \RuntimeException('This class require the gettext extension for PHP'); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
//detects the language environment respecting the priority order |
19
|
|
|
//http://php.net/manual/en/function.gettext.php#114062 |
20
|
|
|
if (empty($language)) { |
21
|
|
|
$language = getenv('LANGUAGE') ?: getenv('LC_ALL') ?: getenv('LC_MESSAGES') ?: getenv('LANG'); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
if (!empty($language)) { |
25
|
|
|
$this->setLanguage($language); |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Define the current locale. |
31
|
|
|
* |
32
|
|
|
* @param string $language |
33
|
|
|
* @param int|null $category |
34
|
|
|
* |
35
|
|
|
* @return self |
36
|
|
|
*/ |
37
|
|
|
public function setLanguage($language, $category = null) |
38
|
|
|
{ |
39
|
|
|
if ($category === null) { |
40
|
|
|
$category = defined('LC_MESSAGES') ? LC_MESSAGES : LC_ALL; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
setlocale($category, $language); |
44
|
|
|
putenv('LANGUAGE='.$language); |
45
|
|
|
|
46
|
|
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Loads a gettext domain. |
51
|
|
|
* |
52
|
|
|
* @param string $domain |
53
|
|
|
* @param string $path |
54
|
|
|
* @param bool $default |
55
|
|
|
* |
56
|
|
|
* @return self |
57
|
|
|
*/ |
58
|
|
|
public function loadDomain($domain, $path = null, $default = true) |
59
|
|
|
{ |
60
|
|
|
bindtextdomain($domain, $path); |
61
|
|
|
bind_textdomain_codeset($domain, 'UTF-8'); |
62
|
|
|
|
63
|
|
|
if ($default) { |
64
|
|
|
textdomain($domain); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @see TranslatorInterface |
72
|
|
|
* |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
|
|
public function gettext($original) |
76
|
|
|
{ |
77
|
|
|
return gettext($original); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @see TranslatorInterface |
82
|
|
|
* |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
|
|
public function ngettext($original, $plural, $value) |
86
|
|
|
{ |
87
|
|
|
return ngettext($original, $plural, $value); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @see TranslatorInterface |
92
|
|
|
* |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
95
|
|
|
public function dngettext($domain, $original, $plural, $value) |
96
|
|
|
{ |
97
|
|
|
return dngettext($domain, $original, $plural, $value); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @see TranslatorInterface |
102
|
|
|
* |
103
|
|
|
* {@inheritdoc} |
104
|
|
|
*/ |
105
|
|
View Code Duplication |
public function npgettext($context, $original, $plural, $value) |
|
|
|
|
106
|
|
|
{ |
107
|
|
|
$message = $context."\x04".$original; |
108
|
|
|
$translation = ngettext($message, $plural, $value); |
109
|
|
|
|
110
|
|
|
return ($translation === $message) ? $original : $translation; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @see TranslatorInterface |
115
|
|
|
* |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
*/ |
118
|
|
|
public function pgettext($context, $original) |
119
|
|
|
{ |
120
|
|
|
$message = $context."\x04".$original; |
121
|
|
|
$translation = gettext($message); |
122
|
|
|
|
123
|
|
|
return ($translation === $message) ? $original : $translation; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @see TranslatorInterface |
128
|
|
|
* |
129
|
|
|
* {@inheritdoc} |
130
|
|
|
*/ |
131
|
|
|
public function dgettext($domain, $original) |
132
|
|
|
{ |
133
|
|
|
return dgettext($domain, $original); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @see TranslatorInterface |
138
|
|
|
* |
139
|
|
|
* {@inheritdoc} |
140
|
|
|
*/ |
141
|
|
|
public function dpgettext($domain, $context, $original) |
142
|
|
|
{ |
143
|
|
|
$message = $context."\x04".$original; |
144
|
|
|
$translation = dgettext($domain, $message); |
145
|
|
|
|
146
|
|
|
return ($translation === $message) ? $original : $translation; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @see TranslatorInterface |
151
|
|
|
* |
152
|
|
|
* {@inheritdoc} |
153
|
|
|
*/ |
154
|
|
View Code Duplication |
public function dnpgettext($domain, $context, $original, $plural, $value) |
|
|
|
|
155
|
|
|
{ |
156
|
|
|
$message = $context."\x04".$original; |
157
|
|
|
$translation = dngettext($domain, $message, $plural, $value); |
158
|
|
|
|
159
|
|
|
return ($translation === $message) ? $original : $translation; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.