Ticket #9112: new-gettext-lib.diff
File new-gettext-lib.diff, 8.9 KB (added by , 16 years ago) |
---|
-
wp-includes/l10n.php
44 44 } 45 45 46 46 /** 47 * Retrieve the translated text. 47 * Retrieves the translation of $text. If there is no translation, or 48 * the domain isn't loaded the original text is returned. 48 49 * 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 * 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. 59 54 * … … 61 56 * @param string $domain Domain to retrieve the translated text. 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 73 64 function before_last_bar( $string ) { … … 79 70 } 80 71 81 72 /** 82 * Retrieve the translated text and strip context. 73 * Translate $text like translate(), but assumes that the text 74 * contains a context after its last vertical bar. 83 75 * 84 * If the domain is set in the $l10n global, then the text is run through the85 * 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.89 *90 76 * @since 2.5 91 77 * @uses translate() 92 78 * … … 99 85 100 86 } 101 87 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 102 93 /** 103 * Retrieves the translated string from the translate(). 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() 106 98 * @since 2.1.0 … … 109 101 * @param string $domain Optional. Domain to retrieve the translated text 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 116 108 /** … … 122 114 * @param string $text Text to translate 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);117 function _e( $text, $domain = 'default' ) { 118 echo translate( $text, $domain ); 127 119 } 128 120 129 121 /** 130 * Retrieve context translated string.122 * Retrieve translated string with vertical bar context 131 123 * 132 124 * Quite a few times, there will be collisions with similar translatable text 133 125 * found in more than two places but with different translated context. … … 149 141 return translate_with_context($text, $domain); 150 142 } 151 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); 152 } 153 152 154 /** 153 155 * Retrieve the plural or single form based on the amount. 154 156 * … … 171 173 * @param string $domain Optional. The domain identifier the text should be retrieved in 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 } 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 ); 185 180 } 186 181 187 182 /** 188 * @see __ngettext() An alias of __ngettext 183 * @see _n() A version of _n(), which supports contexts -- 184 * strips everything from the translation after the last bar 189 185 * 190 186 */ 191 function _n() { 192 $args = func_get_args(); 193 return call_user_func_array('__ngettext', $args); 187 function _nc( $single, $plural, $number, $domain = 'default' ) { 188 return before_last_bar( _n( $single, $plural, $number, $domain ) ); 194 189 } 195 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 196 197 /** 197 * @see _n() A version of _n(), which supports contexts -- 198 * strips everything from the translation after the last bar 199 * 198 * @deprecated Use _n_noop() 200 199 */ 201 function _nc( $single, $plural, $number, $domain = 'default' ) { 202 return before_last_bar( __ngettext( $single, $plural, $number, $domain ) ); 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 205 207 /** … … 210 212 * 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 function _n_noop( $single, $plural, $number = 1, $domain = 'default' ) { 230 return array( $single, $plural ); 229 231 } 230 232 231 233 /** 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);238 }239 240 /**241 234 * Loads MO file into the list of domains. 242 235 * 243 236 * If the domain already exists, the inclusion will fail. If the MO file is not … … 258 251 function load_textdomain($domain, $mofile) { 259 252 global $l10n; 260 253 261 if ( is_readable($mofile))262 $input = new CachedFileReader($mofile);263 else264 return;254 if ( !is_readable($mofile)) return; 255 256 $mo = new MO(); 257 $mo->import_from_file( $mofile ); 265 258 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); 259 if (isset($l10n[$domain])) 260 $mo->merge_with( $l10n[$domain] ); 261 262 $l10n[$domain] = &$mo; 276 263 } 277 264 278 265 /** … … 340 327 load_textdomain($domain, $mofile); 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 ?> -
wp-settings.php
273 273 274 274 require (ABSPATH . WPINC . '/plugin.php'); 275 275 require (ABSPATH . WPINC . '/default-filters.php'); 276 include_once(ABSPATH . WPINC . '/streams.php'); 277 include_once(ABSPATH . WPINC . '/gettext.php'); 276 include_once(ABSPATH . WPINC . '/pomo/mo.php'); 278 277 require_once (ABSPATH . WPINC . '/l10n.php'); 279 278 280 279 if ( !is_blog_installed() && (strpos($_SERVER['PHP_SELF'], 'install.php') === false && !defined('WP_INSTALLING')) ) {