-
-
Notifications
You must be signed in to change notification settings - Fork 166
/
I18n.php
230 lines (196 loc) · 5.35 KB
/
I18n.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
<?php
namespace Kirby\Toolkit;
use Closure;
use Exception;
/**
* Localization class, roughly inspired by VueI18n
*
* @package Kirby Toolkit
* @author Bastian Allgeier <bastian@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier GmbH
* @license https://opensource.org/licenses/MIT
*/
class I18n
{
/**
* Custom loader function
*
* @var Closure
*/
public static $load = null;
/**
* Current locale
*
* @var string
*/
public static $locale = 'en';
/**
* All registered translations
*
* @var array
*/
public static $translations = [];
/**
* The fallback locale
*
* @var string
*/
public static $fallback = 'en';
/**
* Returns the fallback code
*
* @return string
*/
public static function fallback(): string
{
if (is_string(static::$fallback) === true) {
return static::$fallback;
}
if (is_callable(static::$fallback) === true) {
return static::$fallback = (static::$fallback)();
}
return static::$fallback = 'en';
}
/**
* Returns singular or plural
* depending on the given number
*
* @param int $count
* @param boolean $none If true, 'none' will be returned if the count is 0
* @return string
*/
public static function form(int $count, bool $none = false): string
{
if ($none === true && $count === 0) {
return 'none';
}
return $count === 1 ? 'singular' : 'plural';
}
/**
* Returns the locale code
*
* @return string
*/
public static function locale(): string
{
if (is_string(static::$locale) === true) {
return static::$locale;
}
if (is_callable(static::$locale) === true) {
return static::$locale = (static::$locale)();
}
return static::$locale = 'en';
}
/**
* Translates a given message
* according to the currently set locale
*
* @param string|array $key
* @param string|array|null $fallback
* @param string|null $locale
* @return string|array|null
*/
public static function translate($key, $fallback = null, string $locale = null)
{
$locale = $locale ?? static::locale();
if (is_array($key) === true) {
if (isset($key[$locale])) {
return $key[$locale];
}
if (is_array($fallback)) {
return $fallback[$locale] ?? $fallback['en'] ?? reset($fallback);
}
return $fallback;
}
if ($translation = static::translation($locale)[$key] ?? null) {
return $translation;
}
if ($fallback !== null) {
return $fallback;
}
if ($locale !== static::fallback()) {
return static::translation(static::fallback())[$key] ?? null;
}
return null;
}
/**
* Translate by key and then replace
* placeholders in the text
*
* @param string $key
* @param string $fallback
* @param array $replace
* @param string $locale
* @return string
*/
public static function template(string $key, $fallback = null, array $replace = null, string $locale = null)
{
if (is_array($fallback) === true) {
$replace = $fallback;
$fallback = null;
$locale = null;
}
$template = static::translate($key, $fallback, $locale);
return Str::template($template, $replace, '-', '{', '}');
}
/**
* Returns the current or any other translation
* by locale. If the translation does not exist
* yet, the loader will try to load it, if defined.
*
* @param string|null $locale
* @return array
*/
public static function translation(string $locale = null): array
{
$locale = $locale ?? static::locale();
if (isset(static::$translations[$locale]) === true) {
return static::$translations[$locale];
}
if (is_a(static::$load, 'Closure') === true) {
return static::$translations[$locale] = (static::$load)($locale);
}
return static::$translations[$locale] = [];
}
/**
* Returns all loaded or defined translations
*
* @return array
*/
public static function translations(): array
{
return static::$translations;
}
/**
* Translate amounts
*
* @param string $key
* @param integer $count
* @return mixed
*/
public static function translateCount(string $key, int $count, string $locale = null)
{
$translation = static::translate($key, null, $locale);
if ($translation === null) {
return null;
}
if (is_string($translation) === true) {
return $translation;
}
if (count($translation) !== 3) {
throw new Exception('Please provide 3 translations');
}
switch ($count) {
case 0:
$message = $translation[0];
break;
case 1:
$message = $translation[1];
break;
default:
$message = $translation[2];
}
return str_replace('{{ count }}', $count, $message);
}
}