-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathI18nCatalog.php
189 lines (165 loc) · 4.88 KB
/
I18nCatalog.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
<?php
namespace Koded\I18n;
use Koded\Stdlib\Configuration;
use Throwable;
use function array_key_exists;
use function error_log;
use function getcwd;
use function rtrim;
use function str_replace;
use function strtolower;
abstract class I18nCatalog
{
protected string $locale;
protected string $directory;
protected I18nFormatter $formatter;
private function __construct(
I18nFormatter $formatter,
string $directory,
string $locale)
{
$this->formatter = $formatter;
$this->directory = rtrim($directory, '/');
$this->locale = $this->initialize($locale);
}
public static function new(Configuration $conf): I18nCatalog
{
$catalog = $conf->get('translation.catalog', ArrayCatalog::class);
$formatter = $conf->get('translation.formatter', DefaultFormatter::class);
$instance = new $catalog(
new $formatter,
$directory = $conf->get('translation.dir', getcwd() . '/locale'),
$locale = self::normalizeLocale($conf->get('translation.locale', I18n::DEFAULT_LOCALE))
);
if ($instance->supports($locale)) {
return $instance;
}
if ($catalog !== ArrayCatalog::class) {
error_log(" > ($locale) gettext not supported, trying ArrayCatalog ...");
$conf->set('translation.catalog', ArrayCatalog::class);
return static::new($conf);
}
// Last resort, passthru
return new NoCatalog(new $formatter, $directory, $locale);
}
public static function normalizeLocale(string $locale): string
{
$locale = explode('_', str_replace('.', '_', $locale));
return "$locale[0]_$locale[1]";
}
public function translate(
string $domain,
string $key,
array $arguments = [],
int $n = 0): string
{
return $this->formatter->format(
$this->message($domain, $key, $n),
$arguments
);
}
public function locale(): string
{
return $this->locale;
}
public function directory(): string
{
return $this->directory;
}
public function formatter(): I18nFormatter
{
return $this->formatter;
}
public function urlized(): string
{
return str_replace('_', '-', strtolower($this->locale));
}
/**
* Returns the localized display name for catalog's language.
* Defaults to locale value if language value is not available.
*
* @return string
*/
abstract public function language(): string;
/**
* Translates the message.
*
* @param string $domain
* @param string $string
* @param int $n
* @return string
*/
abstract protected function message(
string $domain,
string $string,
int $n): string;
/**
* Checks if the locale is supported for this catalog,
* or other specific requirements.
*
* @param string $locale
* @return bool
*/
abstract protected function supports(string $locale): bool;
/**
* Initialize the catalog object. This method is
* called before supports().
*
* @param string $locale Desired locale to be initialized
* @return string|false Returns the set locale,
* or FALSE if initialization fails.
*/
abstract protected function initialize(string $locale): string|false;
}
class NoCatalog extends I18nCatalog
{
public function language(): string
{
return $this->locale ?: '';
}
protected function message(string $domain, string $string, int $n): string
{
return $string;
}
// @codeCoverageIgnoreStart
protected function supports(string $locale): bool
{
return true;
}
// @codeCoverageIgnoreEnd
protected function initialize(string $locale): string|false
{
return $locale;
}
}
class ArrayCatalog extends I18nCatalog
{
private array $data = [];
public function language(): string
{
return $this->data['language'] ?? $this->locale;
}
protected function message(string $domain, string $string, int $n): string
{
return $this->data[$domain][$string] ?? $string;
}
protected function supports(string $locale): bool
{
return $this->locale === $locale;
}
protected function initialize(string $locale): string|false
{
try {
/** @noinspection PhpIncludeInspection */
$this->data = require($catalog = "$this->directory/$locale.php");
if (false === array_key_exists('messages', $this->data)) {
error_log("ERROR : i18n catalog $catalog is missing the messages array");
return false;
}
return $locale;
} catch (Throwable $e) {
error_log($e->getMessage());
return false;
}
}
}