-
+ 1D961FD068388D07B853A391FAB13199321A07AB89AB11DDC1A02A98D9518CF14BF7708B60CF8615974DB3549B6253F8D3798A60D5AE9E24FEE66AE47C286B02
mp-wp/wp-includes/l10n.php
(0 . 0)(1 . 343)
134546 <?php
134547 /**
134548 * WordPress Translation API
134549 *
134550 * @package WordPress
134551 * @subpackage i18n
134552 */
134553
134554 /**
134555 * Gets the current locale.
134556 *
134557 * If the locale is set, then it will filter the locale in the 'locale' filter
134558 * hook and return the value.
134559 *
134560 * If the locale is not set already, then the WPLANG constant is used if it is
134561 * defined. Then it is filtered through the 'locale' filter hook and the value
134562 * for the locale global set and the locale is returned.
134563 *
134564 * The process to get the locale should only be done once but the locale will
134565 * always be filtered using the 'locale' hook.
134566 *
134567 * @since 1.5.0
134568 * @uses apply_filters() Calls 'locale' hook on locale value.
134569 * @uses $locale Gets the locale stored in the global.
134570 *
134571 * @return string The locale of the blog or from the 'locale' hook.
134572 */
134573 function get_locale() {
134574 global $locale;
134575
134576 if (isset($locale))
134577 return apply_filters( 'locale', $locale );
134578
134579 // WPLANG is defined in wp-config.
134580 if (defined('WPLANG'))
134581 $locale = WPLANG;
134582
134583 if (empty($locale))
134584 $locale = 'en_US';
134585
134586 $locale = apply_filters('locale', $locale);
134587
134588 return $locale;
134589 }
134590
134591 /**
134592 * Retrieve the translated text.
134593 *
134594 * If the domain is set in the $l10n global, then the text is run through the
134595 * domain's translate method. After it is passed to the 'gettext' filter hook,
134596 * along with the untranslated text as the second parameter.
134597 *
134598 * If the domain is not set, the $text is just returned.
134599 *
134600 * @since 2.2.0
134601 * @uses $l10n Gets list of domain translated string (gettext_reader) objects.
134602 * @uses apply_filters() Calls 'gettext' on domain translated text
134603 * with the untranslated text as second parameter.
134604 *
134605 * @param string $text Text to translate.
134606 * @param string $domain Domain to retrieve the translated text.
134607 * @return string Translated text
134608 */
134609 function translate($text, $domain = 'default') {
134610 global $l10n;
134611
134612 if (isset($l10n[$domain]))
134613 return apply_filters('gettext', $l10n[$domain]->translate($text), $text, $domain);
134614 else
134615 return apply_filters('gettext', $text, $text, $domain);
134616 }
134617
134618 function before_last_bar( $string ) {
134619 $last_bar = strrpos( $string, '|' );
134620 if ( false == $last_bar )
134621 return $string;
134622 else
134623 return substr( $string, 0, $last_bar );
134624 }
134625
134626 /**
134627 * Retrieve the translated text and strip context.
134628 *
134629 * If the domain is set in the $l10n global, then the text is run through the
134630 * domain's translate method. After it is passed to the 'gettext' filter hook,
134631 * along with the untranslated text as the second parameter.
134632 *
134633 * If the domain is not set, the $text is just returned.
134634 *
134635 * @since 2.5
134636 * @uses translate()
134637 *
134638 * @param string $text Text to translate
134639 * @param string $domain Domain to retrieve the translated text
134640 * @return string Translated text
134641 */
134642 function translate_with_context( $text, $domain = 'default' ) {
134643 return before_last_bar( translate( $text, $domain ) );
134644
134645 }
134646
134647 /**
134648 * Retrieves the translated string from the translate().
134649 *
134650 * @see translate() An alias of translate()
134651 * @since 2.1.0
134652 *
134653 * @param string $text Text to translate
134654 * @param string $domain Optional. Domain to retrieve the translated text
134655 * @return string Translated text
134656 */
134657 function __($text, $domain = 'default') {
134658 return translate($text, $domain);
134659 }
134660
134661 /**
134662 * Displays the returned translated text from translate().
134663 *
134664 * @see translate() Echos returned translate() string
134665 * @since 1.2.0
134666 *
134667 * @param string $text Text to translate
134668 * @param string $domain Optional. Domain to retrieve the translated text
134669 */
134670 function _e($text, $domain = 'default') {
134671 echo translate($text, $domain);
134672 }
134673
134674 /**
134675 * Retrieve context translated string.
134676 *
134677 * Quite a few times, there will be collisions with similar translatable text
134678 * found in more than two places but with different translated context.
134679 *
134680 * In order to use the separate contexts, the _c() function is used and the
134681 * translatable string uses a pipe ('|') which has the context the string is in.
134682 *
134683 * When the translated string is returned, it is everything before the pipe, not
134684 * including the pipe character. If there is no pipe in the translated text then
134685 * everything is returned.
134686 *
134687 * @since 2.2.0
134688 *
134689 * @param string $text Text to translate
134690 * @param string $domain Optional. Domain to retrieve the translated text
134691 * @return string Translated context string without pipe
134692 */
134693 function _c($text, $domain = 'default') {
134694 return translate_with_context($text, $domain);
134695 }
134696
134697 /**
134698 * Retrieve the plural or single form based on the amount.
134699 *
134700 * If the domain is not set in the $l10n list, then a comparsion will be made
134701 * and either $plural or $single parameters returned.
134702 *
134703 * If the domain does exist, then the parameters $single, $plural, and $number
134704 * will first be passed to the domain's ngettext method. Then it will be passed
134705 * to the 'ngettext' filter hook along with the same parameters. The expected
134706 * type will be a string.
134707 *
134708 * @since 1.2.0
134709 * @uses $l10n Gets list of domain translated string (gettext_reader) objects
134710 * @uses apply_filters() Calls 'ngettext' hook on domains text returned,
134711 * along with $single, $plural, and $number parameters. Expected to return string.
134712 *
134713 * @param string $single The text that will be used if $number is 1
134714 * @param string $plural The text that will be used if $number is not 1
134715 * @param int $number The number to compare against to use either $single or $plural
134716 * @param string $domain Optional. The domain identifier the text should be retrieved in
134717 * @return string Either $single or $plural translated text
134718 */
134719 function __ngettext($single, $plural, $number, $domain = 'default') {
134720 global $l10n;
134721
134722 if (isset($l10n[$domain])) {
134723 return apply_filters('ngettext', $l10n[$domain]->ngettext($single, $plural, $number), $single, $plural, $number);
134724 } else {
134725 if ($number != 1)
134726 return $plural;
134727 else
134728 return $single;
134729 }
134730 }
134731
134732 /**
134733 * @see __ngettext() An alias of __ngettext
134734 *
134735 */
134736 function _n() {
134737 $args = func_get_args();
134738 return call_user_func_array('__ngettext', $args);
134739 }
134740
134741 /**
134742 * @see _n() A version of _n(), which supports contexts --
134743 * strips everything from the translation after the last bar
134744 *
134745 */
134746 function _nc( $single, $plural, $number, $domain = 'default' ) {
134747 return before_last_bar( __ngettext( $single, $plural, $number, $domain ) );
134748 }
134749
134750 /**
134751 * Register plural strings in POT file, but don't translate them.
134752 *
134753 * Used when you want do keep structures with translatable plural strings and
134754 * use them later.
134755 *
134756 * Example:
134757 * $messages = array(
134758 * 'post' => ngettext_noop('%s post', '%s posts'),
134759 * 'page' => ngettext_noop('%s pages', '%s pages')
134760 * );
134761 * ...
134762 * $message = $messages[$type];
134763 * $usable_text = sprintf(__ngettext($message[0], $message[1], $count), $count);
134764 *
134765 * @since 2.5
134766 * @param $single Single form to be i18ned
134767 * @param $plural Plural form to be i18ned
134768 * @param $number Not used, here for compatibility with __ngettext, optional
134769 * @param $domain Not used, here for compatibility with __ngettext, optional
134770 * @return array array($single, $plural)
134771 */
134772 function __ngettext_noop($single, $plural, $number=1, $domain = 'default') {
134773 return array($single, $plural);
134774 }
134775
134776 /**
134777 * @see __ngettext_noop() An alias of __ngettext_noop()
134778 *
134779 */
134780 function _n_noop() {
134781 $args = func_get_args();
134782 return call_user_func_array('__ngettext_noop', $args);
134783 }
134784
134785 /**
134786 * Loads MO file into the list of domains.
134787 *
134788 * If the domain already exists, the inclusion will fail. If the MO file is not
134789 * readable, the inclusion will fail.
134790 *
134791 * On success, the mofile will be placed in the $l10n global by $domain and will
134792 * be an gettext_reader object.
134793 *
134794 * @since 1.5.0
134795 * @uses $l10n Gets list of domain translated string (gettext_reader) objects
134796 * @uses CacheFileReader Reads the MO file
134797 * @uses gettext_reader Allows for retrieving translated strings
134798 *
134799 * @param string $domain Unique identifier for retrieving translated strings
134800 * @param string $mofile Path to the .mo file
134801 * @return null On failure returns null and also on success returns nothing.
134802 */
134803 function load_textdomain($domain, $mofile) {
134804 global $l10n;
134805
134806 if ( is_readable($mofile))
134807 $input = new CachedFileReader($mofile);
134808 else
134809 return;
134810
134811 $gettext = new gettext_reader($input);
134812
134813 if (isset($l10n[$domain])) {
134814 $l10n[$domain]->load_tables();
134815 $gettext->load_tables();
134816 $l10n[$domain]->cache_translations = array_merge($gettext->cache_translations, $l10n[$domain]->cache_translations);
134817 } else
134818 $l10n[$domain] = $gettext;
134819
134820 unset($input, $gettext);
134821 }
134822
134823 /**
134824 * Loads default translated strings based on locale.
134825 *
134826 * Loads the .mo file in WP_LANG_DIR constant path from WordPress root. The
134827 * translated (.mo) file is named based off of the locale.
134828 *
134829 * @since 1.5.0
134830 */
134831 function load_default_textdomain() {
134832 $locale = get_locale();
134833
134834 $mofile = WP_LANG_DIR . "/$locale.mo";
134835
134836 load_textdomain('default', $mofile);
134837 }
134838
134839 /**
134840 * Loads the plugin's translated strings.
134841 *
134842 * If the path is not given then it will be the root of the plugin directory.
134843 * The .mo file should be named based on the domain with a dash followed by a
134844 * dash, and then the locale exactly.
134845 *
134846 * @since 1.5.0
134847 *
134848 * @param string $domain Unique identifier for retrieving translated strings
134849 * @param string $abs_rel_path Optional. Relative path to ABSPATH of a folder,
134850 * where the .mo file resides. Deprecated, but still functional until 2.7
134851 * @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
134852 */
134853 function load_plugin_textdomain($domain, $abs_rel_path = false, $plugin_rel_path = false) {
134854 $locale = get_locale();
134855
134856 if ( false !== $plugin_rel_path )
134857 $path = WP_PLUGIN_DIR . '/' . trim( $plugin_rel_path, '/');
134858 else if ( false !== $abs_rel_path)
134859 $path = ABSPATH . trim( $abs_rel_path, '/');
134860 else
134861 $path = WP_PLUGIN_DIR;
134862
134863 $mofile = $path . '/'. $domain . '-' . $locale . '.mo';
134864 load_textdomain($domain, $mofile);
134865 }
134866
134867 /**
134868 * Loads the theme's translated strings.
134869 *
134870 * If the current locale exists as a .mo file in the theme's root directory, it
134871 * will be included in the translated strings by the $domain.
134872 *
134873 * The .mo files must be named based on the locale exactly.
134874 *
134875 * @since 1.5.0
134876 *
134877 * @param string $domain Unique identifier for retrieving translated strings
134878 */
134879 function load_theme_textdomain($domain, $path = false) {
134880 $locale = get_locale();
134881
134882 $path = ( empty( $path ) ) ? get_template_directory() : $path;
134883
134884 $mofile = "$path/$locale.mo";
134885 load_textdomain($domain, $mofile);
134886 }
134887
134888 ?>