forked from Mika-/i18next-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
i18next.php
257 lines (192 loc) · 8.18 KB
/
i18next.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<?php
/*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <github.com/Mika-> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return
* ----------------------------------------------------------------------------
*/
class i18next {
private static $_path = null;
private static $_language = null;
private static $_translation = array();
/**
* Fall back language for translations not found in current language
* @var string fall back language
*/
private static $_fallBackLanguage = 'dev';
/**
* Plural rules based on i18next.plurals.js definition
*
* @todo: parse https://github.com/jamuhl/i18next/blob/master/src/i18next.plurals.js to get more rules :/
* Watch for function result transformations based on numbers array!
* @var array
*/
private static $_pluralRules = array(
'sl' => 'return $n % 100 === 1 ? 1 : ($n % 100 === 2 ? 2 : ( $n%100 === 3 || $n%100===4 ? 3 : 5));'
);
/**
* Will hold plural functions for languages
* @var array
*/
private static $_pluralFunctions = array ();
/**
* Inits i18next static class...
*
* path may include __lng___ and __ns__ placeholders so all languages and namespaces are loaded!
*
* @param string $language locale language code
* @param string $path path to locale json files
*/
public static function init($language = 'en', $path = null) {
self::$_language = $language;
self::$_path = $path;
self::loadTranslation();
self::populatePluralFunctions();
}
/**
* Populates plural functions from plural rules
*/
private static function populatePluralFunctions() {
foreach (self::$_pluralRules as $lng => $rule) {
self::$_pluralFunctions[$lng] = create_function('$n', $rule);
}
}
/**
* Change default language and fall back language
* If fallback is not set it is left unchanged
*
* @param string $language New default language
* @param string $fallback Fallback language
*/
public static function setLanguage($language,$fallback=null) {
self::$_language = $language;
if (!empty($fallback)) {
self::$_fallBackLanguage = $fallback;
}
}
public static function existTranslation($key) {
$return = self::_getKey($key);
if ($return)
$return = true;
return $return;
}
public static function getTranslation($key, $variables = array()) {
$return = self::_getKey($key, $variables);
if ($return && isset($variables['postProcess']) && $variables['postProcess'] === 'sprintf' && isset($variables['sprintf'])) {
if (is_array($variables['sprintf']))
$return = vsprintf($return, $variables['sprintf']);
else
$return = sprintf($return, $variables['sprintf']);
}
if (!$return)
$return = $key;
foreach ($variables as $variable => $value) {
if (is_string($value) || is_numeric($value))
$return = preg_replace('/__' . $variable . '__/', $value, $return);
}
return $return;
}
/**
* Loads translation(s)
* @throws Exception
*/
private static function loadTranslation() {
$path = preg_replace('/__(.+?)__/', '*', self::$_path, 2, $hasNs);
if (!preg_match('/\.json$/', $path)) {
$path = $path . 'translation.json';
// Fix for ns & lng parser to work since it references self::$_path
self::$_path = self::$_path . 'translation.json';
}
$dir = glob($path);
if (count($dir) === 0)
throw new Exception('Translation file not found');
foreach ($dir as $file) {
$translation = file_get_contents($file);
$translation = json_decode($translation, true);
if (!$translation)
throw new Exception('Invalid json');
if ($hasNs) {
$regexp = preg_replace('/__(.+?)__/', '(?<$1>.+)?', preg_quote(self::$_path, '/'));
preg_match('/^' . $regexp . '$/', $file, $ns);
if (!array_key_exists('lng', $ns))
$ns['lng'] = self::$_language;
if (array_key_exists('ns', $ns)) {
if (array_key_exists($ns['lng'], self::$_translation) && array_key_exists($ns['ns'], self::$_translation[$ns['lng']]))
self::$_translation[$ns['lng']][$ns['ns']] = array_merge(self::$_translation[$ns['lng']][$ns['ns']], array($ns['ns'] => $translation));
else if (array_key_exists($ns['lng'], self::$_translation))
self::$_translation[$ns['lng']] = array_merge(self::$_translation[$ns['lng']], array($ns['ns'] => $translation));
else
self::$_translation[$ns['lng']] = array($ns['ns'] => $translation);
}
else {
if (array_key_exists($ns['lng'], self::$_translation))
self::$_translation[$ns['lng']] = array_merge(self::$_translation[$ns['lng']], $translation);
else
self::$_translation[$ns['lng']] = $translation;
}
}
else {
if (array_key_exists(self::$_language, $translation))
self::$_translation = $translation;
else
self::$_translation = array_merge(self::$_translation, $translation);
}
}
}
/**
* Get translation for given key
*
* Translation is looked up in language specified in $variables['lng'],current language or fall back language - in this order.
* Fall back language is used only if defined and no explicit language was specified in $variables
*
* @param string $key key for translation
* @param array $variables variables
* @return mixed translated string or false if no matching translation has been found
*/
private static function _getKey($key, $variables = array()) {
$return = false;
if (array_key_exists('lng', $variables) && array_key_exists($variables['lng'], self::$_translation))
{
$translation = self::$_translation[$variables['lng']];
// we need to store language used for multiple plural forms
$language = $variables['lng'];
}
else if (array_key_exists(self::$_language, self::$_translation))
{
$translation = self::$_translation[self::$_language];
$language = self::$_language;
}
else
{
$translation = array();
// Khm... skip lookup into empty array!
return $return;
}
foreach (explode('.', $key) as $path) {
if (array_key_exists($path, $translation) && is_array($translation[$path])) {
$translation = $translation[$path];
}
else if (array_key_exists($path, $translation)) {
// transform count if exists and there is rule function for multiple plural forms
$count = (array_key_exists('count', $variables) && array_key_exists($language, self::$_pluralFunctions)) ?
call_user_func(self::$_pluralFunctions[$language],$variables['count']) : (empty($variables['count'])? null : $variables['count'] );
if (array_key_exists('count', $variables) && $variables['count'] != 1 && array_key_exists($path . '_plural_' . $count, $translation))
$return = $translation[$path . '_plural_' . $count];
else if (array_key_exists('count', $variables) && $variables['count'] != 1 && array_key_exists($path . '_plural', $translation))
$return = $translation[$path . '_plural'];
else
$return = $translation[$path];
break;
}
}
if (is_array($translation) && isset($variables['returnObjectTrees']) && $variables['returnObjectTrees'] === true)
$return = $translation;
// Fallback language check...
if ($return === false and empty($variables['lng']) and !empty(self::$_fallBackLanguage)) {
$return = self::_getKey($key, array_merge($variables,array('lng'=> self::$_fallBackLanguage)));
}
return $return;
}
}