Make WordPress Core

Ticket #9112: new-gettext-lib.diff

File new-gettext-lib.diff, 8.9 KB (added by nbachiyski, 16 years ago)
  • wp-includes/l10n.php

     
    4444}
    4545
    4646/**
    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.
    4849 *
    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 __()
    5551 * @since 2.2.0
    56  * @uses $l10n Gets list of domain translated string (gettext_reader) objects.
    5752 * @uses apply_filters() Calls 'gettext' on domain translated text
    5853 *              with the untranslated text as second parameter.
    5954 *
     
    6156 * @param string $domain Domain to retrieve the translated text.
    6257 * @return string Translated text
    6358 */
    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);
     59function translate( $text, $domain = 'default' ) {
     60        $translations = &get_translations_for_domain( $domain );
     61        return apply_filters('gettext', $translations->translate($text), $text, $domain);
    7162}
    7263
    7364function before_last_bar( $string ) {
     
    7970}
    8071
    8172/**
    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.
    8375 *
    84  * If the domain is set in the $l10n global, then the text is run through the
    85  * 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  *
    9076 * @since 2.5
    9177 * @uses translate()
    9278 *
     
    9985
    10086}
    10187
     88function 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
    10293/**
    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.
    10496 *
    10597 * @see translate() An alias of translate()
    10698 * @since 2.1.0
     
    109101 * @param string $domain Optional. Domain to retrieve the translated text
    110102 * @return string Translated text
    111103 */
    112 function __($text, $domain = 'default') {
    113         return translate($text, $domain);
     104function __( $text, $domain = 'default' ) {
     105        return translate( $text, $domain );
    114106}
    115107
    116108/**
     
    122114 * @param string $text Text to translate
    123115 * @param string $domain Optional. Domain to retrieve the translated text
    124116 */
    125 function _e($text, $domain = 'default') {
    126         echo translate($text, $domain);
     117function _e( $text, $domain = 'default' ) {
     118        echo translate( $text, $domain );
    127119}
    128120
    129121/**
    130  * Retrieve context translated string.
     122 * Retrieve translated string with vertical bar context
    131123 *
    132124 * Quite a few times, there will be collisions with similar translatable text
    133125 * found in more than two places but with different translated context.
     
    149141        return translate_with_context($text, $domain);
    150142}
    151143
     144function _x( $single, $context, $domain = 'default' ) {
     145        return translate_with_gettext_context( $single, $context, $domain );
     146}
     147
     148function __ngettext() {
     149        _deprecated_function( __FUNCTION__, '2.8', '_n()' );
     150        $args = func_get_args();
     151        return call_user_func_array('_n', $args);
     152}
     153
    152154/**
    153155 * Retrieve the plural or single form based on the amount.
    154156 *
     
    171173 * @param string $domain Optional. The domain identifier the text should be retrieved in
    172174 * @return string Either $single or $plural translated text
    173175 */
    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         }
     176function _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 );
    185180}
    186181
    187182/**
    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
    189185 *
    190186 */
    191 function _n() {
    192         $args = func_get_args();
    193         return call_user_func_array('__ngettext', $args);
     187function _nc( $single, $plural, $number, $domain = 'default' ) {
     188        return before_last_bar( _n( $single, $plural, $number, $domain ) );
    194189}
    195190
     191function _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
    196197/**
    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()
    200199 */
    201 function _nc( $single, $plural, $number, $domain = 'default' ) {
    202         return before_last_bar( __ngettext( $single, $plural, $number, $domain ) );
     200function __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
    203205}
    204206
    205207/**
     
    210212 *
    211213 * Example:
    212214 *  $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')
    215217 *  );
    216218 *  ...
    217219 *  $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);
    219221 *
    220222 * @since 2.5
    221223 * @param $single Single form to be i18ned
    222224 * @param $plural Plural form to be i18ned
    223  * @param $number Not used, here for compatibility with __ngettext, optional
    224  * @param $domain Not used, here for compatibility with __ngettext, optional
     225 * @param $number Not used, here for compatibility with _n, optional
     226 * @param $domain Not used, here for compatibility with _n, optional
    225227 * @return array array($single, $plural)
    226228 */
    227 function __ngettext_noop($single, $plural, $number=1, $domain = 'default') {
    228         return array($single, $plural);
     229function _n_noop( $single, $plural, $number = 1, $domain = 'default' ) {
     230        return array( $single, $plural );
    229231}
    230232
    231233/**
    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 /**
    241234 * Loads MO file into the list of domains.
    242235 *
    243236 * If the domain already exists, the inclusion will fail. If the MO file is not
     
    258251function load_textdomain($domain, $mofile) {
    259252        global $l10n;
    260253
    261         if ( is_readable($mofile))
    262                 $input = new CachedFileReader($mofile);
    263         else
    264                 return;
     254        if ( !is_readable($mofile)) return;
     255       
     256        $mo = new MO();
     257        $mo->import_from_file( $mofile );
    265258
    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;
    276263}
    277264
    278265/**
     
    340327        load_textdomain($domain, $mofile);
    341328}
    342329
     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 */
     337function get_translations_for_domain( $domain ) {
     338        global $l10n;
     339        $empty = &new Translations;
     340        return isset($l10n[$domain])? $l10n[$domain] : $empty;
     341}
     342
    343343?>
  • wp-settings.php

     
    273273
    274274require (ABSPATH . WPINC . '/plugin.php');
    275275require (ABSPATH . WPINC . '/default-filters.php');
    276 include_once(ABSPATH . WPINC . '/streams.php');
    277 include_once(ABSPATH . WPINC . '/gettext.php');
     276include_once(ABSPATH . WPINC . '/pomo/mo.php');
    278277require_once (ABSPATH . WPINC . '/l10n.php');
    279278
    280279if ( !is_blog_installed() && (strpos($_SERVER['PHP_SELF'], 'install.php') === false && !defined('WP_INSTALLING')) ) {