Passed
Push — master ( 97f7f8...fdd631 )
by Joël
04:32
created

Gettext::changeCategory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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