|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
if (!defined('BASEPATH')) { |
|
4
|
|
|
exit('No direct script access allowed'); |
|
5
|
|
|
} |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Code Igniter Gettext Extension library |
|
9
|
|
|
* |
|
10
|
|
|
* This Library overides the original CI's language class. Needs the $config['language'] variable set as it_IT or en_EN or fr_FR ... |
|
11
|
|
|
* |
|
12
|
|
|
* @package Gettext Extension |
|
13
|
|
|
* @author wokamoto |
|
14
|
|
|
* @copyright Copyright (c) 2012 |
|
15
|
|
|
* @license http://www.gnu.org/licenses/lgpl.txt |
|
16
|
|
|
* @link |
|
17
|
|
|
* @version Version 0.1 |
|
18
|
|
|
* @since 2012 January, 27th |
|
19
|
|
|
*/ |
|
20
|
|
|
// ------------------------------------------------------------------------ |
|
21
|
|
|
|
|
22
|
|
|
class MY_Lang extends CI_Lang |
|
23
|
|
|
{ |
|
24
|
|
|
|
|
25
|
|
|
private $ci; |
|
26
|
|
|
|
|
27
|
|
|
private $gettext_language; |
|
28
|
|
|
|
|
29
|
|
|
private $gettext_codeset; |
|
30
|
|
|
|
|
31
|
|
|
private $gettext_domain; |
|
32
|
|
|
|
|
33
|
|
|
private $gettext_path; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* The constructor initialize the library |
|
37
|
|
|
* |
|
38
|
|
|
* @return MY_Lang |
|
|
|
|
|
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct() { |
|
41
|
|
|
parent::__construct(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
private function _language() { |
|
45
|
|
|
static $language; |
|
46
|
|
|
|
|
47
|
|
|
if (!isset($this->ci)) { |
|
48
|
|
|
$this->ci = & get_instance(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
if (!isset($language)) { |
|
52
|
|
|
$language = $this->ci->config->item('language'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
if (!isset($language) || !in_array($language, $this->ci->config->item('selectable_languages'))) { |
|
56
|
|
|
$language = 'english'; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
if ($language != $this->ci->config->item('language')) { |
|
60
|
|
|
$this->ci->config->set_item('language', $language); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return empty($language) ? 'english' : $language; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Load a language file |
|
68
|
|
|
* |
|
69
|
|
|
* @access public |
|
70
|
|
|
* @param mixed the name of the language file to be loaded. Can be an array |
|
71
|
|
|
* @param string the language (english, etc.) |
|
72
|
|
|
* @return mixed |
|
73
|
|
|
*/ |
|
74
|
|
|
public function load($langfile = '', $idiom = '', $return = FALSE, $add_suffix = TRUE, $alt_path = '') { |
|
75
|
|
|
return parent::load($langfile, !empty($idiom) ? $idiom : $this->_language(), $return, $add_suffix, $alt_path); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* This method overides the original load method. Its duty is loading the domain files by config or by default internal settings. |
|
80
|
|
|
* |
|
81
|
|
|
* @access public |
|
82
|
|
|
* @param string $userlang the language, set as ja_JP or it_IT or en_EN or fr_FR ... |
|
83
|
|
|
* @param string $codeset the codeset, set as UTF-8 or EUC ... |
|
84
|
|
|
* @return bool |
|
85
|
|
|
*/ |
|
86
|
|
|
public function load_gettext($userlang = '', $codeset = '', $textdomain = 'lang', $path = '') { |
|
87
|
|
|
if (!isset($this->ci)) { |
|
88
|
|
|
$this->ci = & get_instance(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$this->gettext_language = $this->language_select(!empty($userlang) ? $userlang : $this->_language()); |
|
92
|
|
|
$this->gettext_codeset = !empty($codeset) ? $codeset : $this->ci->config->item('charset'); |
|
93
|
|
|
$this->gettext_domain = $textdomain; |
|
94
|
|
|
$this->gettext_path = !empty($path) ? $path : APPPATH . 'language/locale'; |
|
95
|
|
|
log_message('debug', 'Gettext Class language was set by parameter:' . $this->gettext_language . ',' . $this->gettext_codeset); |
|
96
|
|
|
|
|
97
|
|
|
/* put env and set locale */ |
|
98
|
|
|
putenv("LANG={$this->gettext_language}"); |
|
99
|
|
|
setlocale(LC_ALL, $this->gettext_language); |
|
100
|
|
|
|
|
101
|
|
|
/* bind text domain */ |
|
102
|
|
|
$textdomain_path = bindtextdomain($this->gettext_domain, $this->gettext_path); |
|
103
|
|
|
bind_textdomain_codeset($this->gettext_domain, $this->gettext_codeset); |
|
104
|
|
|
textdomain($this->gettext_domain); |
|
105
|
|
|
log_message('debug', 'Gettext Class path: ' . $textdomain_path); |
|
106
|
|
|
log_message('debug', 'Gettext Class the domain: ' . $this->gettext_domain); |
|
107
|
|
|
|
|
108
|
|
|
return true; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
private function language_select($userlang = '') { |
|
112
|
|
|
$userlang = !empty($userlang) ? $userlang : $this->_language(); |
|
113
|
|
|
|
|
114
|
|
|
switch ($userlang) { |
|
115
|
|
|
case 'japanese': |
|
116
|
|
|
$userlang = 'ja_JP'; |
|
117
|
|
|
break; |
|
118
|
|
|
case 'english': |
|
119
|
|
|
default: |
|
120
|
|
|
$userlang = 'en'; |
|
121
|
|
|
break; |
|
122
|
|
|
} |
|
123
|
|
|
return $userlang; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Fetch a single line of text from the language array |
|
128
|
|
|
* |
|
129
|
|
|
* @access public |
|
130
|
|
|
* @param string $line the language line |
|
131
|
|
|
* @return string |
|
132
|
|
|
*/ |
|
133
|
|
|
public function line($line = '', $params = FALSE) { |
|
134
|
|
|
|
|
135
|
|
|
if ($params !== FALSE || FALSE === ($value = parent::line($line))) { |
|
136
|
|
|
$value = $this->_trans($line, $params); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
return $value ? $value : $line; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Plural forms added by Tchinkatchuk |
|
144
|
|
|
* http://www.codeigniter.com/forums/viewthread/2168/ |
|
145
|
|
|
*/ |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* The translator method |
|
149
|
|
|
* |
|
150
|
|
|
* @access private |
|
151
|
|
|
* @param string $original the original string to translate |
|
152
|
|
|
* @param array $aParams the plural parameters |
|
|
|
|
|
|
153
|
|
|
* @return false|string the string translated |
|
154
|
|
|
*/ |
|
155
|
|
|
private function _trans($original, $aParams = false) { |
|
156
|
|
|
if (!isset($this->gettext_domain)) { |
|
157
|
|
|
return false; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
if ($aParams && isset($aParams['plural']) && isset($aParams['count'])) { |
|
161
|
|
|
$sTranslate = ngettext($original, $aParams['plural'], $aParams['count']); |
|
162
|
|
|
$sTranslate = $this->replaceDynamically($sTranslate, $aParams); |
|
163
|
|
|
} else { |
|
164
|
|
|
$sTranslate = gettext($original); |
|
165
|
|
|
if (is_array($aParams) && count($aParams)) { |
|
166
|
|
|
$sTranslate = $this->replaceDynamically($sTranslate, $aParams); |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
return $sTranslate; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Allow dynamic allocation in traduction |
|
175
|
|
|
* |
|
176
|
|
|
* @final |
|
177
|
|
|
* @access private |
|
178
|
|
|
* @param string $sString |
|
179
|
|
|
* @return string |
|
180
|
|
|
*/ |
|
181
|
|
|
private function replaceDynamically($sString) { |
|
182
|
|
|
$aTrad = []; |
|
183
|
|
|
for ($i = 1, $iMax = func_num_args(); $i < $iMax; $i++) { |
|
184
|
|
|
$arg = func_get_arg($i); |
|
185
|
|
|
if (is_array($arg)) { |
|
186
|
|
|
foreach ($arg as $key => $sValue) { |
|
187
|
|
|
$aTrad['%' . $key] = $sValue; |
|
188
|
|
|
} |
|
189
|
|
|
} else { |
|
190
|
|
|
$aTrad['%' . $key] = $arg; |
|
|
|
|
|
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
return strtr($sString, $aTrad); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
} |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.