home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Blogs / wordpress2.6.exe / wordpress2.6 / wp-includes / l10n.php < prev    next >
Encoding:
PHP Script  |  2008-08-06  |  9.8 KB  |  326 lines

  1. <?php
  2. /**
  3.  * WordPress Translation API
  4.  *
  5.  * @package WordPress
  6.  * @subpackage i18n
  7.  */
  8.  
  9. /**
  10.  * get_locale() - Gets the current locale
  11.  *
  12.  * If the locale is set, then it will filter the locale
  13.  * in the 'locale' filter hook and return the value.
  14.  *
  15.  * If the locale is not set already, then the WPLANG
  16.  * constant is used if it is defined. Then it is filtered
  17.  * through the 'locale' filter hook and the value for the
  18.  * locale global set and the locale is returned.
  19.  *
  20.  * The process to get the locale should only be done once
  21.  * but the locale will always be filtered using the
  22.  * 'locale' hook.
  23.  *
  24.  * @since 1.5.0
  25.  * @uses apply_filters() Calls 'locale' hook on locale value
  26.  * @uses $locale Gets the locale stored in the global
  27.  *
  28.  * @return string The locale of the blog or from the 'locale' hook
  29.  */
  30. function get_locale() {
  31.     global $locale;
  32.  
  33.     if (isset($locale))
  34.         return apply_filters( 'locale', $locale );
  35.  
  36.     // WPLANG is defined in wp-config.
  37.     if (defined('WPLANG'))
  38.         $locale = WPLANG;
  39.  
  40.     if (empty($locale))
  41.         $locale = 'en_US';
  42.  
  43.     $locale = apply_filters('locale', $locale);
  44.  
  45.     return $locale;
  46. }
  47.  
  48. /**
  49.  * translate() - Retrieve the translated text
  50.  *
  51.  * If the domain is set in the $l10n global, then the text is run
  52.  * through the domain's translate method. After it is passed to
  53.  * the 'gettext' filter hook, along with the untranslated text as
  54.  * the second parameter.
  55.  *
  56.  * If the domain is not set, the $text is just returned.
  57.  *
  58.  * @since 2.2.0
  59.  * @uses $l10n Gets list of domain translated string (gettext_reader) objects
  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.     global $l10n;
  69.  
  70.     if (isset($l10n[$domain]))
  71.         return apply_filters('gettext', $l10n[$domain]->translate($text), $text);
  72.     else
  73.         return $text;
  74. }
  75.  
  76. /**
  77.  * translate_with_context() - Retrieve the translated text and strip context
  78.  *
  79.  * If the domain is set in the $l10n global, then the text is run
  80.  * through the domain's translate method. After it is passed to
  81.  * the 'gettext' filter hook, along with the untranslated text as
  82.  * the second parameter.
  83.  *
  84.  * If the domain is not set, the $text is just returned.
  85.  *
  86.  * @since 2.5
  87.  * @uses translate()
  88.  *
  89.  * @param string $text Text to translate
  90.  * @param string $domain Domain to retrieve the translated text
  91.  * @return string Translated text
  92.  */
  93. function translate_with_context($text, $domain = 'default') {
  94.     $whole = translate($text, $domain);
  95.     $last_bar = strrpos($whole, '|');
  96.     if ( false == $last_bar ) {
  97.         return $whole;
  98.     } else {
  99.         return substr($whole, 0, $last_bar);
  100.     }
  101. }
  102.  
  103. /**
  104.  * __() - Retrieve a translated string
  105.  *
  106.  * __() is a convenience function which retrieves the translated
  107.  * string from the translate().
  108.  *
  109.  * @see translate() An alias of translate()
  110.  * @since 2.1.0
  111.  *
  112.  * @param string $text Text to translate
  113.  * @param string $domain Optional. Domain to retrieve the translated text
  114.  * @return string Translated text
  115.  */
  116. function __($text, $domain = 'default') {
  117.     return translate($text, $domain);
  118. }
  119.  
  120. // .
  121. /**
  122.  * _e() - Display a translated string
  123.  *
  124.  * _e() is a convenience function which displays the returned
  125.  * translated text from translate().
  126.  *
  127.  * @see translate() Echos returned translate() string
  128.  * @since 1.2.0
  129.  *
  130.  * @param string $text Text to translate
  131.  * @param string $domain Optional. Domain to retrieve the translated text
  132.  */
  133. function _e($text, $domain = 'default') {
  134.     echo translate($text, $domain);
  135. }
  136.  
  137. /**
  138.  * _c() - Retrieve context translated string
  139.  *
  140.  * Quite a few times, there will be collisions with similar
  141.  * translatable text found in more than two places but with
  142.  * different translated context.
  143.  *
  144.  * In order to use the separate contexts, the _c() function
  145.  * is used and the translatable string uses a pipe ('|')
  146.  * which has the context the string is in.
  147.  *
  148.  * When the translated string is returned, it is everything
  149.  * before the pipe, not including the pipe character. If
  150.  * there is no pipe in the translated text then everything
  151.  * is returned.
  152.  *
  153.  * @since 2.2.0
  154.  *
  155.  * @param string $text Text to translate
  156.  * @param string $domain Optional. Domain to retrieve the translated text
  157.  * @return string Translated context string without pipe
  158.  */
  159. function _c($text, $domain = 'default') {
  160.     return translate_with_context($text, $domain);
  161. }
  162.  
  163. /**
  164.  * __ngettext() - Retrieve the plural or single form based on the amount
  165.  *
  166.  * If the domain is not set in the $l10n list, then a comparsion
  167.  * will be made and either $plural or $single parameters returned.
  168.  *
  169.  * If the domain does exist, then the parameters $single, $plural,
  170.  * and $number will first be passed to the domain's ngettext method.
  171.  * Then it will be passed to the 'ngettext' filter hook along with
  172.  * the same parameters. The expected type will be a string.
  173.  *
  174.  * @since 1.2.0
  175.  * @uses $l10n Gets list of domain translated string (gettext_reader) objects
  176.  * @uses apply_filters() Calls 'ngettext' hook on domains text returned,
  177.  *        along with $single, $plural, and $number parameters. Expected to return string.
  178.  *
  179.  * @param string $single The text that will be used if $number is 1
  180.  * @param string $plural The text that will be used if $number is not 1
  181.  * @param int $number The number to compare against to use either $single or $plural
  182.  * @param string $domain Optional. The domain identifier the text should be retrieved in
  183.  * @return string Either $single or $plural translated text
  184.  */
  185. function __ngettext($single, $plural, $number, $domain = 'default') {
  186.     global $l10n;
  187.  
  188.     if (isset($l10n[$domain])) {
  189.         return apply_filters('ngettext', $l10n[$domain]->ngettext($single, $plural, $number), $single, $plural, $number);
  190.     } else {
  191.         if ($number != 1)
  192.             return $plural;
  193.         else
  194.             return $single;
  195.     }
  196. }
  197.  
  198. /**
  199.  * __ngettext_noop() - register plural strings in POT file, but don't translate them
  200.  *
  201.  * Used when you want do keep structures with translatable plural strings and
  202.  * use them later.
  203.  *
  204.  * Example:
  205.  *  $messages = array(
  206.  *      'post' => ngettext_noop('%s post', '%s posts'),
  207.  *      'page' => ngettext_noop('%s pages', '%s pages')
  208.  *  );
  209.  *  ...
  210.  *  $message = $messages[$type];
  211.  *  $usable_text = sprintf(__ngettext($message[0], $message[1], $count), $count);
  212.  *
  213.  * @since 2.5
  214.  * @param $single Single form to be i18ned
  215.  * @param $plural Plural form to be i18ned
  216.  * @param $number Not used, here for compatibility with __ngettext, optional
  217.  * @param $domain Not used, here for compatibility with __ngettext, optional
  218.  * @return array array($single, $plural)
  219.  */
  220. function __ngettext_noop($single, $plural, $number=1, $domain = 'default') {
  221.     return array($single, $plural);
  222. }
  223.  
  224. /**
  225.  * load_textdomain() - Loads MO file into the list of domains
  226.  *
  227.  * If the domain already exists, the inclusion will fail. If the
  228.  * MO file is not readable, the inclusion will fail.
  229.  *
  230.  * On success, the mofile will be placed in the $l10n global by
  231.  * $domain and will be an gettext_reader object.
  232.  *
  233.  * @since 1.5.0
  234.  * @uses $l10n Gets list of domain translated string (gettext_reader) objects
  235.  * @uses CacheFileReader Reads the MO file
  236.  * @uses gettext_reader Allows for retrieving translated strings
  237.  *
  238.  * @param string $domain Unique identifier for retrieving translated strings
  239.  * @param string $mofile Path to the .mo file
  240.  * @return null On failure returns null and also on success returns nothing.
  241.  */
  242. function load_textdomain($domain, $mofile) {
  243.     global $l10n;
  244.  
  245.     if ( is_readable($mofile))
  246.         $input = new CachedFileReader($mofile);
  247.     else
  248.         return;
  249.  
  250.     $gettext = new gettext_reader($input);
  251.  
  252.     if (isset($l10n[$domain])) {
  253.         $l10n[$domain]->load_tables();
  254.         $gettext->load_tables();
  255.         $l10n[$domain]->cache_translations = array_merge($gettext->cache_translations, $l10n[$domain]->cache_translations);
  256.     } else
  257.         $l10n[$domain] = $gettext;
  258.  
  259.     unset($input, $gettext);
  260. }
  261.  
  262. /**
  263.  * load_default_textdomain() - Loads default translated strings based on locale
  264.  *
  265.  * Loads the .mo file in WP_LANG_DIR constant path from WordPress root.
  266.  * The translated (.mo) file is named based off of the locale.
  267.  *
  268.  * @since 1.5.0
  269.  */
  270. function load_default_textdomain() {
  271.     $locale = get_locale();
  272.  
  273.     $mofile = WP_LANG_DIR . "/$locale.mo";
  274.  
  275.     load_textdomain('default', $mofile);
  276. }
  277.  
  278. /**
  279.  * load_plugin_textdomain() - Loads the plugin's translated strings
  280.  *
  281.  * If the path is not given then it will be the root of the plugin
  282.  * directory. The .mo file should be named based on the domain with a
  283.  * dash followed by a dash, and then the locale exactly.
  284.  *
  285.  * @since 1.5.0
  286.  *
  287.  * @param string $domain Unique identifier for retrieving translated strings
  288.  * @param string $abs_rel_path Optional. Relative path to ABSPATH of a folder,
  289.  *     where the .mo file resides. Deprecated, but still functional until 2.7
  290.  * @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
  291.  */
  292. function load_plugin_textdomain($domain, $abs_rel_path = false, $plugin_rel_path = false) {
  293.     $locale = get_locale();
  294.     
  295.     if ( false !== $plugin_rel_path    )
  296.         $path = WP_PLUGIN_DIR . '/' . trim( $plugin_rel_path, '/');
  297.     else if ( false !== $abs_rel_path)
  298.         $path = ABSPATH . trim( $abs_rel_path, '/');
  299.     else
  300.         $path = WP_PLUGIN_DIR;
  301.  
  302.     $mofile = $path . '/'. $domain . '-' . $locale . '.mo';
  303.     load_textdomain($domain, $mofile);
  304. }
  305.  
  306. /**
  307.  * load_theme_textdomain() - Includes theme's translated strings for the theme
  308.  *
  309.  * If the current locale exists as a .mo file in the theme's root directory, it
  310.  * will be included in the translated strings by the $domain.
  311.  *
  312.  * The .mo files must be named based on the locale exactly.
  313.  *
  314.  * @since 1.5.0
  315.  *
  316.  * @param string $domain Unique identifier for retrieving translated strings
  317.  */
  318. function load_theme_textdomain($domain) {
  319.     $locale = get_locale();
  320.  
  321.     $mofile = get_template_directory() . "/$locale.mo";
  322.     load_textdomain($domain, $mofile);
  323. }
  324.  
  325. ?>
  326.