Completed
Push — master ( 1013b3...f7122d )
by Mārtiņš
02:08
created

LocaleLoader::load()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
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
}