|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Printful\GettextCms; |
|
4
|
|
|
|
|
5
|
|
|
use Printful\GettextCms\Interfaces\MessageConfigInterface; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class sets locales and binds domains for current request |
|
9
|
|
|
*/ |
|
10
|
|
|
class LocaleLoader |
|
11
|
|
|
{ |
|
12
|
|
|
/** @var MessageConfigInterface */ |
|
13
|
|
|
private $config; |
|
14
|
|
|
|
|
15
|
|
|
/** @var MessageRevisions */ |
|
16
|
|
|
private $revisions; |
|
17
|
|
|
|
|
18
|
1 |
|
public function __construct(MessageConfigInterface $config) |
|
19
|
|
|
{ |
|
20
|
1 |
|
$this->revisions = new MessageRevisions($config); |
|
21
|
1 |
|
$this->config = $config; |
|
22
|
1 |
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Globally set current locale and bind gettext domains |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $locale |
|
28
|
|
|
* @return bool If false is returned, some domains may not have been bound and will fail |
|
29
|
|
|
*/ |
|
30
|
1 |
|
public function load(string $locale): bool |
|
31
|
|
|
{ |
|
32
|
1 |
|
putenv("LANG=" . $locale); |
|
33
|
1 |
|
setlocale(LC_ALL, $locale); |
|
34
|
|
|
|
|
35
|
1 |
|
return $this->bindDomains($locale); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Bind all domains for locale |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $locale |
|
42
|
|
|
* @return bool Returns false if binding fails, translations may not work correctly |
|
43
|
|
|
*/ |
|
44
|
1 |
|
private function bindDomains(string $locale): bool |
|
45
|
|
|
{ |
|
46
|
1 |
|
$domainBindingFailed = false; |
|
47
|
|
|
|
|
48
|
1 |
|
$domainDir = rtrim($this->config->getMoDirectory(), '/'); |
|
49
|
|
|
|
|
50
|
1 |
|
$defaultDomain = $this->config->getDefaultDomain(); |
|
51
|
|
|
|
|
52
|
1 |
|
$allDomains = $this->config->getOtherDomains(); |
|
53
|
1 |
|
$allDomains[] = $defaultDomain; |
|
54
|
|
|
|
|
55
|
1 |
|
foreach ($allDomains as $domain) { |
|
56
|
1 |
|
$actualDomain = $this->getActualDomain($locale, $domain); |
|
57
|
|
|
|
|
58
|
|
|
// File structure for a translation is: mo-directory/{locale}/LC_MESSAGES/{domain}.mo |
|
59
|
1 |
|
if (!bindtextdomain($actualDomain, $domainDir)) { |
|
60
|
|
|
// If text domain binding fails, something is wrong with the paths |
|
61
|
|
|
$domainBindingFailed = true; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
1 |
|
bind_textdomain_codeset($actualDomain, 'utf8'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
// Bind the default domain for _() calls (and other non-domain specific calls) |
|
68
|
1 |
|
textdomain($this->getActualDomain($locale, $defaultDomain)); |
|
69
|
|
|
|
|
70
|
1 |
|
return $domainBindingFailed; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Get actual domain name. If revisions are enabled, this will return something like "domain_xxxxxx" |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $locale |
|
77
|
|
|
* @param string $domain |
|
78
|
|
|
* @return string |
|
79
|
|
|
*/ |
|
80
|
1 |
|
private function getActualDomain(string $locale, string $domain): string |
|
81
|
|
|
{ |
|
82
|
1 |
|
if ($this->config->useRevisions()) { |
|
83
|
|
|
return $this->revisions->getRevisionedDomain($locale, $domain); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
1 |
|
return $domain; |
|
87
|
|
|
} |
|
88
|
|
|
} |