Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 16 | class Gettext |
||
| 17 | { |
||
| 18 | /** @var string domain name match with file contains translation */ |
||
| 19 | private $_textDomain; |
||
| 20 | |||
| 21 | /** @var string character encoding in which the messages are */ |
||
| 22 | private $_catalogCodeSet; |
||
| 23 | |||
| 24 | /** @var string the path for a domain */ |
||
| 25 | private $_localeDir; |
||
| 26 | |||
| 27 | /** @var string|array locale or array of locales */ |
||
| 28 | private $_locale; |
||
| 29 | |||
| 30 | /** @var string constant specifying the category of the functions affected by the locale setting */ |
||
| 31 | private $_category; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Initialize Codeigniter PHP framework and get configuration |
||
| 35 | * |
||
| 36 | * @param array $config Override default configuration |
||
| 37 | */ |
||
| 38 | 108 | public function __construct(array $config = array()) |
|
| 39 | { |
||
| 40 | 108 | log_message('info', 'Gettext Library Class Initialized'); |
|
| 41 | |||
| 42 | 108 | $this->_setConfig($config); |
|
| 43 | |||
| 44 | 81 | $this |
|
| 45 | 108 | ->_bindTextDomainCodeSet() |
|
| 46 | 108 | ->_bindTextDomain() |
|
| 47 | 108 | ->_textDomain() |
|
| 48 | 108 | ->_setLocale() |
|
| 49 | 108 | ->_putEnv() |
|
| 50 | 108 | ->_checkLocaleFile(); |
|
| 51 | 108 | } |
|
| 52 | |||
| 53 | /** |
||
| 54 | * Merge config as parameter and default config (config/gettext.php file) |
||
| 55 | * |
||
| 56 | * @param array $config |
||
| 57 | */ |
||
| 58 | 108 | private function _setConfig(array $config = array()) |
|
| 59 | { |
||
| 60 | 108 | $this->_localeDir = isset($config['gettext_locale_dir']) |
|
| 61 | 108 | ? $config['gettext_locale_dir'] : config_item('gettext_locale_dir'); |
|
| 62 | |||
| 63 | 108 | $this->_textDomain = isset($config['gettext_text_domain']) |
|
| 64 | 108 | ? $config['gettext_text_domain'] : config_item('gettext_text_domain'); |
|
| 65 | |||
| 66 | 108 | $this->_catalogCodeSet = isset($config['gettext_catalog_codeset']) |
|
| 67 | 108 | ? $config['gettext_catalog_codeset'] : config_item('gettext_catalog_codeset'); |
|
| 68 | |||
| 69 | 108 | $this->_locale = isset($config['gettext_locale']) |
|
| 70 | 108 | ? $config['gettext_locale'] : config_item('gettext_locale'); |
|
| 71 | |||
| 72 | 108 | $this->_category = (isset($config['gettext_category']) |
|
| 73 | 108 | ? $config['gettext_category'] : config_item('gettext_category')); |
|
| 74 | 108 | } |
|
| 75 | |||
| 76 | /** |
||
| 77 | * Load a domain |
||
| 78 | * |
||
| 79 | * @param string $domain |
||
| 80 | */ |
||
| 81 | 40 | public function changeDomain($domain) |
|
| 82 | { |
||
| 83 | 40 | log_message('info', 'Gettext Library Class -> Change domain'); |
|
| 84 | |||
| 85 | 40 | $this->_textDomain = $domain; |
|
| 86 | |||
| 87 | 30 | $this |
|
| 88 | 40 | ->_bindTextDomainCodeSet() |
|
| 89 | 40 | ->_bindTextDomain() |
|
| 90 | 40 | ->_textDomain() |
|
| 91 | 40 | ->_checkLocaleFile(); |
|
| 92 | 40 | } |
|
| 93 | |||
| 94 | /** |
||
| 95 | * Switch to a category |
||
| 96 | * |
||
| 97 | * @param string $category |
||
| 98 | */ |
||
| 99 | 4 | public function changeCategory($category) |
|
| 107 | |||
| 108 | /** |
||
| 109 | * Gettext catalog codeset |
||
| 110 | * |
||
| 111 | * @return $this |
||
| 112 | */ |
||
| 113 | 108 | private function _bindTextDomainCodeSet() |
|
| 126 | |||
| 127 | /** |
||
| 128 | * Path to gettext locales directory relative to APPPATH |
||
| 129 | * |
||
| 130 | * @return $this |
||
| 131 | */ |
||
| 132 | 108 | View Code Duplication | private function _bindTextDomain() |
| 145 | |||
| 146 | /** |
||
| 147 | * Gettext domain |
||
| 148 | * |
||
| 149 | * @return $this |
||
| 150 | */ |
||
| 151 | 108 | private function _textDomain() |
|
| 162 | |||
| 163 | /** |
||
| 164 | * Gettext locale |
||
| 165 | * |
||
| 166 | * @return $this |
||
| 167 | */ |
||
| 168 | 108 | private function _setLocale() |
|
| 184 | |||
| 185 | /** |
||
| 186 | * Change environment language for CLI |
||
| 187 | * |
||
| 188 | * @return $this |
||
| 189 | */ |
||
| 190 | 108 | private function _putEnv() |
|
| 212 | |||
| 213 | /** |
||
| 214 | * MO file exists for locale |
||
| 215 | * |
||
| 216 | * @return $this |
||
| 217 | */ |
||
| 218 | 108 | View Code Duplication | private function _checkLocaleFile() |
| 230 | |||
| 231 | } |
||
| 232 | |||
| 235 | /* Location: ./application/libraries/Gettext.php */ |