Ticket #16750: l10n.diff
| File l10n.diff, 20.3 KB (added by , 15 years ago) |
|---|
-
l10n.php
52 52 } 53 53 54 54 /** 55 * Retrieves the translation of $text. If there is no translation, or 56 * the domainisn't loaded the original text is returned.55 * Retrieves the translation of $text. If there is no translation, or the domain 56 * isn't loaded the original text is returned. 57 57 * 58 * @see __() Don't use translate() directly, use __() 58 * @see __() Don't use translate() directly, use __(). 59 * 59 60 * @since 2.2.0 60 * @uses apply_filters() Calls 'gettext' on domain translated text61 * with the untranslated text as second parameter.62 61 * 62 * @uses apply_filters() Calls 'gettext' on domain translated text with the 63 * untranslated text as second parameter. 64 * 63 65 * @param string $text Text to translate. 64 * @param string $domain Domain to retrieve the translated text. 65 * @return string Translated text 66 * @param string $domain Optional. Unique identifier for retrieving translated 67 * strings. 68 * @return string Translated text. 66 69 */ 67 70 function translate( $text, $domain = 'default' ) { 68 71 $translations = &get_translations_for_domain( $domain ); 69 72 return apply_filters( 'gettext', $translations->translate( $text ), $text, $domain ); 70 73 } 71 74 75 /** 76 * Removes last item on a pipe delimited string. 77 * 78 * Meant for removing the last item in a string like: 'Role name|User role'. It 79 * will return* the original string if no pipe '|' characters are found in 80 * the string. 81 * 82 * @since 2.7.0 83 * 84 * @param string $strimg A pipe delimited string. 85 * @return string Either $string or everything before last pipe. 86 */ 72 87 function before_last_bar( $string ) { 73 88 $last_bar = strrpos( $string, '|' ); 74 89 if ( false == $last_bar ) … … 77 92 return substr( $string, 0, $last_bar ); 78 93 } 79 94 95 /** 96 * Retrieves the translation of $text in the context defined in $context. If 97 * there is no translation, or the domain isn't loaded the original text is 98 * returned. 99 * 100 * @since 2.8.0 101 * @see translate() 102 * @see _x() 103 * 104 * @param string $text Text to translate. 105 * @param string $context Context information for the translators. 106 * @param string $domain Optional. Unique identifier for retrieving translated 107 * strings. 108 * @return string Translated text. 109 */ 80 110 function translate_with_gettext_context( $text, $context, $domain = 'default' ) { 81 111 $translations = &get_translations_for_domain( $domain ); 82 112 return apply_filters( 'gettext_with_context', $translations->translate( $text, $context ), $text, $context, $domain ); … … 86 116 * Retrieves the translation of $text. If there is no translation, or 87 117 * the domain isn't loaded the original text is returned. 88 118 * 89 * @see translate() An alias of translate() 119 * @see translate() An alias of translate(). 90 120 * @since 2.1.0 91 121 * 92 * @param string $text Text to translate 93 * @param string $domain Optional. Domain to retrieve the translated text 94 * @return string Translated text 122 * @param string $text Text to translate. 123 * @param string $domain Optional. Unique identifier for retrieving translated 124 * strings. 125 * @return string Translated text. 95 126 */ 96 127 function __( $text, $domain = 'default' ) { 97 128 return translate( $text, $domain ); 98 129 } 99 130 100 131 /** 101 * Retrieves the translation of $text and escapes it for safe use in an attribute. 102 * If there is no translation, or the domain isn't loaded the original text is returned. 132 * Retrieves the translation of $text and escapes it for safe use in an 133 * attribute. If there is no translation, or the domain isn't loaded the 134 * original text is returned. 103 135 * 104 * @see translate() An alias of translate() 136 * @see translate() An alias of translate(). 105 137 * @see esc_attr() 106 138 * @since 2.8.0 107 139 * 108 * @param string $text Text to translate 109 * @param string $domain Optional. Domain to retrieve the translated text 110 * @return string Translated text 140 * @param string $text Text to translate. 141 * @param string $domain Optional. Unique identifier for retrieving translated 142 * strings. 143 * @return string Translated text. 111 144 */ 112 145 function esc_attr__( $text, $domain = 'default' ) { 113 146 return esc_attr( translate( $text, $domain ) ); 114 147 } 115 148 116 149 /** 117 * Retrieves the translation of $text and escapes it for safe use in HTML output. 118 * If there is no translation, or the domain isn't loaded the original text is returned. 150 * Retrieves the translation of $text and escapes it for safe use in HTML 151 * output. If there is no translation, or the domain isn't loaded the original 152 * text is returned. 119 153 * 120 * @see translate() An alias of translate() 154 * @see translate() An alias of translate(). 121 155 * @see esc_html() 122 156 * @since 2.8.0 123 157 * 124 158 * @param string $text Text to translate 125 * @param string $domain Optional. Domain to retrieve the translated text 159 * @param string $domain Optional. Unique identifier for retrieving translated 160 * strings. 126 161 * @return string Translated text 127 162 */ 128 163 function esc_html__( $text, $domain = 'default' ) { … … 132 167 /** 133 168 * Displays the returned translated text from translate(). 134 169 * 135 * @see translate() Echoes returned translate() string 170 * @see translate() Echoes returned translate() string. 136 171 * @since 1.2.0 137 172 * 138 * @param string $text Text to translate 139 * @param string $domain Optional. Domain to retrieve the translated text 173 * @param string $text Text to translate. 174 * @param string $domain Optional. Unique identifier for retrieving translated 175 * strings. 140 176 */ 141 177 function _e( $text, $domain = 'default' ) { 142 178 echo translate( $text, $domain ); … … 145 181 /** 146 182 * Displays translated text that has been escaped for safe use in an attribute. 147 183 * 148 * @see translate() Echoes returned translate() string 184 * @see translate() Echoes returned translate() string. 149 185 * @see esc_attr() 150 186 * @since 2.8.0 151 187 * 152 * @param string $text Text to translate 153 * @param string $domain Optional. Domain to retrieve the translated text 188 * @param string $text Text to translate. 189 * @param string $domain Optional. Unique identifier for retrieving translated 190 * strings. 154 191 */ 155 192 function esc_attr_e( $text, $domain = 'default' ) { 156 193 echo esc_attr( translate( $text, $domain ) ); … … 159 196 /** 160 197 * Displays translated text that has been escaped for safe use in HTML output. 161 198 * 162 * @see translate() Echoes returned translate() string 199 * @see translate() Echoes returned translate() string. 163 200 * @see esc_html() 164 201 * @since 2.8.0 165 202 * 166 * @param string $text Text to translate 167 * @param string $domain Optional. Domain to retrieve the translated text 203 * @param string $text Text to translate. 204 * @param string $domain Optional. Unique identifier for retrieving translated 205 * strings. 168 206 */ 169 207 function esc_html_e( $text, $domain = 'default' ) { 170 208 echo esc_html( translate( $text, $domain ) ); 171 209 } 172 210 173 211 /** 174 * Retrieve translated string with gettext context 212 * Retrieve translated string with gettext context. 175 213 * 176 214 * Quite a few times, there will be collisions with similar translatable text 177 215 * found in more than two places but with different translated context. … … 181 219 * 182 220 * @since 2.8.0 183 221 * 184 * @param string $text Text to translate 185 * @param string $context Context information for the translators 186 * @param string $domain Optional. Domain to retrieve the translated text 187 * @return string Translated context string without pipe 222 * @param string $text Text to translate. 223 * @param string $context Context information for the translators. 224 * @param string $domain Optional. Unique identifier for retrieving translated 225 * strings. 226 * @return string Translated text. 188 227 */ 189 228 function _x( $single, $context, $domain = 'default' ) { 190 229 return translate_with_gettext_context( $single, $context, $domain ); 191 230 } 192 231 193 232 /** 194 * Displays translated string with gettext context 233 * Displays translated string with gettext context. 195 234 * 196 235 * @see _x 197 236 * @since 3.0.0 198 237 * 199 * @param string $text Text to translate 200 * @param string $context Context information for the translators 201 * @param string $domain Optional. Domain to retrieve the translated text 202 * @return string Translated context string without pipe 238 * @param string $single Text to translate. 239 * @param string $context Context information for the translators. 240 * @param string $domain Optional. Unique identifier for retrieving translated 241 * strings. 242 * @return string Translated text. 203 243 */ 204 244 function _ex( $single, $context, $domain = 'default' ) { 205 245 echo _x( $single, $context, $domain ); 206 246 } 207 247 248 /** 249 * Displays translated string with gettext context after escaping for HTML. 250 * 251 * @see translate() An alias of translate(). 252 * @see esc_attr() 253 * 254 * @since 3.0.0 255 * 256 * @param string $single Text to translate. 257 * @param string $context Context information for the translators. 258 * @param string $domain Optional. Unique identifier for retrieving translated 259 * strings. 260 * @return string Translated text. 261 */ 208 262 function esc_attr_x( $single, $context, $domain = 'default' ) { 209 263 return esc_attr( translate_with_gettext_context( $single, $context, $domain ) ); 210 264 } 211 265 266 /** 267 * Displays translated string with gettext context after escaping for HTML 268 * attributes. 269 * 270 * @see translate() An alias of translate(). 271 * @see esc_html() 272 * 273 * @since 3.0.0 274 * 275 * @param string $single Text to translate. 276 * @param string $context Context information for the translators. 277 * @param string $domain Optional. Unique identifier for retrieving translated 278 * strings. 279 * @return string Translated text. 280 */ 212 281 function esc_html_x( $single, $context, $domain = 'default' ) { 213 282 return esc_html( translate_with_gettext_context( $single, $context, $domain ) ); 214 283 } … … 226 295 * 227 296 * @since 2.8.0 228 297 * @uses $l10n Gets list of domain translated string (gettext_reader) objects 229 * @uses apply_filters() Calls 'ngettext' hook on domains text returned, 230 * alongwith $single, $plural, and $number parameters. Expected to return string.298 * @uses apply_filters() Calls 'ngettext' hook on domains text returned, along 299 * with $single, $plural, and $number parameters. Expected to return string. 231 300 * 232 * @param string $single The text that will be used if $number is 1 233 * @param string $plural The text that will be used if $number is not 1 234 * @param int $number The number to compare against to use either $single or $plural 235 * @param string $domain Optional. The domain identifier the text should be retrieved in 301 * @param string $single The text that will be used if $number is 1. 302 * @param string $plural The text that will be used if $number is not 1. 303 * @param int $number 1 for $single, plural otherwise. 304 * @param string $domain Optional. Unique identifier for retrieving translated 305 * strings. 236 306 * @return string Either $single or $plural translated text 237 307 */ 238 308 function _n( $single, $plural, $number, $domain = 'default' ) { … … 247 317 * @see _n() 248 318 * @see _x() 249 319 * 320 * @since 2.8.0 321 * 322 * @param string $single The text that will be used if $number is 1. 323 * @param string $plural The text that will be used if $number is not 1. 324 * @param int $number 1 for $single, plural otherwise. 325 * @param string $context Context information for the translators. 326 * @param string $domain Optional. Unique identifier for retrieving translated 327 * strings. 328 * @return string Either $single or $plural translated text. 250 329 */ 251 330 function _nx($single, $plural, $number, $context, $domain = 'default') { 252 331 $translations = &get_translations_for_domain( $domain ); … … 269 348 * $message = $messages[$type]; 270 349 * $usable_text = sprintf( translate_nooped_plural( $message, $count ), $count ); 271 350 * 351 * @see _nx_noop() 272 352 * @since 2.5 273 * @param string $single Single form to be i18ned 274 * @param string $plural Plural form to be i18ned 275 * @return array array($single, $plural) 353 * 354 * @param string $singular singular form to be i18ned. 355 * @param string $plural Plural form to be i18ned. 356 * @return array array( $single, $plural ). 276 357 */ 277 358 function _n_noop( $singular, $plural ) { 278 359 return array( 0 => $singular, 1 => $plural, 'singular' => $singular, 'plural' => $plural, 'context' => null ); … … 282 363 * Register plural strings with context in POT file, but don't translate them. 283 364 * 284 365 * @see _n_noop() 366 * @since 2.5 367 * 368 * @param string $singular singular form to be i18ned. 369 * @param string $plural Plural form to be i18ned. 370 * @param string $context Context information for the translators. 371 * @return array array( $single, $plural ). 285 372 */ 286 373 function _nx_noop( $singular, $plural, $context ) { 287 374 return array( 0 => $singular, 1 => $plural, 2 => $context, 'singular' => $singular, 'plural' => $plural, 'context' => $context ); … … 291 378 * Translate the result of _n_noop() or _nx_noop() 292 379 * 293 380 * @since 3.1 294 * @param array $nooped_plural array with singular, plural and context keys, usually the result of _n_noop() or _nx_noop() 295 * @param int $count number of objects 296 * @param string $domain Optional. The domain identifier the text should be retrieved in 381 * 382 * @param array $nooped_plural array with singular, plural and context keys, 383 * usually the result of _n_noop() or _nx_noop(). 384 * @param int $count Number of objects. 385 * @param string $domain Optional. Unique identifier for retrieving translated 386 * strings. 387 * @return string Either $nooped_plural['singular'] or $nooped_plural['plural'] 388 * translated text. 297 389 */ 298 390 function translate_nooped_plural( $nooped_plural, $count, $domain = 'default' ) { 299 391 if ( $nooped_plural['context'] ) … … 305 397 /** 306 398 * Loads a MO file into the domain $domain. 307 399 * 308 * If the domain already exists, the translations will be merged. If both 309 * setshave the same string, the translation from the original value will be taken.400 * If the domain already exists, the translations will be merged. If both sets 401 * have the same string, the translation from the original value will be taken. 310 402 * 311 * On success, the .mo file will be placed in the $l10n global by $domain 312 * andwill be a MO object.403 * On success, the .mo file will be placed in the $l10n global by $domain and 404 * will be a MO object. 313 405 * 314 406 * @since 1.5.0 315 * @uses $l10n Gets list of domain translated string objects 407 * @uses $l10n Gets list of domain translated string objects. 316 408 * 317 * @param string $domain Unique identifier for retrieving translated strings318 * @param string $mofile Path to the .mo file 319 * @return bool true on success, false on failure 409 * @param string $domain Unique identifier for retrieving translated strings. 410 * @param string $mofile Path to the .mo file. 411 * @return bool true on success, false on failure. 320 412 */ 321 413 function load_textdomain( $domain, $mofile ) { 322 414 global $l10n; … … 345 437 } 346 438 347 439 /** 348 * Unloads translations for a domain 440 * Unloads translations for a domain. 349 441 * 350 442 * @since 3.0.0 351 * @param string $domain Textdomain to be unloaded 352 * @return bool Whether textdomain was unloaded 443 * 444 * @param string $domain Text domain to be unloaded. 445 * @return bool Whether text domain was unloaded. 353 446 */ 354 447 function unload_textdomain( $domain ) { 355 448 global $l10n; … … 391 484 * Loads the plugin's translated strings. 392 485 * 393 486 * If the path is not given then it will be the root of the plugin directory. 394 * The .mo file should be named based on the domain with a dash, and then the locale exactly. 487 * The .mo file should be named based on the domain with a dash, and then the 488 * locale exactly. 395 489 * 396 490 * @since 1.5.0 397 491 * 398 * @param string $domain Unique identifier for retrieving translated strings 492 * @param string $domain Unique identifier for retrieving translated strings. 399 493 * @param string $abs_rel_path Optional. Relative path to ABSPATH of a folder, 400 * where the .mo file resides. Deprecated, but still functional until 2.7 401 * @param string $plugin_rel_path Optional. Relative path to WP_PLUGIN_DIR. This is the preferred argument to use. It takes precendence over $abs_rel_path 494 * where the .mo file resides. Deprecated, but still functional until 2.7. 495 * @param string $plugin_rel_path Optional. Relative path to WP_PLUGIN_DIR. 496 * This is the preferred argument to use. It takes precendence over $abs_rel_path. 497 * @return bool true on success, false on failure. 402 498 */ 403 499 function load_plugin_textdomain( $domain, $abs_rel_path = false, $plugin_rel_path = false ) { 404 500 $locale = apply_filters( 'plugin_locale', get_locale(), $domain ); … … 421 517 * 422 518 * @since 3.0.0 423 519 * 424 * @param string $domain Unique identifier for retrieving translated strings 425 * @param strings $mu_plugin_rel_path Relative to WPMU_PLUGIN_DIR directory in which 426 * the MO file resides. Defaults is empty string. 520 * @param string $domain Unique identifier for retrieving translated strings. 521 * @param strings $mu_plugin_rel_path Relative to WPMU_PLUGIN_DIR directory in 522 * which * the MO file resides. Defaults is empty string. 523 * @return bool true on success, false on failure. 427 524 */ 428 525 function load_muplugin_textdomain( $domain, $mu_plugin_rel_path = '' ) { 429 526 $locale = apply_filters( 'plugin_locale', get_locale(), $domain ); … … 441 538 * 442 539 * @since 1.5.0 443 540 * 444 * @param string $domain Unique identifier for retrieving translated strings 541 * @param string $domain Unique identifier for retrieving translated strings. 542 * @return bool true on success, false on failure. 445 543 */ 446 544 function load_theme_textdomain( $domain, $path = false ) { 447 545 $locale = apply_filters( 'theme_locale', get_locale(), $domain ); … … 455 553 /** 456 554 * Loads the child themes translated strings. 457 555 * 458 * If the current locale exists as a .mo file in the child themes root directory, it459 * will be included in the translated strings by the $domain.556 * If the current locale exists as a .mo file in the child themes root 557 * directory, it will be included in the translated strings by the $domain. 460 558 * 461 559 * The .mo files must be named based on the locale exactly. 462 560 * 463 561 * @since 2.9.0 464 562 * 465 * @param string $domain Unique identifier for retrieving translated strings 563 * @param string $domain Unique identifier for retrieving translated strings. 564 * @param string|bool $path Optional. Path to chlid theme directory of false. 565 * @return bool true on success, false on failure. 466 566 */ 467 567 function load_child_theme_textdomain( $domain, $path = false ) { 468 568 $locale = apply_filters( 'theme_locale', get_locale(), $domain ); … … 477 577 * Returns the Translations instance for a domain. If there isn't one, 478 578 * returns empty Translations instance. 479 579 * 480 * @param string $domain 481 * @return object A Translation instance 580 * @see NOOP_Translations class. 581 * 582 * @since 3.0.0 583 * 584 * @param string $domain Unique identifier for retrieving translated strings. 585 * @return object A Translation instance. 482 586 */ 483 587 function &get_translations_for_domain( $domain ) { 484 588 global $l10n; … … 489 593 } 490 594 491 595 /** 492 * Whether there are translations for the domain 596 * Whether there are translations for the domain. 493 597 * 494 598 * @since 3.0.0 495 * @param string $domain 496 * @return bool Whether there are translations 599 * 600 * @param string $domain Unique identifier for retrieving translated strings. 601 * @return bool Whether there are translations. 497 602 */ 498 603 function is_textdomain_loaded( $domain ) { 499 604 global $l10n; … … 509 614 * using the old context format: 'Role name|User role' and just skipping the 510 615 * content after the last bar is easier than fixing them in the DB. New installs 511 616 * won't suffer from that problem. 617 * 618 * @since 2.8.0 619 * 620 * @param string $name A pipe delimited string. 621 * @return string Translated text. 512 622 */ 513 623 function translate_user_role( $name ) { 514 624 return translate_with_gettext_context( before_last_bar($name), 'User role' ); 515 625 } 516 626 517 627 /** 518 * Get all available languages based on the presence of *.mo files in a given directory. The default directory is WP_LANG_DIR. 628 * Get all available languages based on the presence of *.mo files in a given 629 * directory. The default directory is WP_LANG_DIR. 519 630 * 520 631 * @since 3.0.0 521 632 * 522 * @param string $dir A directory in which to search for language files. The default directory is WP_LANG_DIR. 523 * @return array Array of language codes or an empty array if no languages are present. Language codes are formed by stripping the .mo extension from the language file names. 633 * @param string $dir A directory in which to search for language files. The 634 * default directory is WP_LANG_DIR. 635 * @return array Array of language codes or an empty array if no languages are 636 * present. Language codes are formed by stripping the .mo extension from the 637 * language file names. 524 638 */ 525 639 function get_available_languages( $dir = null ) { 526 640 $languages = array(); … … 532 646 } 533 647 534 648 return $languages; 535 } 536 No newline at end of file 649 }