language_helper.php ➔ chose_language()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 6
nop 1
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
if (!defined('BASEPATH')) {
4
    exit('No direct script access allowed');
5
}
6
/**
7
 * CodeIgniter
8
 *
9
 * An open source application development framework for PHP 5.1.6 or newer
10
 *
11
 * @package     CodeIgniter
12
 * @author      ExpressionEngine Dev Team
13
 * @copyright   Copyright (c) 2008 - 2011, EllisLab, Inc.
14
 * @license     http://codeigniter.com/user_guide/license.html
15
 * @link        http://codeigniter.com
16
 * @since       Version 1.0
17
 * @filesource
18
 */
19
// ------------------------------------------------------------------------
20
21
/**
22
 * CodeIgniter Language Helpers
23
 *
24
 * @package     CodeIgniter
25
 * @subpackage  Helpers
26
 * @category    Helpers
27
 * @author      ExpressionEngine Dev Team
28
 * @link        http://codeigniter.com/user_guide/helpers/language_helper.html
29
 */
30
// ------------------------------------------------------------------------
31
32
/**
33
 * Lang
34
 *
35
 * Fetches a language variable and optionally outputs a form label
36
 *
37
 * @access  public
38
 * @param   string  the language line
39
 * @param   string  the id of the form element
40
 * @return  string
41
 */
42
if (!function_exists('lang')) {
43
44
    function lang($line, $domain = 'main', $wrapper = TRUE) {
45
46
        $translation = MY_Lang::getTranslation($line, $domain);
47
48
        if ($wrapper && defined('ENABLE_TRANSLATION_API')) {
49
            $translation = "<translate origin='" . $line . "' domain='" . $domain . "'>" . $translation . '</translate>';
50
        }
51
52
        return $translation;
53
    }
54
55
}
56
57
if (!function_exists('getPoFileAttributes')) {
58
59
    /**
60
     * @param string $domain
61
     * @return array|bool
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array|false.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
62
     */
63
    function getPoFileAttributes($domain) {
64
65
        if ($domain) {
66
            $CI = &get_instance();
67
68
            if (strstr($CI->input->serevr('HTTP_REFERER'), 'admin')) {
69
                $language = $CI->config->item('language');
70
                $locale = $language;
71
            } else {
72
                $currentLocale = MY_Controller::getCurrentLocale();
73
                $language = $CI->db->where('identif', $currentLocale)->get('languages');
74
                $language = $language->row_array();
75
                $locale = $language['locale'];
76
            }
77
78
            if ($locale) {
79
                $attributes = [];
80
81
                switch ($domain) {
82
                    case 'main':
83
                        $attributes = [
84
                                       'name' => 'main',
85
                                       'type' => 'main',
86
                                       'lang' => $locale,
87
                                      ];
88
                        break;
89
                    default :
90
                        if (moduleExists($domain)) {
91
                            $attributes = [
92
                                           'name' => $domain,
93
                                           'type' => 'modules',
94
                                           'lang' => $locale,
95
                                          ];
96
                        } elseif (file_exists('./templates/' . $domain)) {
97
                            $attributes = [
98
                                           'name' => $domain,
99
                                           'type' => 'templates',
100
                                           'lang' => $locale,
101
                                          ];
102
                        }
103
                        break;
104
                }
105
                return $attributes;
106
            }
107
        }
108
        return FALSE;
109
    }
110
111
}
112
113
if (!function_exists('getModulePathForTranslator')) {
114
115
    /**
116
     *
117
     * @param string $module_name
118
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|boolean?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
119
     */
120
    function getModulePathForTranslator($module_name) {
121
122
        $module_path = getModulePath($module_name);
123
124
        if (MAINSITE) {
125
            $module_path = str_replace(MAINSITE, '.', $module_path);
126
        }
127
128
        return $module_path;
129
    }
130
131
}
132
133
134
if (!function_exists('getMoFileName')) {
135
136
    /**
137
     * @param string $domain
138
     * @return string
139
     */
140
    function getMoFileName($domain) {
141
142
        if ($domain) {
143
            if (array_key_exists('MO_FILE_NAMES', $GLOBALS) && array_key_exists($domain, $GLOBALS['MO_FILE_NAMES'])) {
144
                return $GLOBALS['MO_FILE_NAMES'][$domain];
145
            }
146
147
            $GLOBALS['MO_FILE_NAMES'] = array_key_exists('MO_FILE_NAMES', $GLOBALS) && $GLOBALS['MO_FILE_NAMES'] ? $GLOBALS['MO_FILE_NAMES'] : [];
148
            $GLOBALS['MO_FILE_NAMES'][$domain] = str_replace('.mo', '', basename(getMoFilePath($domain)));
149
            return $GLOBALS['MO_FILE_NAMES'][$domain];
150
        }
151
        return FALSE;
152
    }
153
154
}
155
156
if (!function_exists('getMoFilePath')) {
157
158
    /**
159
     * @param string $domain
160
     * @return string
161
     */
162
    function getMoFilePath($domain) {
163
164
        if ($domain) {
165
            if (!defined('CUR_LOCALE')) {
166
                $CI = &get_instance();
167
168
                if (strstr($CI->input->server('REQUEST_URI'), 'install')) {
169
                    $lang = $CI->config->item('language');
170
171
                    $locale = $lang ?: 'ru_RU';
172
                } else {
173
                    if (strstr(uri_string(), 'admin')) {
174
                        $locale = $CI->config->item('language');
175
                    } else {
176
177
                        $locale = MY_Controller::getCurrentLanguage('locale');
178
                    }
179
                }
180
181
                define('CUR_LOCALE', $locale);
182
            } else {
183
                $locale = CUR_LOCALE;
184
            }
185
186
            switch ($domain) {
187
                case 'main':
188
                    $searched = glob(correctUrl('./application/language/main/' . $locale) . '/' . $locale . '/LC_MESSAGES/main*.mo');
189
                    if (!empty($searched)) {
190
                        $filePath = array_pop($searched);
191
                    }
192
                    break;
193
                default :
194
                    $module_path = correctUrl('./application/modules/' . $domain . '/language/' . $locale);
195
                    if (is_dir($module_path)) {
196
                        $searched = glob($module_path . '/' . $locale . '/LC_MESSAGES/' . $domain . '*.mo');
197
                        if (!empty($searched)) {
198
                            $filePath = array_pop($searched);
199
                        }
200
                    } elseif (file_exists(TEMPLATES_PATH . $domain)) {
201
                        $searched = glob(TEMPLATES_PATH . $domain . '/language/' . $domain . '/' . $locale . '/LC_MESSAGES/' . $domain . '*.mo');
202
                        if (!empty($searched)) {
203
                            $filePath = array_pop($searched);
204
                        }
205
                    }
206
                    break;
207
            }
208
209
            return $filePath;
210
        }
211
        return FALSE;
212
    }
213
214
}
215
216
217
if (!function_exists('correctUrl')) {
218
219
    /**
220
     * @param string $url
221
     *
222
     * @return string
223
     */
224
    function correctUrl($url) {
225
226
        if (MAINSITE) {
227
            $url = MAINSITE . str_replace('./', '/', $url);
228
        }
229
230
        $last_slash_pos = strrpos($url, '/');
231
        $url = str_replace(substr($url, $last_slash_pos), '', $url);
232
        return $url;
233
    }
234
235
}
236
237
// select language identif from url address
238
if (!function_exists('chose_language')) {
239
240
    /**
241
     * @param bool|FALSE $active
242
     * @return mixed
243
     */
244
    function chose_language($active = FALSE) {
245
        $ci = &get_instance();
246
247
        $url_arr = $ci->uri->segment_array();
248
        foreach ((array) MY_Controller::getAllLocales($active) as $l) {
249
            if (in_array($l, $url_arr)) {
250
                $lang = $l;
251
            }
252
        }
253
254
        if (!$lang) {
255
            $lang = MY_Controller::getDefaultLanguage()['identif'];
256
        }
257
258
        return $lang;
259
    }
260
261
}
262
263
/**
264
 * @param string $flag
265
 * @return array
266
 */
267
function get_main_lang($flag = null) {
268
    $ci = &get_instance();
269
    if (!$ci->db) {
270
        return FALSE;
271
    }
272
273
    $lang = $ci->db->get('languages')->result_array();
274
    $lan_array = [];
275 View Code Duplication
    foreach ($lang as $l) {
276
        $lan_array[$l['identif']] = $l['id'];
277
        $lan_array_rev[$l['id']] = $l['identif'];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$lan_array_rev was never initialized. Although not strictly required by PHP, it is generally a good practice to add $lan_array_rev = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
278
    }
279
280
    $lang_uri = $ci->uri->segment(1);
281
282 View Code Duplication
    if (in_array($lang_uri, $lan_array_rev)) {
283
        $lang_id = $lan_array[$lang_uri];
284
        $lang_ident = $lang_uri;
285
    } else {
286
        $lang = $ci->db->where('default', 1)->get('languages')->result_array();
287
        $lang_id = $lang[0]['id'];
288
        $lang_ident = $lang[0]['identif'];
289
    }
290
291
    if ($flag == 'id') {
292
        return $lang_id;
293
    }
294
295
    if ($flag == 'identif') {
296
        return $lang_ident;
297
    }
298
299
    if ($flag == null) {
300
        return [
301
                'id'      => $lang_id,
302
                'identif' => $lang_ident,
303
               ];
304
    }
305
}
306
307
/**
308
 * Get admin locale name
309
 */
310
function get_admin_locale() {
311
312
    $ci = &get_instance();
313
    $admin_language = $ci->config->item('language');
314
    $all_languages = $ci->config->item('languages');
315
316
    return isset($all_languages[$admin_language][0]) ? $all_languages[$admin_language][0] : 'ru';
317
}
318
319
/**
320
 * Same as lang() function, but can adding data into line
321
 *
322
 * @param string $line
323
 * @param string $name textdomain name
324
 * @param string|array $data data for adding to line
325
 * @return string
326
 */
327
if (!function_exists('langf')) {
328
329
    /**
330
     * @param string $line
331
     * @param string $name
332
     * @param array $data
333
     * @return null|string
334
     */
335
    function langf($line, $name = 'main', array $data = []) {
336
337
        $line = lang($line, $name);
338
339
        foreach ($data as $key => $value) {
340
            $line = str_replace('|' . $key . '|', $value, $line);
341
        }
342
343
        return $line;
344
    }
345
346
}
347
348 View Code Duplication
if (!function_exists('tlang')) {
349
350
    /**
351
     * @param string $line
352
     * @return string
353
     */
354
    function tlang($line) {
355
356
        $CI = &get_instance();
357
        $name = $CI->config->item('template');
358
359
        return lang($line, $name);
360
    }
361
362
}
363
364
if (!function_exists('tlangf')) {
365
366
    /**
367
     * @param string $line
368
     * @param array $data
369
     * @return mixed|string
370
     */
371
    function tlangf($line, array $data = []) {
372
373
        $CI = &get_instance();
374
        $name = $CI->config->item('template');
375
        $line = lang($line, $name);
376
377
        foreach ($data as $key => $value) {
378
            $line = str_replace('|' . $key . '|', $value, $line);
379
        }
380
381
        return $line;
382
    }
383
384
}
385
386
if (!function_exists('getLanguage')) {
387
388
    /**
389
     * @param array $where_array
390
     * @return array
391
     */
392
    function getLanguage($where_array = []) {
393
394
        $languages = CI::$APP->db->where($where_array)->get('languages');
395
        return $languages ? $languages->row_array() : [];
396
    }
397
398
}
399
400
if (!function_exists('current_language')) {
401
402
    /**
403
     * @return string
404
     */
405
    function current_language() {
406
407
        $language = MY_Controller::getCurrentLanguage();
408
        /* Return language code from locale */
409
        return strstr($language['locale'], '_', true);
410
    }
411
412
}
413
// ------------------------------------------------------------------------
414
/* End of file language_helper.php */
415
/* Location: ./system/helpers/language_helper.php */