Test Failed
Push — master ( f0f19e...7f25ec )
by Joël
02:58
created

Gettext::_putEnv()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 1
nop 0
dl 0
loc 11
ccs 0
cts 0
cp 0
crap 6
rs 9.4285
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 int 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 10
            ->_bindTextDomain()
48
            ->_textDomain()
49
            ->_setLocale()
50 10
            ->_putEnv()
51 10
            ->_checkLocaleFile()
52 10
        ;
53 10
    }
54
55 10
    /**
56 10
     * Load a domain
57
     * @param string $domain
58 10
     */
59 10
    public function changeDomain($domain)
60
    {
61
        log_message('info', 'Gettext Library Class -> Change domain');
62 10
63 10
        $this->_textDomain = $domain;
64 10
65 10
        $this
66
            ->_bindTextDomainCodeSet()
67 10
            ->_bindTextDomain()
68 10
            ->_textDomain()
69
            ->_checkLocaleFile()
70 10
        ;
71 10
    }
72
73
    /**
74 10
     * Merge config as parameter and default config (config/gettext.php file)
75 10
     * @param array $config
76 10
     */
77
    private function _setConfig(array $config = array())
78 10
    {
79 10
        $this->_localeDir = isset($config['gettext_locale_dir'])
80
            ? $config['gettext_locale_dir'] : config_item('gettext_locale_dir');
81 10
82 10
        $this->_textDomain = isset($config['gettext_text_domain'])
83
            ? $config['gettext_text_domain'] : config_item('gettext_text_domain');
84
85 10
        $this->_catalogCodeSet = isset($config['gettext_catalog_codeset'])
86 10
            ? $config['gettext_catalog_codeset'] : config_item('gettext_catalog_codeset');
87 10
88 10
        $this->_locale = isset($config['gettext_locale'])
89
            ? $config['gettext_locale'] : config_item('gettext_locale');
90 10
91 10
        $this->_category = LC_ALL;
92
    }
93 10
94 10
    /**
95
     * Gettext catalog codeset
96 10
     * @return $this
97
     */
98
    private function _bindTextDomainCodeSet()
99 10
    {
100 10
        $isBindTextDomainCodeSet = bind_textdomain_codeset($this->_textDomain, $this->_catalogCodeSet);
101 10
102 10
        log_message(
103 10
            (is_string($isBindTextDomainCodeSet) ? 'info' : 'error'),
104
            'Gettext Library -> Bind ' .
105 10
            'text domain: ' . $this->_textDomain . ' - ' .
106 10
            'with code set: ' . $this->_catalogCodeSet
107
        );
108 10
109
        return $this;
110
    }
111 10
112 10
    /**
113 10
     * Path to gettext locales directory relative to APPPATH
114 10
     * @return $this
115 10
     */
116 10 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...
117
    {
118 10
        $isBindTextDomain = bindtextdomain($this->_textDomain, APPPATH . $this->_localeDir);
119 10
120
        log_message(
121
            (is_string($isBindTextDomain) ? 'info' : 'error'),
122 10
            'Gettext Library -> Bind ' .
123 10
            'text domain: ' . $this->_textDomain . ' - ' .
124
            'with directory: ' . APPPATH . $this->_localeDir
125
        );
126
127
        return $this;
128
    }
129
130
    /**
131
     * Gettext domain
132
     * @return $this
133
     */
134
    private function _textDomain()
135
    {
136
        $isSetTextDomain = textdomain($this->_textDomain);
137
138
        log_message(
139
            (is_string($isSetTextDomain) ? 'info' : 'error'),
140
            'Gettext Library -> Set text domain: ' . $this->_textDomain
141
        );
142
143
        return $this;
144
    }
145
146
    /**
147
     * Gettext locale
148
     * @return $this
149
     */
150
    private function _setLocale()
151
    {
152
        $isSetLocale = setlocale($this->_category, $this->_locale);
153
154
        log_message(
155
            (is_string($isSetLocale) ? 'info' : 'error'),
156
            'Gettext Library -> ' .
157
            'Set locale: ' . (is_array($this->_locale) ? print_r($this->_locale, TRUE) : $this->_locale). ' ' .
158
            'for category: ' . $this->_category
159
        );
160
161
        // the new current locale, or FALSE if the locale is not implemented on your platform
162
        $this->_locale = $isSetLocale;
163
164
        return $this;
165
    }
166
167
    /**
168
     * Change environment language for CLI
169
     * @return $this
170
     */
171
    private function _putEnv()
172
    {
173
        $isPutEnv = putenv('LANGUAGE=' . $this->_locale);
174
175
        log_message(
176
            ($isPutEnv === TRUE ? 'info' : 'error'),
177
            'Gettext Library -> Set environment language: ' . $this->_locale
178
        );
179
180
        return $this;
181
    }
182
183
    /**
184
     * MO file exists for locale
185
     * @return $this
186
     */
187 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...
188
    {
189
        $file = APPPATH . $this->_localeDir . '/' . $this->_locale . '/LC_MESSAGES/' . $this->_textDomain . '.mo';
190
191
        log_message(
192
            (is_file($file) === TRUE ? 'info' : 'error'),
193
            'Gettext Library -> Check MO file exists: ' . $file
194
        );
195
196
        return $this;
197
    }
198
}
199
200
/* End of file Gettext.php */
201
/* Location: ./libraries/gettext.php */