Passed
Push — master ( fdd631...9022e5 )
by Joël
02:48
created

Gettext::_putEnv()   B

Complexity

Conditions 4
Paths 1

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 14
nc 1
nop 0
dl 0
loc 22
ccs 17
cts 17
cp 1
crap 4
rs 8.9197
c 0
b 0
f 0
1
<?php
2
// @codeCoverageIgnoreStart
3
defined('BASEPATH') || exit('No direct script access allowed');
4
// @codeCoverageIgnoreEnd
5
6
/**
7
 * Codeigniter PHP framework library class for dealing with gettext.
8
 *
9
 * @package     CodeIgniter
10
 * @subpackage  Libraries
11
 * @category    Language
12
 * @author      Joël Gaujard <[email protected]>
13
 * @author      Marko Martinović <[email protected]>
14
 * @link        https://github.com/joel-depiltech/codeigniter-gettext
15
 */
16
class Gettext
17
{
18
    /** @var string domain name match with file contains translation */
19
    private $_textDomain;
20
21
    /** @var string character encoding in which the messages are */
22
    private $_catalogCodeSet;
23
24
    /** @var string the path for a domain */
25
    private $_localeDir;
26
27
    /** @var string|array locale or array of locales */
28
    private $_locale;
29
30
    /** @var string constant specifying the category of the functions affected by the locale setting */
31
    private $_category;
32
33
    /**
34
     * Initialize Codeigniter PHP framework and get configuration
35
     *
36
     * @codeCoverageIgnore
37
     * @param array $config Override default configuration
38
     */
39
    public function __construct(array $config = array())
40
    {
41
        log_message('info', 'Gettext Library Class Initialized');
42
43
        $this->_setConfig($config);
44
45
        $this
46
            ->_bindTextDomainCodeSet()
47
            ->_bindTextDomain()
48
            ->_textDomain()
49
            ->_setLocale()
50
            ->_putEnv()
51
            ->_checkLocaleFile()
52
        ;
53
    }
54
55
    /**
56
     * Merge config as parameter and default config (config/gettext.php file)
57
     * @param array $config
58
     */
59 27
    private function _setConfig(array $config = array())
60
    {
61 27
        $this->_localeDir = isset($config['gettext_locale_dir'])
62 27
            ? $config['gettext_locale_dir'] : config_item('gettext_locale_dir');
63
64 27
        $this->_textDomain = isset($config['gettext_text_domain'])
65 27
            ? $config['gettext_text_domain'] : config_item('gettext_text_domain');
66
67 27
        $this->_catalogCodeSet = isset($config['gettext_catalog_codeset'])
68 27
            ? $config['gettext_catalog_codeset'] : config_item('gettext_catalog_codeset');
69
70 27
        $this->_locale = isset($config['gettext_locale'])
71 27
            ? $config['gettext_locale'] : config_item('gettext_locale');
72
73 27
        $this->_category = (isset($config['gettext_category'])
74 27
            ? $config['gettext_category'] : config_item('gettext_category'));
75 27
    }
76
77
    /**
78
     * Load a domain
79
     * @param string $domain
80
     */
81 10
    public function changeDomain($domain)
82
    {
83 10
        log_message('info', 'Gettext Library Class -> Change domain');
84
85 10
        $this->_textDomain = $domain;
86
87 10
        $this
88 10
            ->_bindTextDomainCodeSet()
89 10
            ->_bindTextDomain()
90 10
            ->_textDomain()
91 10
            ->_checkLocaleFile()
92
        ;
93 10
    }
94
95
    /**
96
     * Switch to a category
97
     * @param $category
98
     */
99 1
    public function changeCategory($category)
100
    {
101 1
        log_message('info', 'Gettext Library Class -> Change category');
102
103 1
        $this->_category = $category;
104
105 1
        $this->_setLocale();
106 1
    }
107
108
    /**
109
     * Gettext catalog codeset
110
     * @return $this
111
     */
112 27
    private function _bindTextDomainCodeSet()
113
    {
114 27
        $isBindTextDomainCodeSet = bind_textdomain_codeset($this->_textDomain, $this->_catalogCodeSet);
115
116 27
        log_message(
117 27
            (is_string($isBindTextDomainCodeSet) ? 'info' : 'error'),
118
            'Gettext Library -> Bind ' .
119 27
            'text domain: ' . $this->_textDomain . ' - ' .
120 27
            'with code set: ' . $this->_catalogCodeSet
121 27
        );
122
123 27
        return $this;
124
    }
125
126
    /**
127
     * Path to gettext locales directory relative to APPPATH
128
     * @return $this
129
     */
130 27 View Code Duplication
    private function _bindTextDomain()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
131
    {
132 27
        $isBindTextDomain = bindtextdomain($this->_textDomain, APPPATH . $this->_localeDir);
133
134 27
        log_message(
135 27
            (is_string($isBindTextDomain) ? 'info' : 'error'),
136
            'Gettext Library -> Bind ' .
137 27
            'text domain: ' . $this->_textDomain . ' - ' .
138 27
            'with directory: ' . APPPATH . $this->_localeDir
139 27
        );
140
141 27
        return $this;
142
    }
143
144
    /**
145
     * Gettext domain
146
     * @return $this
147
     */
148 27
    private function _textDomain()
149
    {
150 27
        $isSetTextDomain = textdomain($this->_textDomain);
151
152 27
        log_message(
153 27
            (is_string($isSetTextDomain) ? 'info' : 'error'),
154 27
            'Gettext Library -> Set text domain: ' . $this->_textDomain
155 27
        );
156
157 27
        return $this;
158
    }
159
160
    /**
161
     * Gettext locale
162
     * @return $this
163
     */
164 27
    private function _setLocale()
165
    {
166 27
        $isSetLocale = setlocale(constant($this->_category), $this->_locale);
167
168 27
        log_message(
169 27
            (is_string($isSetLocale) ? 'info' : 'error'),
170
            'Gettext Library -> ' .
171 27
            'Set locale: ' . (is_array($this->_locale) ? print_r($this->_locale, TRUE) : $this->_locale). ' ' .
172 27
            'for category: ' . $this->_category
173 27
        );
174
175
        // the new current locale, or FALSE if the locale is not implemented on your platform
176 27
        $this->_locale = $isSetLocale;
177
178 27
        return $this;
179
    }
180
181
    /**
182
     * Change environment language for CLI
183
     * @return $this
184
     */
185 27
    private function _putEnv()
186
    {
187 27
        $isPutEnv = putenv('LANGUAGE=' . $this->_locale);
188 27
        log_message(
189 27
            ($isPutEnv ? 'info' : 'error'),
190 27
            'Gettext Library -> Set environment LANGUAGE: ' . $this->_locale
191 27
        );
192
193 27
        $isPutEnv = putenv('LANG=' . $this->_locale);
194 27
        log_message(
195 27
            ($isPutEnv ? 'info' : 'error'),
196 27
            'Gettext Library -> Set environment LANG: ' . $this->_locale
197 27
        );
198
199 27
        $isPutEnv = putenv($this->_category . '=' . $this->_locale);
200 27
        log_message(
201 27
            ($isPutEnv ? 'info' : 'error'),
202 27
            'Gettext Library -> Set environment category ' . $this->_category . ': ' . $this->_locale
203 27
        );
204
205 27
        return $this;
206
    }
207
208
    /**
209
     * MO file exists for locale
210
     * @return $this
211
     */
212 27 View Code Duplication
    private function _checkLocaleFile()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
213
    {
214 27
        $file = APPPATH . $this->_localeDir . '/' . $this->_locale . '/LC_MESSAGES/' . $this->_textDomain . '.mo';
215
216 27
        log_message(
217 27
            (is_file($file) === TRUE ? 'info' : 'error'),
218
            'Gettext Library -> Check MO file exists: ' . $file
219 27
        );
220
221 27
        return $this;
222
    }
223
}
224
225
/* End of file Gettext.php */
226
/* Location: ./libraries/gettext.php */
227