forked from phptal/PHPTAL
/
GetTextTranslator.php
187 lines (169 loc) · 4.78 KB
/
GetTextTranslator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
declare(strict_types=1);
/**
* PHPTAL templating engine
*
* @category HTML
* @package PHPTAL
* @author Laurent Bedubourg <lbedubourg@motion-twin.com>
* @author Kornel Lesiński <kornel@aardvarkmedia.co.uk>
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
* @link http://phptal.org/
*/
namespace PhpTal;
/**
* \PhpTal\TranslationService gettext implementation.
*
* Because gettext is the most common translation library in use, this
* implementation is shipped with the PHPTAL library.
*
* Please refer to the PHPTAL documentation for usage examples.
*
* @package PHPTAL
* @author Laurent Bedubourg <lbedubourg@motion-twin.com>
*/
class GetTextTranslator implements TranslationServiceInterface
{
/**
* @var array
*/
private $vars = [];
/**
* @var string
*/
private $currentDomain;
/**
* @var string
*/
private $encoding = 'UTF-8';
/**
* GetTextTranslator constructor.
* @throws Exception\ConfigurationException
*/
public function __construct()
{
if (!function_exists('gettext')) {
throw new Exception\ConfigurationException('Gettext not installed');
}
}
/**
* set encoding that is used by template and is expected from gettext
* the default is UTF-8
*
* @param string $enc encoding name
*/
public function setEncoding(string $enc): void
{
$this->encoding = $enc;
}
/**
* It expects locale names as arguments.
* Choses first one that works.
*
* setLanguage("en_US.utf8","en_US","en_GB","en")
*
* @param array $langs
* @return string - chosen language
* @throws Exception\ConfigurationException
*/
public function setLanguage(...$langs): string
{
$langCode = $this->trySettingLanguages(LC_ALL, $langs);
if ($langCode) {
return $langCode;
}
if (defined('LC_MESSAGES')) {
$langCode = $this->trySettingLanguages(LC_MESSAGES, $langs);
if ($langCode) {
return $langCode;
}
}
throw new Exception\ConfigurationException(
'Language(s) code(s) "' . implode(', ', $langs) . '" not supported by your system'
);
}
/**
* @param int $category
* @param array $langs
*
* @return string
*/
private function trySettingLanguages(int $category, array $langs): ?string
{
foreach ($langs as $langCode) {
putenv("LANG=$langCode");
putenv("LC_ALL=$langCode");
putenv("LANGUAGE=$langCode");
if (setlocale($category, $langCode)) {
return $langCode;
}
}
return null;
}
/**
* Adds translation domain (usually it's the same as name of .po file [without extension])
*
* Encoding must be set before calling addDomain!
*
* @param string $domain
* @param string $path
*/
public function addDomain(string $domain, ?string $path = null): void
{
bindtextdomain($domain, $path ?? './locale/');
if ($this->encoding) {
bind_textdomain_codeset($domain, $this->encoding);
}
$this->useDomain($domain);
}
/**
* Switches to one of the domains previously set via addDomain()
*
* @param string $domain name of translation domain to be used.
*
* @return string - old domain
*/
public function useDomain(string $domain): ?string
{
$old = $this->currentDomain;
$this->currentDomain = $domain;
textdomain($domain);
return $old;
}
/**
* used by generated PHP code. Don't use directly.
*
* @param string $key
* @param mixed $value
*/
public function setVar(string $key, $value): void
{
$this->vars[$key] = $value;
}
/**
* translate given key.
*
* @param string $key
* @param bool $htmlencode if true, output will be HTML-escaped.
*
* @return string
* @throws Exception\VariableNotFoundException
*/
public function translate(string $key, bool $htmlencode): string
{
$value = gettext($key);
if ($htmlencode) {
$value = htmlspecialchars($value, ENT_QUOTES, $this->encoding);
}
while (preg_match('/\${(.*?)\}/sm', $value, $m)) {
list($src, $var) = $m;
if (!array_key_exists($var, $this->vars)) {
throw new Exception\VariableNotFoundException(
'Interpolation error. Translation uses ${' . $var . '}, which is not defined in the template (via i18n:name)'
);
}
$value = str_replace($src, $this->vars[$var], $value);
}
return $value;
}
}