File indexing completed on 2024-12-22 05:33:09
0001 <?php 0002 0003 /** 0004 * Flooer Framework 0005 * 0006 * LICENSE: BSD License (2 Clause) 0007 * 0008 * @category Flooer 0009 * @package Flooer_Gettext 0010 * @author Akira Ohgaki <akiraohgaki@gmail.com> 0011 * @copyright Akira Ohgaki 0012 * @license https://opensource.org/licenses/BSD-2-Clause BSD License (2 Clause) 0013 * @link https://github.com/akiraohgaki/flooer 0014 */ 0015 0016 /** 0017 * Usage 0018 * 0019 * $gettext = new Flooer_Gettext(); 0020 * $gettext->setBaseDir('./locales'); 0021 * $gettext->setLocale('en_US'); 0022 * $gettext->setup(); 0023 */ 0024 0025 /** 0026 * Gettext and locale information setting class 0027 * 0028 * @category Flooer 0029 * @package Flooer_Gettext 0030 * @author Akira Ohgaki <akiraohgaki@gmail.com> 0031 */ 0032 class Flooer_Gettext 0033 { 0034 0035 /** 0036 * Configuration options 0037 * 0038 * @var array 0039 */ 0040 protected $_config = array( 0041 'baseDir' => null, 0042 'domain' => 'messages', 0043 'encoding' => 'UTF-8', 0044 'category' => LC_ALL, 0045 'locale' => 'C' 0046 ); 0047 0048 /** 0049 * Constructor 0050 * 0051 * @param array $config 0052 * @return void 0053 */ 0054 public function __construct(array $config = null) 0055 { 0056 if ($config) { 0057 $this->_config = $config + $this->_config; 0058 } 0059 } 0060 0061 /** 0062 * Setup for gettext 0063 * 0064 * @return void 0065 */ 0066 public function setup() 0067 { 0068 if ($this->_config['locale'] != 'C' 0069 && $this->_config['baseDir'] 0070 && is_dir($this->_config['baseDir']) 0071 ) { 0072 if (!ini_get('safe_mode')) { 0073 @putenv('LANG=' . $this->_config['locale']); 0074 } 0075 setlocale($this->_config['category'], $this->_config['locale']); 0076 bindtextdomain($this->_config['domain'], $this->_config['baseDir']); 0077 bind_textdomain_codeset($this->_config['domain'], $this->_config['encoding']); 0078 textdomain($this->_config['domain']); 0079 } 0080 } 0081 0082 /** 0083 * Set the path of a base directory 0084 * 0085 * @param string $path 0086 * @return void 0087 */ 0088 public function setBaseDir($path) 0089 { 0090 $this->_config['baseDir'] = $path; 0091 } 0092 0093 /** 0094 * Get the path of a base directory 0095 * 0096 * @return string 0097 */ 0098 public function getBaseDir() 0099 { 0100 return $this->_config['baseDir']; 0101 } 0102 0103 /** 0104 * Set a DOMAIN message catalog 0105 * 0106 * @param string $domain 0107 * @return void 0108 */ 0109 public function setDomain($domain) 0110 { 0111 $this->_config['domain'] = $domain; 0112 } 0113 0114 /** 0115 * Get a DOMAIN message catalog 0116 * 0117 * @return string 0118 */ 0119 public function getDomain() 0120 { 0121 return $this->_config['domain']; 0122 } 0123 0124 /** 0125 * Set a character encoding 0126 * 0127 * @param string $encoding Codeset in a DOMAIN message catalog 0128 * @return void 0129 */ 0130 public function setEncoding($encoding) 0131 { 0132 $this->_config['encoding'] = $encoding; 0133 } 0134 0135 /** 0136 * Get a character encoding 0137 * 0138 * @return string 0139 */ 0140 public function getEncoding() 0141 { 0142 return $this->_config['encoding']; 0143 } 0144 0145 /** 0146 * Set a category for locale information 0147 * 0148 * @param int $category Available values to setlocale() 0149 * @return void 0150 */ 0151 public function setCategory($category) 0152 { 0153 $this->_config['category'] = $category; 0154 } 0155 0156 /** 0157 * Get a category for locale information 0158 * 0159 * @return int 0160 */ 0161 public function getCategory() 0162 { 0163 return $this->_config['category']; 0164 } 0165 0166 /** 0167 * Set locale information 0168 * 0169 * @param string $locale Available values to setlocale() 0170 * @return void 0171 */ 0172 public function setLocale($locale) 0173 { 0174 $this->_config['locale'] = $locale; 0175 } 0176 0177 /** 0178 * Get locale information 0179 * 0180 * @return string 0181 */ 0182 public function getLocale() 0183 { 0184 return $this->_config['locale']; 0185 } 0186 0187 }