|
1
|
|
|
<?php |
|
2
|
|
|
// @codeCoverageIgnoreStart |
|
3
|
|
|
defined('BASEPATH') || exit('No direct script access allowed'); |
|
4
|
|
|
// @codeCoverageIgnoreEnd |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Codeigniter PHP framework library class for dealing with gettext. |
|
8
|
|
|
* |
|
9
|
|
|
* @package CodeIgniter |
|
10
|
|
|
* @subpackage Libraries |
|
11
|
|
|
* @category Language |
|
12
|
|
|
* @author Joël Gaujard <[email protected]> |
|
13
|
|
|
* @author Marko Martinović <[email protected]> |
|
14
|
|
|
* @link https://github.com/joel-depiltech/codeigniter-gettext |
|
15
|
|
|
*/ |
|
16
|
|
|
class Gettext |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var string domain name match with file contains translation */ |
|
19
|
|
|
private $_textDomain; |
|
20
|
|
|
|
|
21
|
|
|
/** @var string character encoding in which the messages are */ |
|
22
|
|
|
private $_catalogCodeSet; |
|
23
|
|
|
|
|
24
|
|
|
/** @var string the path for a domain */ |
|
25
|
|
|
private $_localeDir; |
|
26
|
|
|
|
|
27
|
|
|
/** @var string|array locale or array of locales */ |
|
28
|
|
|
private $_locale; |
|
29
|
|
|
|
|
30
|
|
|
/** @var string constant specifying the category of the functions affected by the locale setting */ |
|
31
|
|
|
private $_category; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Initialize Codeigniter PHP framework and get configuration |
|
35
|
|
|
* |
|
36
|
|
|
* @param array $config Override default configuration |
|
37
|
|
|
*/ |
|
38
|
108 |
|
public function __construct(array $config = array()) |
|
39
|
|
|
{ |
|
40
|
108 |
|
log_message('info', 'Gettext Library Class Initialized'); |
|
41
|
|
|
|
|
42
|
108 |
|
$this->_setConfig($config); |
|
43
|
|
|
|
|
44
|
81 |
|
$this |
|
45
|
108 |
|
->_bindTextDomainCodeSet() |
|
46
|
108 |
|
->_bindTextDomain() |
|
47
|
108 |
|
->_textDomain() |
|
48
|
108 |
|
->_setLocale() |
|
49
|
108 |
|
->_putEnv() |
|
50
|
108 |
|
->_checkLocaleFile(); |
|
51
|
108 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Merge config as parameter and default config (config/gettext.php file) |
|
55
|
|
|
* |
|
56
|
|
|
* @param array $config |
|
57
|
|
|
*/ |
|
58
|
108 |
|
private function _setConfig(array $config = array()) |
|
59
|
|
|
{ |
|
60
|
108 |
|
$this->_localeDir = isset($config['gettext_locale_dir']) |
|
61
|
108 |
|
? $config['gettext_locale_dir'] : config_item('gettext_locale_dir'); |
|
62
|
|
|
|
|
63
|
108 |
|
$this->_textDomain = isset($config['gettext_text_domain']) |
|
64
|
108 |
|
? $config['gettext_text_domain'] : config_item('gettext_text_domain'); |
|
65
|
|
|
|
|
66
|
108 |
|
$this->_catalogCodeSet = isset($config['gettext_catalog_codeset']) |
|
67
|
108 |
|
? $config['gettext_catalog_codeset'] : config_item('gettext_catalog_codeset'); |
|
68
|
|
|
|
|
69
|
108 |
|
$this->_locale = isset($config['gettext_locale']) |
|
70
|
108 |
|
? $config['gettext_locale'] : config_item('gettext_locale'); |
|
71
|
|
|
|
|
72
|
108 |
|
$this->_category = (isset($config['gettext_category']) |
|
73
|
108 |
|
? $config['gettext_category'] : config_item('gettext_category')); |
|
74
|
108 |
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Load a domain |
|
78
|
|
|
* |
|
79
|
|
|
* @param string $domain |
|
80
|
|
|
*/ |
|
81
|
40 |
|
public function changeDomain($domain) |
|
82
|
|
|
{ |
|
83
|
40 |
|
log_message('info', 'Gettext Library Class -> Change domain'); |
|
84
|
|
|
|
|
85
|
40 |
|
$this->_textDomain = $domain; |
|
86
|
|
|
|
|
87
|
30 |
|
$this |
|
88
|
40 |
|
->_bindTextDomainCodeSet() |
|
89
|
40 |
|
->_bindTextDomain() |
|
90
|
40 |
|
->_textDomain() |
|
91
|
40 |
|
->_checkLocaleFile(); |
|
92
|
40 |
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Switch to a category |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $category |
|
98
|
|
|
*/ |
|
99
|
4 |
|
public function changeCategory($category) |
|
100
|
|
|
{ |
|
101
|
4 |
|
log_message('info', 'Gettext Library Class -> Change category'); |
|
102
|
|
|
|
|
103
|
4 |
|
$this->_category = $category; |
|
104
|
|
|
|
|
105
|
4 |
|
$this->_setLocale(); |
|
106
|
4 |
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Gettext catalog codeset |
|
110
|
|
|
* |
|
111
|
|
|
* @return $this |
|
112
|
|
|
*/ |
|
113
|
108 |
|
private function _bindTextDomainCodeSet() |
|
114
|
|
|
{ |
|
115
|
108 |
|
$isBindTextDomainCodeSet = bind_textdomain_codeset($this->_textDomain, $this->_catalogCodeSet); |
|
116
|
|
|
|
|
117
|
108 |
|
log_message( |
|
118
|
108 |
|
(is_string($isBindTextDomainCodeSet) ? 'info' : 'error'), |
|
119
|
|
|
'Gettext Library -> Bind ' . |
|
120
|
108 |
|
'text domain: ' . $this->_textDomain . ' - ' . |
|
121
|
108 |
|
'with code set: ' . $this->_catalogCodeSet |
|
122
|
81 |
|
); |
|
123
|
|
|
|
|
124
|
108 |
|
return $this; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Path to gettext locales directory relative to APPPATH |
|
129
|
|
|
* |
|
130
|
|
|
* @return $this |
|
131
|
|
|
*/ |
|
132
|
108 |
View Code Duplication |
private function _bindTextDomain() |
|
133
|
|
|
{ |
|
134
|
108 |
|
$isBindTextDomain = bindtextdomain($this->_textDomain, APPPATH . $this->_localeDir); |
|
135
|
|
|
|
|
136
|
108 |
|
log_message( |
|
137
|
108 |
|
(is_string($isBindTextDomain) ? 'info' : 'error'), |
|
138
|
|
|
'Gettext Library -> Bind ' . |
|
139
|
108 |
|
'text domain: ' . $this->_textDomain . ' - ' . |
|
140
|
108 |
|
'with directory: ' . APPPATH . $this->_localeDir |
|
141
|
81 |
|
); |
|
142
|
|
|
|
|
143
|
108 |
|
return $this; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Gettext domain |
|
148
|
|
|
* |
|
149
|
|
|
* @return $this |
|
150
|
|
|
*/ |
|
151
|
108 |
|
private function _textDomain() |
|
152
|
|
|
{ |
|
153
|
108 |
|
$isSetTextDomain = textdomain($this->_textDomain); |
|
154
|
|
|
|
|
155
|
108 |
|
log_message( |
|
156
|
108 |
|
(is_string($isSetTextDomain) ? 'info' : 'error'), |
|
157
|
108 |
|
'Gettext Library -> Set text domain: ' . $this->_textDomain |
|
158
|
81 |
|
); |
|
159
|
|
|
|
|
160
|
108 |
|
return $this; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Gettext locale |
|
165
|
|
|
* |
|
166
|
|
|
* @return $this |
|
167
|
|
|
*/ |
|
168
|
108 |
|
private function _setLocale() |
|
169
|
|
|
{ |
|
170
|
108 |
|
$isSetLocale = setlocale(constant($this->_category), $this->_locale); |
|
171
|
|
|
|
|
172
|
108 |
|
log_message( |
|
173
|
108 |
|
(is_string($isSetLocale) ? 'info' : 'error'), |
|
174
|
|
|
'Gettext Library -> ' . |
|
175
|
108 |
|
'Set locale: ' . (is_array($this->_locale) ? print_r($this->_locale, TRUE) : $this->_locale) . ' ' . |
|
176
|
108 |
|
'for category: ' . $this->_category |
|
177
|
81 |
|
); |
|
178
|
|
|
|
|
179
|
|
|
// the new current locale, or FALSE if the locale is not implemented on your platform |
|
180
|
108 |
|
$this->_locale = $isSetLocale; |
|
181
|
|
|
|
|
182
|
108 |
|
return $this; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Change environment language for CLI |
|
187
|
|
|
* |
|
188
|
|
|
* @return $this |
|
189
|
|
|
*/ |
|
190
|
108 |
|
private function _putEnv() |
|
191
|
|
|
{ |
|
192
|
108 |
|
$isPutEnv = putenv('LANGUAGE=' . $this->_locale); |
|
193
|
108 |
|
log_message( |
|
194
|
108 |
|
($isPutEnv ? 'info' : 'error'), |
|
195
|
108 |
|
'Gettext Library -> Set environment LANGUAGE: ' . $this->_locale |
|
196
|
81 |
|
); |
|
197
|
|
|
|
|
198
|
108 |
|
$isPutEnv = putenv('LANG=' . $this->_locale); |
|
199
|
108 |
|
log_message( |
|
200
|
108 |
|
($isPutEnv ? 'info' : 'error'), |
|
201
|
108 |
|
'Gettext Library -> Set environment LANG: ' . $this->_locale |
|
202
|
81 |
|
); |
|
203
|
|
|
|
|
204
|
108 |
|
$isPutEnv = putenv($this->_category . '=' . $this->_locale); |
|
205
|
108 |
|
log_message( |
|
206
|
108 |
|
($isPutEnv ? 'info' : 'error'), |
|
207
|
108 |
|
'Gettext Library -> Set environment category ' . $this->_category . ': ' . $this->_locale |
|
208
|
81 |
|
); |
|
209
|
|
|
|
|
210
|
108 |
|
return $this; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* MO file exists for locale |
|
215
|
|
|
* |
|
216
|
|
|
* @return $this |
|
217
|
|
|
*/ |
|
218
|
108 |
View Code Duplication |
private function _checkLocaleFile() |
|
219
|
|
|
{ |
|
220
|
108 |
|
$file = APPPATH . $this->_localeDir . |
|
221
|
108 |
|
'/' . $this->_locale . '/LC_MESSAGES/' . $this->_textDomain . '.mo'; |
|
222
|
|
|
|
|
223
|
108 |
|
log_message( |
|
224
|
108 |
|
(is_file($file) ? 'info' : 'error'), |
|
225
|
27 |
|
'Gettext Library -> Check MO file exists: ' . $file |
|
226
|
81 |
|
); |
|
227
|
|
|
|
|
228
|
108 |
|
return $this; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
|
|
234
|
|
|
/* End of file Gettext.php */ |
|
235
|
|
|
/* Location: ./application/libraries/Gettext.php */ |