-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGettextCatalog.php
61 lines (53 loc) · 1.67 KB
/
GettextCatalog.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
<?php
namespace Koded\I18n;
use function bind_textdomain_codeset;
use function bindtextdomain;
use function dgettext;
use function dngettext;
use function extension_loaded;
use function locale_get_display_language;
use function setlocale;
use function textdomain;
final class GettextCatalog extends I18nCatalog
{
private array $domains = [];
public function language(): string
{
return locale_get_display_language($this->locale, $this->locale);
}
protected function message(string $domain, string $string, int $n): string
{
$domain = $this->bindDomain($domain);
return $n > 0
? dngettext($domain, $string, $string, $n) // TODO plural string
: dgettext($domain, $string);
}
protected function supports(string $locale): bool
{
return $locale === $this->locale;
}
protected function initialize(string $locale): string|false
{
// @codeCoverageIgnoreStart
if (false === extension_loaded('gettext')) {
return false;
}
if (false === extension_loaded('intl')) {
return false;
}
// @codeCoverageIgnoreEnd
$this->bindDomain('messages');
return setlocale(LC_MESSAGES, [$locale, "$locale.UTF-8", "$locale.utf8"])
? $locale
: false;
}
private function bindDomain(string $domain): string
{
if (false === isset($this->domains[$domain])) {
$this->directory = bindtextdomain($domain, $this->directory);
$this->domains[$domain = textdomain($domain)] = true;
bind_textdomain_codeset($domain, 'UTF-8');
}
return $domain;
}
}