-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
GettextTranslator.php
116 lines (92 loc) · 3.2 KB
/
GettextTranslator.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
<?php
declare(strict_types = 1);
namespace Gettext;
use RuntimeException;
class GettextTranslator implements TranslatorInterface
{
/**
* Detects the current language using the environment variables.
*/
public function __construct(string $language = null)
{
if (!function_exists('gettext')) {
throw new RuntimeException('This class require the gettext extension for PHP');
}
//detects the language environment respecting the priority order
//http://php.net/manual/en/function.gettext.php#114062
if (empty($language)) {
$language = getenv('LANGUAGE') ?: getenv('LC_ALL') ?: getenv('LC_MESSAGES') ?: getenv('LANG');
}
if (!empty($language)) {
$this->setLanguage($language);
}
}
/**
* Define the current locale.
*/
public function setLanguage(string $language, int $category = null): self
{
if ($category === null) {
$category = defined('LC_MESSAGES') ? LC_MESSAGES : LC_ALL;
}
setlocale($category, $language);
putenv('LANGUAGE='.$language);
return $this;
}
/**
* Loads a gettext domain.
*/
public function loadDomain(string $domain, string $path = null, bool $default = true): self
{
bindtextdomain($domain, $path);
bind_textdomain_codeset($domain, 'UTF-8');
if ($default) {
textdomain($domain);
}
return $this;
}
public function noop(string $original): string
{
return $original;
}
public function gettext(string $original): string
{
return gettext($original);
}
public function ngettext(string $original, string $plural, int $value): string
{
return ngettext($original, $plural, $value);
}
public function dngettext(string $domain, string $original, string $plural, int $value): string
{
return dngettext($domain, $original, $plural, $value);
}
public function npgettext(string $context, string $original, string $plural, int $value): string
{
$message = $context."\x04".$original;
$translation = ngettext($message, $plural, $value);
return ($translation === $message) ? $original : $translation;
}
public function pgettext(string $context, string $original): string
{
$message = $context."\x04".$original;
$translation = gettext($message);
return ($translation === $message) ? $original : $translation;
}
public function dgettext(string $domain, string $original): string
{
return dgettext($domain, $original);
}
public function dpgettext(string $domain, string $context, string $original): string
{
$message = $context."\x04".$original;
$translation = dgettext($domain, $message);
return ($translation === $message) ? $original : $translation;
}
public function dnpgettext(string $domain, string $context, string $original, string $plural, int $value): string
{
$message = $context."\x04".$original;
$translation = dngettext($domain, $message, $plural, $value);
return ($translation === $message) ? $original : $translation;
}
}