Passed
Push — master ( 18961c...b447ac )
by Joël
04:33
created

Gettext::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 1
dl 0
loc 17
ccs 0
cts 0
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
    /**
19
     * Initialize Codeigniter PHP framework and get configuration
20
     *
21
     * @codeCoverageIgnore
22
     * @param array $config Override default configuration
23
     */
24
    public function __construct($config = array())
25
    {
26
        log_message('info', 'Gettext Library Class Initialized');
27
28
        // Merge $config and config/gettext.php $config
29
        $config = array_merge(
30
            array(
31
                'gettext_locale_dir' => config_item('gettext_locale_dir'),
32
                'gettext_text_domain' => config_item('gettext_text_domain'),
33
                'gettext_catalog_codeset' => config_item('gettext_catalog_codeset'),
34
                'gettext_locale' => config_item('gettext_locale')
35
            ),
36
            $config
37
        );
38
39
        self::init($config);
40
    }
41
42
    /**
43
     * Initialize gettext inside Codeigniter PHP framework.
44
     *
45
     * @param array $config configuration
46
     */
47 11
    public static function init(array $config)
48
    {
49
        // Gettext catalog codeset
50 11
        $isBindTextDomainCodeset = bind_textdomain_codeset(
51 11
            $config['gettext_text_domain'],
52 11
            $config['gettext_catalog_codeset']
53 11
        );
54
55 11
        log_message(
56 11
            (is_string($isBindTextDomainCodeset) ? 'info' : 'error'),
57
            'Gettext Library -> Bind text domain code set: ' .
58 11
            $config['gettext_catalog_codeset']
59 11
        );
60
61
        // Path to gettext locales directory relative to FCPATH.APPPATH
62 11
        $isBindTextDomain = bindtextdomain(
63 11
            $config['gettext_text_domain'],
64 11
            APPPATH . $config['gettext_locale_dir']
65 11
        );
66
67 11
        log_message(
68 11
            (is_string($isBindTextDomain) ? 'info' : 'error'),
69
            'Gettext Library -> Bind text domain directory: ' .
70 11
            APPPATH . $config['gettext_locale_dir']
71 11
        );
72
73
        // Gettext domain
74 11
        $isSetTextDomain = textdomain(
75 11
            $config['gettext_text_domain']
76 11
        );
77
78 11
        log_message(
79 11
            (is_string($isSetTextDomain) ? 'info' : 'error'),
80
            'Gettext Library -> Set text domain: ' .
81 11
            $config['gettext_text_domain']
82 11
        );
83
84
        // Gettext locale
85 11
        $isSetLocale = setlocale(
86 11
            LC_ALL,
87 11
            $config['gettext_locale']
88 11
        );
89
90 11
        log_message(
91 11
            (is_string($isSetLocale) ? 'info' : 'error'),
92
            'Gettext Library -> Set locale: ' .
93 11
            (is_array($config['gettext_locale']) ?
94 11
                print_r($config['gettext_locale'], TRUE) : $config['gettext_locale']
95
            )
96 11
        );
97
98
        // Change environment language for CLI
99 11
        $gettext_locale = is_array($config['gettext_locale'])
100 11
            ? $config['gettext_locale'][key($config['gettext_locale'])]
101 11
            : $config['gettext_locale']
102 11
        ;
103 11
        $isPutEnv = putenv('LANGUAGE=' . $gettext_locale);
104
105 11
        log_message(
106 11
            ($isPutEnv === TRUE ? 'info' : 'error'),
107
            'Gettext Library -> Set environment language: ' . $gettext_locale
108 11
        );
109
110
        // MO file exists for language
111 11
        $file = APPPATH . $config['gettext_locale_dir'] .
112 11
            '/' . $isSetLocale .
113 11
            '/LC_MESSAGES/' .
114 11
            $config['gettext_text_domain'] . '.mo'
115 11
        ;
116 11
        $isFileExists = is_file($file);
117
118 11
        log_message(
119 11
            ($isFileExists === TRUE ? 'info' : 'error'),
120
            'Gettext Library -> Check MO file exists: ' .
121
            $file
122 11
        );
123 11
    }
124
}
125
126
/* End of file Gettext.php */
127
/* Location: ./libraries/config/gettext.php */