1
|
|
|
<?php |
|
|
|
|
2
|
|
|
// @codeCoverageIgnoreStart |
3
|
|
|
if (!defined('PHPUNIT_TESTING')) { |
4
|
|
|
defined('BASEPATH') || exit('No direct script access allowed'); |
5
|
|
|
} |
6
|
|
|
// @codeCoverageIgnoreEnd |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Codeigniter PHP framework library class for dealing with gettext. |
10
|
|
|
* |
11
|
|
|
* @package CodeIgniter |
12
|
|
|
* @subpackage Libraries |
13
|
|
|
* @category Language |
14
|
|
|
* @author Joël Gaujard <[email protected]> |
15
|
|
|
* @author Marko Martinović <[email protected]> |
16
|
|
|
* @link https://github.com/joel-depiltech/codeigniter-gettext |
17
|
|
|
*/ |
18
|
|
|
class Gettext |
|
|
|
|
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Initialize Codeigniter PHP framework and get configuration |
22
|
|
|
* |
23
|
|
|
* @codeCoverageIgnore |
24
|
|
|
* @param array $config Override default configuration |
25
|
|
|
*/ |
26
|
|
|
public function __construct($config = array()) |
27
|
|
|
{ |
28
|
|
|
log_message('info', 'Gettext Library Class Initialized'); |
29
|
|
|
|
30
|
|
|
$CI = &get_instance(); |
31
|
|
|
|
32
|
|
|
// Merge $config and config/gettext.php $config |
33
|
|
|
$config = array_merge( |
34
|
|
|
array( |
35
|
|
|
'gettext_locale_dir' => $CI->config->item('gettext_locale_dir'), |
36
|
|
|
'gettext_text_domain' => $CI->config->item('gettext_text_domain'), |
37
|
|
|
'gettext_catalog_codeset' => $CI->config->item('gettext_catalog_codeset'), |
38
|
|
|
'gettext_locale' => $CI->config->item('gettext_locale') |
39
|
|
|
), |
40
|
|
|
$config |
41
|
|
|
); |
42
|
|
|
|
43
|
|
|
self::init($config); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Initialize gettext inside Codeigniter PHP framework. |
48
|
|
|
* |
49
|
|
|
* @param array $config configuration |
50
|
|
|
*/ |
51
|
6 |
|
static public function init(array $config) |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
// Gettext catalog codeset |
54
|
6 |
|
$IsBindTextDomainCodeset = bind_textdomain_codeset( |
55
|
6 |
|
$config['gettext_text_domain'], |
56
|
6 |
|
$config['gettext_catalog_codeset'] |
57
|
6 |
|
); |
58
|
|
|
|
59
|
6 |
|
log_message( |
60
|
6 |
|
(is_string($IsBindTextDomainCodeset) ? 'info' : 'error'), |
61
|
|
|
'Gettext Library -> Try to bind text domain code set: ' . |
62
|
6 |
|
$config['gettext_catalog_codeset'] |
63
|
6 |
|
); |
64
|
|
|
|
65
|
|
|
// Path to gettext locales directory relative to FCPATH.APPPATH |
66
|
6 |
|
$IsBindTextDomain = bindtextdomain( |
67
|
6 |
|
$config['gettext_text_domain'], |
68
|
6 |
|
APPPATH . $config['gettext_locale_dir'] |
69
|
6 |
|
); |
70
|
|
|
|
71
|
6 |
|
log_message( |
72
|
6 |
|
(is_string($IsBindTextDomain) ? 'info' : 'error'), |
73
|
|
|
'Gettext Library -> Try to bind text domain directory: ' . |
74
|
6 |
|
APPPATH . $config['gettext_locale_dir'] |
75
|
6 |
|
); |
76
|
|
|
|
77
|
|
|
// Gettext domain |
78
|
6 |
|
$IsSetTextDomain = textdomain( |
79
|
6 |
|
$config['gettext_text_domain'] |
80
|
6 |
|
); |
81
|
|
|
|
82
|
6 |
|
log_message( |
83
|
6 |
|
(is_string($IsSetTextDomain) ? 'info' : 'error'), |
84
|
|
|
'Gettext Library -> Try to set text domain: ' . |
85
|
6 |
|
$config['gettext_text_domain'] |
86
|
6 |
|
); |
87
|
|
|
|
88
|
|
|
// Gettext locale |
89
|
6 |
|
$IsSetLocale = setlocale( |
90
|
6 |
|
LC_ALL, |
91
|
6 |
|
$config['gettext_locale'] |
92
|
6 |
|
); |
93
|
|
|
|
94
|
6 |
|
log_message( |
95
|
6 |
|
(is_string($IsSetLocale) ? 'info' : 'error'), |
96
|
|
|
'Gettext Library -> Try to set locale: ' . |
97
|
6 |
|
(is_array($config['gettext_locale']) ? |
98
|
6 |
|
print_r($config['gettext_locale'], TRUE) : $config['gettext_locale'] |
99
|
|
|
) |
100
|
6 |
|
); |
101
|
|
|
|
102
|
|
|
// Change environment language for CLI |
103
|
6 |
|
$IsPutEnv = putenv('LANGUAGE=' . (is_array($config['gettext_locale']) ? $config['gettext_locale'][0] : $config['gettext_locale'])); |
104
|
|
|
|
105
|
6 |
|
log_message( |
106
|
6 |
|
($IsPutEnv === TRUE ? 'info' : 'error'), |
107
|
|
|
'Gettext Library -> Try to set environment language: ' . |
108
|
6 |
|
(is_array($config['gettext_locale']) ? |
109
|
6 |
|
$config['gettext_locale'][0] : $config['gettext_locale'] |
110
|
|
|
) |
111
|
6 |
|
); |
112
|
|
|
|
113
|
|
|
// MO file exists for language |
114
|
6 |
|
$file = APPPATH . $config['gettext_locale_dir'] . |
115
|
6 |
|
'/' . $IsSetLocale . |
116
|
6 |
|
'/LC_MESSAGES/' . |
117
|
6 |
|
$config['gettext_text_domain'] . '.mo' |
118
|
6 |
|
; |
119
|
6 |
|
$IsFileExists = is_file($file); |
120
|
|
|
|
121
|
6 |
|
log_message( |
122
|
6 |
|
($IsFileExists === TRUE ? 'info' : 'error'), |
123
|
|
|
'Gettext Library -> Try to check MO file exists: ' . |
124
|
|
|
$file |
125
|
6 |
|
); |
126
|
6 |
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/* End of file Gettext.php */ |
130
|
|
|
/* Location: ./libraries/config/gettext.php */ |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.