- Timestamp:
- 2012/11/16 14:34:39 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/version-2_12-multilang/data/class/helper/SC_Helper_Locale.php
r22085 r22099 35 35 class SC_Helper_Locale { 36 36 37 /** 38 * Store the instance of SC_Helper_Locale_Ex. 39 * @var SC_Helper_Locale 40 */ 41 static $_instance = NULL; 42 37 43 public $_translations = array(); 38 39 public $_plural_forms = array();40 44 41 45 /** 42 46 * Return a string which corresponding with message alias. 43 47 * 44 * @param string $string message alias 45 * @param string $lang_code language code 46 * @param integer $device_type_id device type ID 48 * @param string $string message alias 49 * @param array $options options 47 50 * @return string a string corresponding with message alias 48 51 */ 49 function get_locale($string, $lang_code = FALSE, $device_type_id = FALSE) { 50 // Set language code when it is not set. 51 if ($lang_code === FALSE) { 52 if (defined(LANG_CODE)) { 53 $lang_code = LANG_CODE; 52 public static function get_locale($string, &$options) { 53 is_null(SC_Helper_Locale_Ex::$_instance) and SC_Helper_Locale_Ex::$_instance = new SC_Helper_Locale_Ex(); 54 55 // If language code is not specified, use site default. 56 if (empty($options['lang_code'])) { 57 $lang_code = $options['lang_code'] = defined('LANG_CODE') ? LANG_CODE : 'en'; 58 } else { 59 $lang_code = $options['lang_code']; 60 } 61 // If device type ID is not specified, detect the viewing device. 62 if (!isset($options['device_type_id']) || ($options['device_type_id'] !== FALSE && !strlen($options['device_type_id']))) { 63 if (method_exists('SC_Display_Ex', 'detectDevice')) { 64 $device_type_id = SC_Display_Ex::detectDevice(); 54 65 } else { 55 $ lang_code = 'en';66 $device_type_id = FALSE; 56 67 } 68 } else { 69 $device_type_id = $options['device_type_id']; 57 70 } 71 58 72 // Get string list of specified language. 59 73 if ($lang_code == 'en') { 60 74 return $string; 61 75 } else { 62 $translations = $this->get_translations($lang_code, $device_type_id);76 $translations = SC_Helper_Locale_Ex::$_instance->get_translations($lang_code, $device_type_id); 63 77 // Whether a string which corresponding with alias is exist. 64 78 if (isset($translations[$string])) { … … 69 83 } 70 84 } 85 } 86 87 /** 88 * Return a string which corresponding with message alias. 89 * 90 * @param string $single message alias (single) 91 * @param string $plural message alias (plural) 92 * @param array $options options 93 * @return array 94 */ 95 public static function get_locale_plural($single, $plural, &$options) { 96 // Plural strings are coupled with a null character. 97 $key = $single . chr(0) . $plural; 98 // Get a string of specified language which corresponds to the message alias. 99 $translated = SC_Helper_Locale_Ex::get_locale($key, $options); 100 // Divide with a null character. 101 return explode(chr(0), $translated); 71 102 } 72 103 … … 148 179 return $file_list; 149 180 } 150 151 /**152 * Determine appropriate plural form.153 *154 * @param integer $count counter155 * @param string $lang_code language code156 * @return integer index157 */158 function get_plural_index($count, $lang_code = FALSE) {159 // Set language code when it is not set.160 if ($lang_code === FALSE) {161 if (defined(LANG_CODE)) {162 $lang_code = LANG_CODE;163 } else {164 $lang_code = 'en';165 }166 }167 // Get a formula168 $string = $this->get_plural_forms($lang_code);169 $string = str_replace('nplurals', "\$total", $string);170 $string = str_replace("n", $count, $string);171 $string = str_replace('plural', "\$plural", $string);172 173 $total = 0;174 $plural = 0;175 176 eval("$string");177 if ($plural >= $total) $plural = $total - 1;178 179 return $plural;180 }181 182 /**183 * Get a formula to determine appropriate plural form.184 *185 * @param string $lang_code language code186 * @return string formula187 */188 function get_plural_forms($lang_code) {189 // If formula is empty, include the file.190 if(empty($this->_plural_forms)){191 $this->_plural_forms = @include_once DATA_REALDIR . "include/plural_forms.inc";192 }193 194 return $this->_plural_forms[$lang_code];195 }196 181 }
Note: See TracChangeset
for help on using the changeset viewer.
