Passed
Push — master ( fd07d3...a435d0 )
by Radu
17:51
created

I18n::getLocale()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 1
c 2
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
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
    public function getLanguage()
24
    {
25
        return $this->lang;
26
    }
27
28
    public function getLanguages()
29
    {
30
        return $this->langs;
31
    }
32
33
    public function getLocale()
34
    {
35
        return $this->locale;
36
    }
37
38
    public function init($projectPath, $lang = null)
39
    {
40
        $this->translationsPath = $projectPath . 'resources/translations';
41
42
        $lang = $lang ? $lang : $this->setting('lang', 'en');
43
        $this->setLanguage($lang);
44
45
        return true;
46
    }
47
48
    /**
49
    * After calling init(), a custom language/domain can be set by calling setLanguage with full arguments.
50
    * Call this function afterwards to restore the original language/domain.
51
    */
52
    public function reset()
53
    {
54
        $this->setLanguage($this->lang, $this->translationsPath);
55
    }
56
57
    public function setLanguage($lang, $translationsPath = null)
58
    {
59
        if (!array_key_exists($lang, $this->langs)) {
60
            throw new \WebServCo\Framework\Exceptions\ApplicationException(
61
                sprintf('Language not available: %s.', $lang)
62
            );
63
        }
64
65
        $this->lang = $lang;
66
        $this->locale = $this->langs[$this->lang]['locale'];
67
68
        $this->setLocale($this->locale);
69
        $this->setDomain($this->domain, $translationsPath ?? $this->translationsPath);
70
71
        return true;
72
    }
73
74
    protected function setDomain($domain, $directory)
75
    {
76
        bindtextdomain($domain, $directory);
77
        textdomain($domain);
78
        bind_textdomain_codeset($domain, 'UTF8');
79
80
        return true;
81
    }
82
83
    protected function setLocale($locale)
84
    {
85
        /**
86
         * Rumored to allow using a locale regardless of server locale setup.
87
         * putenv("LANGUAGE=" . $locale);
88
         */
89
90
        /**
91
         * Rumored to be needed on Win.
92
         * putenv("LANG=" . $locale);
93
         */
94
95
        /**
96
         * Do not use LC_ALL, in order to skip LC_NUMERIC.
97
         */
98
        if (defined('LC_MESSAGES')) {
99
            setlocale(LC_COLLATE, $locale);
100
            setlocale(LC_CTYPE, $locale);
101
            setlocale(LC_MONETARY, $locale);
102
            setlocale(LC_TIME, $locale);
103
            setlocale(LC_MESSAGES, $locale);
104
        } else { // Windows
105
            setlocale(LC_ALL, $locale);
106
        }
107
108
        return true;
109
    }
110
}
111