1
|
|
|
<?php |
2
|
|
|
namespace WebServCo\Framework\Libraries; |
3
|
|
|
|
4
|
|
|
final class I18n extends \WebServCo\Framework\AbstractLibrary implements \WebServCo\Framework\Interfaces\I18nInterface |
5
|
|
|
{ |
6
|
|
|
protected $langs; |
7
|
|
|
protected $domain; |
8
|
|
|
|
9
|
|
|
protected $lang; |
10
|
|
|
|
11
|
|
|
protected $locale; |
12
|
|
|
|
13
|
|
|
protected $translationsPath; |
14
|
|
|
|
15
|
|
|
public function __construct($settings = []) |
16
|
|
|
{ |
17
|
|
|
parent::__construct($settings); |
18
|
|
|
|
19
|
|
|
$this->langs = $this->setting('langs', []); |
20
|
|
|
$this->domain = $this->setting('domain', 'messages'); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* After calling init(), a custom language/domain can be set by calling setLanguage with full arguments. |
25
|
|
|
* Call this function afterwards to restore the oriignal language/domain. |
26
|
|
|
*/ |
27
|
|
|
public function reset() |
28
|
|
|
{ |
29
|
|
|
$this->setLanguage($this->lang, $this->translationsPath); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function init($projectPath, $lang = null) |
33
|
|
|
{ |
34
|
|
|
$this->translationsPath = $projectPath . 'resources/translations'; |
35
|
|
|
|
36
|
|
|
$lang = $lang ? $lang : $this->setting('lang', 'en'); |
37
|
|
|
$this->setLanguage($lang); |
38
|
|
|
|
39
|
|
|
return true; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function setLanguage($lang, $translationsPath = null) |
43
|
|
|
{ |
44
|
|
|
if (!array_key_exists($lang, $this->langs)) { |
45
|
|
|
throw new \WebServCo\Framework\Exceptions\ApplicationException( |
46
|
|
|
sprintf('Language not available: %s', $lang) |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$this->lang = $lang; |
51
|
|
|
$this->locale = $this->langs[$this->lang]['locale']; |
52
|
|
|
|
53
|
|
|
$this->setLocale($this->locale); |
54
|
|
|
$this->setDomain($this->domain, $translationsPath ?? $this->translationsPath); |
55
|
|
|
|
56
|
|
|
return true; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function getLanguage() |
60
|
|
|
{ |
61
|
|
|
return $this->lang; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function getLanguages() |
65
|
|
|
{ |
66
|
|
|
return $this->langs; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
protected function setLocale($locale) |
70
|
|
|
{ |
71
|
|
|
/** |
72
|
|
|
* Rumored to allow using a locale regardless of server locale setup. |
73
|
|
|
* putenv("LANGUAGE=" . $locale); |
74
|
|
|
*/ |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Rumored to be needed on Win. |
78
|
|
|
* putenv("LANG=" . $locale); |
79
|
|
|
*/ |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Do not use LC_ALL, in order to skip LC_NUMERIC. |
83
|
|
|
*/ |
84
|
|
|
if (defined('LC_MESSAGES')) { |
85
|
|
|
setlocale(LC_COLLATE, $locale); |
86
|
|
|
setlocale(LC_CTYPE, $locale); |
87
|
|
|
setlocale(LC_MONETARY, $locale); |
88
|
|
|
setlocale(LC_TIME, $locale); |
89
|
|
|
setlocale(LC_MESSAGES, $locale); |
90
|
|
|
} else { // Windows |
91
|
|
|
setlocale(LC_ALL, $locale); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return true; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
protected function setDomain($domain, $directory) |
98
|
|
|
{ |
99
|
|
|
bindtextdomain($domain, $directory); |
100
|
|
|
textdomain($domain); |
101
|
|
|
bind_textdomain_codeset($domain, 'UTF8'); |
102
|
|
|
|
103
|
|
|
return true; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|