1
|
|
|
<?php |
|
|
|
|
2
|
|
|
// @codeCoverageIgnoreStart |
3
|
|
|
if (!defined('PHPUNIT_TESTING')) defined('BASEPATH') OR 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
|
|
|
$CI = &get_instance(); |
27
|
|
|
|
28
|
|
|
// Merge $config and config/gettext.php $config |
29
|
|
|
$config = array_merge( |
30
|
|
|
array( |
31
|
|
|
'gettext_locale_dir' => $CI->config->item('gettext_locale_dir'), |
32
|
|
|
'gettext_text_domain' => $CI->config->item('gettext_text_domain'), |
33
|
|
|
'gettext_catalog_codeset' => $CI->config->item('gettext_catalog_codeset'), |
34
|
|
|
'gettext_locale' => $CI->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
|
|
|
static public function init(array $config) |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
// Gettext catalog codeset |
50
|
|
|
$IsBindTextDomainCodeset = bind_textdomain_codeset( |
51
|
|
|
$config['gettext_text_domain'], |
52
|
|
|
$config['gettext_catalog_codeset'] |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
log_message( |
56
|
|
|
'info', |
57
|
|
|
'Try to bind gettext catalog_codeset: ' . $config['gettext_catalog_codeset'] . " - " . |
58
|
|
|
($IsBindTextDomainCodeset ? 'Successful' : '*** FAILED ***') |
59
|
|
|
); |
60
|
|
|
|
61
|
|
|
// Path to gettext locales directory relative to FCPATH.APPPATH |
62
|
|
|
$IsBindTextDomain = bindtextdomain( |
63
|
|
|
$config['gettext_text_domain'], |
64
|
|
|
APPPATH . $config['gettext_locale_dir'] |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
log_message( |
68
|
|
|
'info', |
69
|
|
|
'Try to bind gettext text domain (locale dir): ' . (empty($IsBindTextDomain) ? $IsBindTextDomain : APPPATH . $config['gettext_locale_dir']) . " - " . |
70
|
|
|
(isset($IsBindTextDomain) ? 'Successful' : '*** FAILED ***') |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
// Gettext domain |
74
|
|
|
$IsSetTextDomain = textdomain( |
75
|
|
|
$config['gettext_text_domain'] |
76
|
|
|
); |
77
|
|
|
|
78
|
|
|
log_message( |
79
|
|
|
'info', |
80
|
|
|
'Try to set gettext text_domain: ' . $config['gettext_text_domain'] . " - " . |
81
|
|
|
($IsSetTextDomain ? 'Successful' : '*** FAILED ***') |
82
|
|
|
); |
83
|
|
|
|
84
|
|
|
// Gettext locale |
85
|
|
|
$IsSetLocale = setlocale( |
86
|
|
|
LC_ALL, |
87
|
|
|
$config['gettext_locale'] |
88
|
|
|
); |
89
|
|
|
|
90
|
|
|
log_message( |
91
|
|
|
'info', |
92
|
|
|
'Try to set gettext locale: ' . (is_array($config['gettext_locale']) ? print_r($config['gettext_locale'], TRUE) : $config['gettext_locale']) . " - " . |
93
|
|
|
($IsSetLocale ? 'Successful' : '*** FAILED ***') |
94
|
|
|
); |
95
|
|
|
|
96
|
|
|
// Change environment language for CLI |
97
|
|
|
$IsPutEnv = putenv('LANGUAGE=' . (is_array($config['gettext_locale']) ? $config['gettext_locale'][0] : $config['gettext_locale'])); |
98
|
|
|
|
99
|
|
|
log_message( |
100
|
|
|
'info', |
101
|
|
|
'Try to set Environment LANGUAGE: ' . (is_array($config['gettext_locale']) ? $config['gettext_locale'][0] : $config['gettext_locale']) . " - " . |
102
|
|
|
($IsPutEnv ? 'Successful' : '*** FAILED ***') |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/* End of file 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.