MY_Lang::_trans()   B
last analyzed

Complexity

Conditions 7
Paths 4

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 4
nop 2
dl 0
loc 17
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
if (!defined('BASEPATH')) {
4
    exit('No direct script access allowed');
5
}
6
7
/**
8
 * Code Igniter Gettext Extension library
9
 *
10
 * This Library overides the original CI's language class. Needs the  $config['language'] variable set as it_IT or en_EN or fr_FR ...
11
 *
12
 * @package       Gettext Extension
13
 * @author        wokamoto
14
 * @copyright     Copyright (c) 2012
15
 * @license       http://www.gnu.org/licenses/lgpl.txt
16
 * @link
17
 * @version       Version 0.1
18
 * @since         2012 January, 27th
19
 */
20
// ------------------------------------------------------------------------
21
22
class MY_Lang extends CI_Lang
23
{
24
25
    private $ci;
26
27
    private $gettext_language;
28
29
    private $gettext_codeset;
30
31
    private $gettext_domain;
32
33
    private $gettext_path;
34
35
    /**
36
     * The constructor initialize the library
37
     *
38
     * @return MY_Lang
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
39
     */
40
    public function __construct() {
41
        parent::__construct();
42
    }
43
44
    private function _language() {
45
        static $language;
46
47
        if (!isset($this->ci)) {
48
            $this->ci = & get_instance();
49
        }
50
51
        if (!isset($language)) {
52
            $language = $this->ci->config->item('language');
53
        }
54
55
        if (!isset($language) || !in_array($language, $this->ci->config->item('selectable_languages'))) {
56
            $language = 'english';
57
        }
58
59
        if ($language != $this->ci->config->item('language')) {
60
            $this->ci->config->set_item('language', $language);
61
        }
62
63
        return empty($language) ? 'english' : $language;
64
    }
65
66
    /**
67
     * Load a language file
68
     *
69
     * @access	public
70
     * @param	mixed	the name of the language file to be loaded. Can be an array
71
     * @param	string	the language (english, etc.)
72
     * @return	mixed
73
     */
74
    public function load($langfile = '', $idiom = '', $return = FALSE, $add_suffix = TRUE, $alt_path = '') {
75
        return parent::load($langfile, !empty($idiom) ? $idiom : $this->_language(), $return, $add_suffix, $alt_path);
76
    }
77
78
    /**
79
     * This method overides the original load method. Its duty is loading the domain files by config or by default internal settings.
80
     *
81
     * @access	public
82
     * @param	string	$userlang	the language, set as ja_JP or it_IT or en_EN or fr_FR ...
83
     * @param	string	$codeset	the codeset, set as UTF-8 or EUC ...
84
     * @return	bool
85
     */
86
    public function load_gettext($userlang = '', $codeset = '', $textdomain = 'lang', $path = '') {
87
        if (!isset($this->ci)) {
88
            $this->ci = & get_instance();
89
        }
90
91
        $this->gettext_language = $this->language_select(!empty($userlang) ? $userlang : $this->_language());
92
        $this->gettext_codeset = !empty($codeset) ? $codeset : $this->ci->config->item('charset');
93
        $this->gettext_domain = $textdomain;
94
        $this->gettext_path = !empty($path) ? $path : APPPATH . 'language/locale';
95
        log_message('debug', 'Gettext Class language was set by parameter:' . $this->gettext_language . ',' . $this->gettext_codeset);
96
97
        /* put env and set locale */
98
        putenv("LANG={$this->gettext_language}");
99
        setlocale(LC_ALL, $this->gettext_language);
100
101
        /* bind text domain */
102
        $textdomain_path = bindtextdomain($this->gettext_domain, $this->gettext_path);
103
        bind_textdomain_codeset($this->gettext_domain, $this->gettext_codeset);
104
        textdomain($this->gettext_domain);
105
        log_message('debug', 'Gettext Class path: ' . $textdomain_path);
106
        log_message('debug', 'Gettext Class the domain: ' . $this->gettext_domain);
107
108
        return true;
109
    }
110
111
    private function language_select($userlang = '') {
112
        $userlang = !empty($userlang) ? $userlang : $this->_language();
113
114
        switch ($userlang) {
115
            case 'japanese':
116
                $userlang = 'ja_JP';
117
                break;
118
            case 'english':
119
            default:
120
                $userlang = 'en';
121
                break;
122
        }
123
        return $userlang;
124
    }
125
126
    /**
127
     * Fetch a single line of text from the language array
128
     *
129
     * @access	public
130
     * @param	string	$line	the language line
131
     * @return	string
132
     */
133
    public function line($line = '', $params = FALSE) {
134
135
        if ($params !== FALSE || FALSE === ($value = parent::line($line))) {
136
            $value = $this->_trans($line, $params);
137
        }
138
139
        return $value ? $value : $line;
140
    }
141
142
    /**
143
     *  Plural forms added by Tchinkatchuk
144
     *  http://www.codeigniter.com/forums/viewthread/2168/
145
     */
146
147
    /**
148
     * The translator method
149
     *
150
     * @access private
151
     * @param string $original the original string to translate
152
     * @param array $aParams the plural parameters
0 ignored issues
show
Documentation introduced by
Should the type for parameter $aParams not be false|array? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
153
     * @return false|string the string translated
154
     */
155
    private function _trans($original, $aParams = false) {
156
        if (!isset($this->gettext_domain)) {
157
            return false;
158
        }
159
160
        if ($aParams && isset($aParams['plural']) && isset($aParams['count'])) {
161
            $sTranslate = ngettext($original, $aParams['plural'], $aParams['count']);
162
            $sTranslate = $this->replaceDynamically($sTranslate, $aParams);
163
        } else {
164
            $sTranslate = gettext($original);
165
            if (is_array($aParams) && count($aParams)) {
166
                $sTranslate = $this->replaceDynamically($sTranslate, $aParams);
167
            }
168
        }
169
170
        return $sTranslate;
171
    }
172
173
    /**
174
     * Allow dynamic allocation in traduction
175
     *
176
     * @final
177
     * @access private
178
     * @param  string $sString
179
     * @return string
180
     */
181
    private function replaceDynamically($sString) {
182
        $aTrad = [];
183
        for ($i = 1, $iMax = func_num_args(); $i < $iMax; $i++) {
184
            $arg = func_get_arg($i);
185
            if (is_array($arg)) {
186
                foreach ($arg as $key => $sValue) {
187
                    $aTrad['%' . $key] = $sValue;
188
                }
189
            } else {
190
                $aTrad['%' . $key] = $arg;
0 ignored issues
show
Bug introduced by
The variable $key seems to be defined by a foreach iteration on line 186. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
191
            }
192
        }
193
194
        return strtr($sString, $aTrad);
195
    }
196
197
}