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
|
<?php
namespace Gettext;
class GettextTranslator extends BaseTranslator implements TranslatorInterface { /** * Constructor. Detects the current language using the environment variables. * * @param string $language */ public function __construct($language = null) { if (!function_exists('gettext')) { throw new \RuntimeException('This class require the gettext extension for PHP'); }
//detects the language environment respecting the priority order //http://php.net/manual/en/function.gettext.php#114062 if (empty($language)) { $language = getenv('LANGUAGE') ?: getenv('LC_ALL') ?: getenv('LC_MESSAGES') ?: getenv('LANG'); }
if (!empty($language)) { $this->setLanguage($language); } }
/** * Define the current locale. * * @param string $language * @param int|null $category * * @return self */ public function setLanguage($language, $category = null) { if ($category === null) { $category = defined('LC_MESSAGES') ? LC_MESSAGES : LC_ALL; }
setlocale($category, $language); putenv('LANGUAGE='.$language);
return $this; }
/** * Loads a gettext domain. * * @param string $domain * @param string $path * @param bool $default * * @return self */ public function loadDomain($domain, $path = null, $default = true) { bindtextdomain($domain, $path); bind_textdomain_codeset($domain, 'UTF-8');
if ($default) { textdomain($domain); }
return $this; }
/** * @see TranslatorInterface * * {@inheritdoc} */ public function gettext($original) { return gettext($original); }
/** * @see TranslatorInterface * * {@inheritdoc} */ public function ngettext($original, $plural, $value) { return ngettext($original, $plural, $value); }
/** * @see TranslatorInterface * * {@inheritdoc} */ public function dngettext($domain, $original, $plural, $value) { return dngettext($domain, $original, $plural, $value); }
/** * @see TranslatorInterface * * {@inheritdoc} */ public function npgettext($context, $original, $plural, $value) { $message = $context."\x04".$original; $translation = ngettext($message, $plural, $value);
return ($translation === $message) ? $original : $translation; }
/** * @see TranslatorInterface * * {@inheritdoc} */ public function pgettext($context, $original) { $message = $context."\x04".$original; $translation = gettext($message);
return ($translation === $message) ? $original : $translation; }
/** * @see TranslatorInterface * * {@inheritdoc} */ public function dgettext($domain, $original) { return dgettext($domain, $original); }
/** * @see TranslatorInterface * * {@inheritdoc} */ public function dpgettext($domain, $context, $original) { $message = $context."\x04".$original; $translation = dgettext($domain, $message);
return ($translation === $message) ? $original : $translation; }
/** * @see TranslatorInterface * * {@inheritdoc} */ public function dnpgettext($domain, $context, $original, $plural, $value) { $message = $context."\x04".$original; $translation = dngettext($domain, $message, $plural, $value);
return ($translation === $message) ? $original : $translation; } }
|