Changeset 10584 for trunk/wp-includes/l10n.php
- Timestamp:
- 02/17/2009 05:03:29 AM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/l10n.php
r9887 r10584 45 45 46 46 /** 47 * Retrieve the translated text. 48 * 49 * If the domain is set in the $l10n global, then the text is run through the 50 * domain's translate method. After it is passed to the 'gettext' filter hook, 51 * along with the untranslated text as the second parameter. 52 * 53 * If the domain is not set, the $text is just returned. 54 * 47 * Retrieves the translation of $text. If there is no translation, or 48 * the domain isn't loaded the original text is returned. 49 * 50 * @see __() Don't use translate() directly, use __() 55 51 * @since 2.2.0 56 * @uses $l10n Gets list of domain translated string (gettext_reader) objects.57 52 * @uses apply_filters() Calls 'gettext' on domain translated text 58 53 * with the untranslated text as second parameter. … … 62 57 * @return string Translated text 63 58 */ 64 function translate($text, $domain = 'default') { 65 global $l10n; 66 67 if (isset($l10n[$domain])) 68 return apply_filters('gettext', $l10n[$domain]->translate($text), $text, $domain); 69 else 70 return apply_filters('gettext', $text, $text, $domain); 59 function translate( $text, $domain = 'default' ) { 60 $translations = &get_translations_for_domain( $domain ); 61 return apply_filters('gettext', $translations->translate($text), $text, $domain); 71 62 } 72 63 … … 80 71 81 72 /** 82 * Retrieve the translated text and strip context. 83 * 84 * If the domain is set in the $l10n global, then the text is run through the 85 * domain's translate method. After it is passed to the 'gettext' filter hook, 86 * along with the untranslated text as the second parameter. 87 * 88 * If the domain is not set, the $text is just returned. 73 * Translate $text like translate(), but assumes that the text 74 * contains a context after its last vertical bar. 89 75 * 90 76 * @since 2.5 … … 100 86 } 101 87 102 /** 103 * Retrieves the translated string from the translate(). 88 function translate_with_gettext_context( $text, $context, $domain = 'default' ) { 89 $translations = &get_translations_for_domain( $domain ); 90 return apply_filters( 'gettext_with_context', $translations->translate( $text, $context ), $text, $context, $domain); 91 } 92 93 /** 94 * Retrieves the translation of $text. If there is no translation, or 95 * the domain isn't loaded the original text is returned. 104 96 * 105 97 * @see translate() An alias of translate() … … 110 102 * @return string Translated text 111 103 */ 112 function __( $text, $domain = 'default') {113 return translate( $text, $domain);104 function __( $text, $domain = 'default' ) { 105 return translate( $text, $domain ); 114 106 } 115 107 … … 123 115 * @param string $domain Optional. Domain to retrieve the translated text 124 116 */ 125 function _e( $text, $domain = 'default') {126 echo translate( $text, $domain);127 } 128 129 /** 130 * Retrieve context translated string.117 function _e( $text, $domain = 'default' ) { 118 echo translate( $text, $domain ); 119 } 120 121 /** 122 * Retrieve translated string with vertical bar context 131 123 * 132 124 * Quite a few times, there will be collisions with similar translatable text … … 148 140 function _c($text, $domain = 'default') { 149 141 return translate_with_context($text, $domain); 142 } 143 144 function _x( $single, $context, $domain = 'default' ) { 145 return translate_with_gettext_context( $single, $context, $domain ); 146 } 147 148 function __ngettext() { 149 _deprecated_function( __FUNCTION__, '2.8', '_n()' ); 150 $args = func_get_args(); 151 return call_user_func_array('_n', $args); 150 152 } 151 153 … … 172 174 * @return string Either $single or $plural translated text 173 175 */ 174 function __ngettext($single, $plural, $number, $domain = 'default') { 175 global $l10n; 176 177 if (isset($l10n[$domain])) { 178 return apply_filters('ngettext', $l10n[$domain]->ngettext($single, $plural, $number), $single, $plural, $number); 179 } else { 180 if ($number != 1) 181 return $plural; 182 else 183 return $single; 184 } 185 } 186 187 /** 188 * @see __ngettext() An alias of __ngettext 189 * 190 */ 191 function _n() { 192 $args = func_get_args(); 193 return call_user_func_array('__ngettext', $args); 176 function _n($single, $plural, $number, $domain = 'default') { 177 $translations = &get_translations_for_domain( $domain ); 178 $translation = $translations->translate_plural( $single, $plural, $number ); 179 return apply_filters( 'ngettext', $translation, $single, $plural, $number ); 194 180 } 195 181 … … 200 186 */ 201 187 function _nc( $single, $plural, $number, $domain = 'default' ) { 202 return before_last_bar( __ngettext( $single, $plural, $number, $domain ) ); 188 return before_last_bar( _n( $single, $plural, $number, $domain ) ); 189 } 190 191 function _nx($single, $plural, $number, $context, $domain = 'default') { 192 $translations = &get_translations_for_domain( $domain ); 193 $translation = $translations->translate_plural( $single, $plural, $number, $context ); 194 return apply_filters( 'ngettext_with_context ', $translation, $single, $plural, $number, $context ); 195 } 196 197 /** 198 * @deprecated Use _n_noop() 199 */ 200 function __ngettext_noop() { 201 _deprecated_function( __FUNCTION__, '2.8', '_n_noop()' ); 202 $args = func_get_args(); 203 return call_user_func_array('_n_noop', $args); 204 203 205 } 204 206 … … 211 213 * Example: 212 214 * $messages = array( 213 * 'post' => ngettext_noop('%s post', '%s posts'),214 * 'page' => ngettext_noop('%s pages', '%s pages')215 * 'post' => _n_noop('%s post', '%s posts'), 216 * 'page' => _n_noop('%s pages', '%s pages') 215 217 * ); 216 218 * ... 217 219 * $message = $messages[$type]; 218 * $usable_text = sprintf(_ _ngettext($message[0], $message[1], $count), $count);220 * $usable_text = sprintf(_n($message[0], $message[1], $count), $count); 219 221 * 220 222 * @since 2.5 221 223 * @param $single Single form to be i18ned 222 224 * @param $plural Plural form to be i18ned 223 * @param $number Not used, here for compatibility with _ _ngettext, optional224 * @param $domain Not used, here for compatibility with _ _ngettext, optional225 * @param $number Not used, here for compatibility with _n, optional 226 * @param $domain Not used, here for compatibility with _n, optional 225 227 * @return array array($single, $plural) 226 228 */ 227 function __ngettext_noop($single, $plural, $number=1, $domain = 'default') { 228 return array($single, $plural); 229 } 230 231 /** 232 * @see __ngettext_noop() An alias of __ngettext_noop() 233 * 234 */ 235 function _n_noop() { 236 $args = func_get_args(); 237 return call_user_func_array('__ngettext_noop', $args); 229 function _n_noop( $single, $plural, $number = 1, $domain = 'default' ) { 230 return array( $single, $plural ); 238 231 } 239 232 … … 259 252 global $l10n; 260 253 261 if ( is_readable($mofile)) 262 $input = new CachedFileReader($mofile); 263 else 264 return; 265 266 $gettext = new gettext_reader($input); 267 268 if (isset($l10n[$domain])) { 269 $l10n[$domain]->load_tables(); 270 $gettext->load_tables(); 271 $l10n[$domain]->cache_translations = array_merge($gettext->cache_translations, $l10n[$domain]->cache_translations); 272 } else 273 $l10n[$domain] = $gettext; 274 275 unset($input, $gettext); 254 if ( !is_readable($mofile)) return; 255 256 $mo = new MO(); 257 $mo->import_from_file( $mofile ); 258 259 if (isset($l10n[$domain])) 260 $mo->merge_with( $l10n[$domain] ); 261 262 $l10n[$domain] = &$mo; 276 263 } 277 264 … … 341 328 } 342 329 330 /** 331 * Returns the Translations instance for a domain. If there isn't one, 332 * returns empty Translations instance. 333 * 334 * @param string $domain 335 * @return object A Translation instance 336 */ 337 function get_translations_for_domain( $domain ) { 338 global $l10n; 339 $empty = &new Translations; 340 return isset($l10n[$domain])? $l10n[$domain] : $empty; 341 } 342 343 343 ?>
Note: See TracChangeset
for help on using the changeset viewer.