WordPress 3.0 beta 1 documentation kindly provided to you by Hay Kranen
| [ Index ] |
PHP Cross Reference of WordPress 3.0 beta 1 |
|
| [ Index ] [ Variables ] [ Functions ] [ Classes ] [ Constants ] [ Statistics ] | ||
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * WordPress Translation API 4 * 5 * @package WordPress 6 * @subpackage i18n 7 */ 8 9 /** 10 * Gets the current locale. 11 * 12 * If the locale is set, then it will filter the locale in the 'locale' filter 13 * hook and return the value. 14 * 15 * If the locale is not set already, then the WPLANG constant is used if it is 16 * defined. Then it is filtered through the 'locale' filter hook and the value 17 * for the locale global set and the locale is returned. 18 * 19 * The process to get the locale should only be done once but the locale will 20 * always be filtered using the 'locale' hook. 21 * 22 * @since 1.5.0 23 * @uses apply_filters() Calls 'locale' hook on locale value. 24 * @uses $locale Gets the locale stored in the global. 25 * 26 * @return string The locale of the blog or from the 'locale' hook. 27 */ 28 function get_locale() { 29 global $locale; 30 31 if ( isset( $locale ) ) 32 return apply_filters( 'locale', $locale ); 33 34 // WPLANG is defined in wp-config. 35 if ( defined( 'WPLANG' ) ) 36 $locale = WPLANG; 37 38 // If multisite, check options. 39 if ( is_multisite() && !defined('WP_INSTALLING') ) { 40 $ms_locale = get_option('WPLANG'); 41 if ( $ms_locale === false ) 42 $ms_locale = get_site_option('WPLANG'); 43 44 if ( $ms_locale !== false ) 45 $locale = $ms_locale; 46 } 47 48 if ( empty( $locale ) ) 49 $locale = 'en_US'; 50 51 return apply_filters( 'locale', $locale ); 52 } 53 54 /** 55 * Retrieves the translation of $text. If there is no translation, or 56 * the domain isn't loaded the original text is returned. 57 * 58 * @see __() Don't use translate() directly, use __() 59 * @since 2.2.0 60 * @uses apply_filters() Calls 'gettext' on domain translated text 61 * with the untranslated text as second parameter. 62 * 63 * @param string $text Text to translate. 64 * @param string $domain Domain to retrieve the translated text. 65 * @return string Translated text 66 */ 67 function translate( $text, $domain = 'default' ) { 68 $translations = &get_translations_for_domain( $domain ); 69 return apply_filters( 'gettext', $translations->translate( $text ), $text, $domain ); 70 } 71 72 function before_last_bar( $string ) { 73 $last_bar = strrpos( $string, '|' ); 74 if ( false == $last_bar ) 75 return $string; 76 else 77 return substr( $string, 0, $last_bar ); 78 } 79 80 function translate_with_gettext_context( $text, $context, $domain = 'default' ) { 81 $translations = &get_translations_for_domain( $domain ); 82 return apply_filters( 'gettext_with_context', $translations->translate( $text, $context ), $text, $context, $domain ); 83 } 84 85 /** 86 * Retrieves the translation of $text. If there is no translation, or 87 * the domain isn't loaded the original text is returned. 88 * 89 * @see translate() An alias of translate() 90 * @since 2.1.0 91 * 92 * @param string $text Text to translate 93 * @param string $domain Optional. Domain to retrieve the translated text 94 * @return string Translated text 95 */ 96 function __( $text, $domain = 'default' ) { 97 return translate( $text, $domain ); 98 } 99 100 /** 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. 103 * 104 * @see translate() An alias of translate() 105 * @see esc_attr() 106 * @since 2.8.0 107 * 108 * @param string $text Text to translate 109 * @param string $domain Optional. Domain to retrieve the translated text 110 * @return string Translated text 111 */ 112 function esc_attr__( $text, $domain = 'default' ) { 113 return esc_attr( translate( $text, $domain ) ); 114 } 115 116 /** 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. 119 * 120 * @see translate() An alias of translate() 121 * @see esc_html() 122 * @since 2.8.0 123 * 124 * @param string $text Text to translate 125 * @param string $domain Optional. Domain to retrieve the translated text 126 * @return string Translated text 127 */ 128 function esc_html__( $text, $domain = 'default' ) { 129 return esc_html( translate( $text, $domain ) ); 130 } 131 132 /** 133 * Displays the returned translated text from translate(). 134 * 135 * @see translate() Echoes returned translate() string 136 * @since 1.2.0 137 * 138 * @param string $text Text to translate 139 * @param string $domain Optional. Domain to retrieve the translated text 140 */ 141 function _e( $text, $domain = 'default' ) { 142 echo translate( $text, $domain ); 143 } 144 145 /** 146 * Displays translated text that has been escaped for safe use in an attribute. 147 * 148 * @see translate() Echoes returned translate() string 149 * @see esc_attr() 150 * @since 2.8.0 151 * 152 * @param string $text Text to translate 153 * @param string $domain Optional. Domain to retrieve the translated text 154 */ 155 function esc_attr_e( $text, $domain = 'default' ) { 156 echo esc_attr( translate( $text, $domain ) ); 157 } 158 159 /** 160 * Displays translated text that has been escaped for safe use in HTML output. 161 * 162 * @see translate() Echoes returned translate() string 163 * @see esc_html() 164 * @since 2.8.0 165 * 166 * @param string $text Text to translate 167 * @param string $domain Optional. Domain to retrieve the translated text 168 */ 169 function esc_html_e( $text, $domain = 'default' ) { 170 echo esc_html( translate( $text, $domain ) ); 171 } 172 173 /** 174 * Retrieve translated string with gettext context 175 * 176 * Quite a few times, there will be collisions with similar translatable text 177 * found in more than two places but with different translated context. 178 * 179 * By including the context in the pot file translators can translate the two 180 * string differently. 181 * 182 * @since 2.8.0 183 * 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 188 */ 189 function _x( $single, $context, $domain = 'default' ) { 190 return translate_with_gettext_context( $single, $context, $domain ); 191 } 192 193 function esc_attr_x( $single, $context, $domain = 'default' ) { 194 return esc_attr( translate_with_gettext_context( $single, $context, $domain ) ); 195 } 196 197 function esc_html_x( $single, $context, $domain = 'default' ) { 198 return esc_html( translate_with_gettext_context( $single, $context, $domain ) ); 199 } 200 201 /** 202 * Retrieve the plural or single form based on the amount. 203 * 204 * If the domain is not set in the $l10n list, then a comparison will be made 205 * and either $plural or $single parameters returned. 206 * 207 * If the domain does exist, then the parameters $single, $plural, and $number 208 * will first be passed to the domain's ngettext method. Then it will be passed 209 * to the 'ngettext' filter hook along with the same parameters. The expected 210 * type will be a string. 211 * 212 * @since 2.8.0 213 * @uses $l10n Gets list of domain translated string (gettext_reader) objects 214 * @uses apply_filters() Calls 'ngettext' hook on domains text returned, 215 * along with $single, $plural, and $number parameters. Expected to return string. 216 * 217 * @param string $single The text that will be used if $number is 1 218 * @param string $plural The text that will be used if $number is not 1 219 * @param int $number The number to compare against to use either $single or $plural 220 * @param string $domain Optional. The domain identifier the text should be retrieved in 221 * @return string Either $single or $plural translated text 222 */ 223 function _n( $single, $plural, $number, $domain = 'default' ) { 224 $translations = &get_translations_for_domain( $domain ); 225 $translation = $translations->translate_plural( $single, $plural, $number ); 226 return apply_filters( 'ngettext', $translation, $single, $plural, $number, $domain ); 227 } 228 229 /** 230 * A hybrid of _n() and _x(). It supports contexts and plurals. 231 * 232 * @see _n() 233 * @see _x() 234 * 235 */ 236 function _nx($single, $plural, $number, $context, $domain = 'default') { 237 $translations = &get_translations_for_domain( $domain ); 238 $translation = $translations->translate_plural( $single, $plural, $number, $context ); 239 return apply_filters( 'ngettext_with_context', $translation, $single, $plural, $number, $context, $domain ); 240 } 241 242 /** 243 * Register plural strings in POT file, but don't translate them. 244 * 245 * Used when you want do keep structures with translatable plural strings and 246 * use them later. 247 * 248 * Example: 249 * $messages = array( 250 * 'post' => _n_noop('%s post', '%s posts'), 251 * 'page' => _n_noop('%s pages', '%s pages') 252 * ); 253 * ... 254 * $message = $messages[$type]; 255 * $usable_text = sprintf(_n($message[0], $message[1], $count), $count); 256 * 257 * @since 2.5 258 * @param $single Single form to be i18ned 259 * @param $plural Plural form to be i18ned 260 * @return array array($single, $plural) 261 */ 262 function _n_noop( $single, $plural ) { 263 return array( $single, $plural ); 264 } 265 266 /** 267 * Register plural strings with context in POT file, but don't translate them. 268 * 269 * @see _n_noop() 270 */ 271 function _nx_noop( $single, $plural, $context ) { 272 return array( $single, $plural, $context ); 273 } 274 275 /** 276 * Loads a MO file into the domain $domain. 277 * 278 * If the domain already exists, the translations will be merged. If both 279 * sets have the same string, the translation from the original value will be taken. 280 * 281 * On success, the .mo file will be placed in the $l10n global by $domain 282 * and will be a MO object. 283 * 284 * @since 1.5.0 285 * @uses $l10n Gets list of domain translated string objects 286 * 287 * @param string $domain Unique identifier for retrieving translated strings 288 * @param string $mofile Path to the .mo file 289 * @return bool true on success, false on failure 290 */ 291 function load_textdomain( $domain, $mofile ) { 292 global $l10n; 293 294 $plugin_override = apply_filters( 'override_load_textdomain', false, $domain, $mofile ); 295 296 if ( true == $plugin_override ) { 297 return true; 298 } 299 300 do_action( 'load_textdomain', $domain, $mofile ); 301 302 $mofile = apply_filters( 'load_textdomain_mofile', $mofile, $domain ); 303 304 if ( !is_readable( $mofile ) ) return false; 305 306 $mo = new MO(); 307 if ( !$mo->import_from_file( $mofile ) ) return false; 308 309 if ( isset( $l10n[$domain] ) ) 310 $mo->merge_with( $l10n[$domain] ); 311 312 $l10n[$domain] = &$mo; 313 314 return true; 315 } 316 317 /** 318 * Unloads translations for a domain 319 * 320 * @since 3.0.0 321 * @param string $domain Textdomain to be unloaded 322 * @return bool Whether textdomain was unloaded 323 */ 324 function unload_textdomain( $domain ) { 325 global $l10n; 326 327 $plugin_override = apply_filters( 'override_unload_textdomain', false, $domain ); 328 329 if ( $plugin_override ) 330 return true; 331 332 do_action( 'unload_textdomain', $domain ); 333 334 if ( isset( $l10n[$domain] ) ) { 335 unset( $l10n[$domain] ); 336 return true; 337 } 338 339 return false; 340 } 341 342 /** 343 * Loads default translated strings based on locale. 344 * 345 * Loads the .mo file in WP_LANG_DIR constant path from WordPress root. The 346 * translated (.mo) file is named based off of the locale. 347 * 348 * @since 1.5.0 349 */ 350 function load_default_textdomain() { 351 $locale = get_locale(); 352 353 $mofile = WP_LANG_DIR . "/$locale.mo"; 354 355 return load_textdomain( 'default', $mofile ); 356 } 357 358 /** 359 * Loads the plugin's translated strings. 360 * 361 * If the path is not given then it will be the root of the plugin directory. 362 * The .mo file should be named based on the domain with a dash, and then the locale exactly. 363 * 364 * @since 1.5.0 365 * 366 * @param string $domain Unique identifier for retrieving translated strings 367 * @param string $abs_rel_path Optional. Relative path to ABSPATH of a folder, 368 * where the .mo file resides. Deprecated, but still functional until 2.7 369 * @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 370 */ 371 function load_plugin_textdomain( $domain, $abs_rel_path = false, $plugin_rel_path = false ) { 372 $locale = apply_filters( 'plugin_locale', get_locale(), $domain ); 373 374 if ( false !== $plugin_rel_path ) { 375 $path = WP_PLUGIN_DIR . '/' . trim( $plugin_rel_path, '/' ); 376 } else if ( false !== $abs_rel_path ) { 377 _deprecated_argument( __FUNCTION__, '2.7' ); 378 $path = ABSPATH . trim( $abs_rel_path, '/' ); 379 } else { 380 $path = WP_PLUGIN_DIR; 381 } 382 383 $mofile = $path . '/'. $domain . '-' . $locale . '.mo'; 384 return load_textdomain( $domain, $mofile ); 385 } 386 387 /** 388 * Load the translated strings for a plugin residing in the mu-plugins dir. 389 * 390 * @since 3.0.0 391 * 392 * @param string $domain Unique identifier for retrieving translated strings 393 */ 394 function load_muplugin_textdomain( $domain, $path = false ) { 395 $locale = apply_filters( 'plugin_locale', get_locale(), $domain ); 396 397 $mofile = WPMU_PLUGIN_DIR . "/$domain-$locale.mo"; 398 load_textdomain( $domain, $mofile ); 399 } 400 401 /** 402 * Loads the theme's translated strings. 403 * 404 * If the current locale exists as a .mo file in the theme's root directory, it 405 * will be included in the translated strings by the $domain. 406 * 407 * The .mo files must be named based on the locale exactly. 408 * 409 * @since 1.5.0 410 * 411 * @param string $domain Unique identifier for retrieving translated strings 412 */ 413 function load_theme_textdomain( $domain, $path = false ) { 414 $locale = apply_filters( 'theme_locale', get_locale(), $domain ); 415 416 $path = ( empty( $path ) ) ? get_template_directory() : $path; 417 418 $mofile = "$path/$locale.mo"; 419 return load_textdomain($domain, $mofile); 420 } 421 422 /** 423 * Loads the child themes translated strings. 424 * 425 * If the current locale exists as a .mo file in the child themes root directory, it 426 * will be included in the translated strings by the $domain. 427 * 428 * The .mo files must be named based on the locale exactly. 429 * 430 * @since 2.9.0 431 * 432 * @param string $domain Unique identifier for retrieving translated strings 433 */ 434 function load_child_theme_textdomain( $domain, $path = false ) { 435 $locale = apply_filters( 'theme_locale', get_locale(), $domain ); 436 437 $path = ( empty( $path ) ) ? get_stylesheet_directory() : $path; 438 439 $mofile = "$path/$locale.mo"; 440 return load_textdomain($domain, $mofile); 441 } 442 443 /** 444 * Returns the Translations instance for a domain. If there isn't one, 445 * returns empty Translations instance. 446 * 447 * @param string $domain 448 * @return object A Translation instance 449 */ 450 function &get_translations_for_domain( $domain ) { 451 global $l10n; 452 if ( !isset( $l10n[$domain] ) ) { 453 $l10n[$domain] = &new NOOP_Translations; 454 } 455 return $l10n[$domain]; 456 } 457 458 /** 459 * Whether there are translations for the domain 460 * 461 * @since 3.0.0 462 * @param string $domain 463 * @return bool Whether there are translations 464 */ 465 function is_textdomain_loaded( $domain ) { 466 global $l10n; 467 return isset( $l10n[$domain] ); 468 } 469 470 /** 471 * Translates role name. Since the role names are in the database and 472 * not in the source there are dummy gettext calls to get them into the POT 473 * file and this function properly translates them back. 474 * 475 * The before_last_bar() call is needed, because older installs keep the roles 476 * using the old context format: 'Role name|User role' and just skipping the 477 * content after the last bar is easier than fixing them in the DB. New installs 478 * won't suffer from that problem. 479 */ 480 function translate_user_role( $name ) { 481 return translate_with_gettext_context( before_last_bar($name), 'User role' ); 482 } 483 484 /** 485 * Get all available languages based on the presence of *.mo files in a given directory. The default directory is WP_LANG_DIR. 486 * 487 * @since 3.0.0 488 * 489 * @param string $dir A directory in which to search for language files. The default directory is WP_LANG_DIR. 490 * @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. 491 */ 492 function get_available_languages( $dir = null ) { 493 $languages = array(); 494 495 if ( empty($dir) ) 496 $dir = WP_LANG_DIR; 497 498 if ( is_dir( $dir ) && $dh = opendir( $dir ) ) { 499 while ( ( $lang_file = readdir( $dh ) ) !== false ) { 500 if ( substr( $lang_file, -3 ) == '.mo' && ( false === strpos( $lang_file, 'continents-cities' ) ) ) 501 $languages[] = basename($lang_file, '.mo'); 502 } 503 closedir($dh); 504 } 505 506 return $languages; 507 } 508 509 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Mon Apr 5 14:26:09 2010 | Cross-referenced by PHPXref 0.7 |