WordPress.org

Make WordPress Core

Changeset 10584


Ignore:
Timestamp:
02/17/2009 05:03:29 AM (12 years ago)
Author:
ryan
Message:

Switch to pomo lib. Support gettext contexts. Deprecate long form functions. Props nbachiyski. fixes #9112 #9111

Location:
trunk
Files:
6 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/l10n.php

    r9887 r10584  
    4545
    4646/**
    47  * Retrieve the translated text.
    48  *
    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  *
     47 * Retrieves the translation of $text. If there is no translation, or
     48 * the domain isn't loaded the original text is returned.
     49 *
     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.
     
    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
     
    8071
    8172/**
    82  * Retrieve the translated text and strip context.
    83  *
    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.
     73 * Translate $text like translate(), but assumes that the text
     74 * contains a context after its last vertical bar.
    8975 *
    9076 * @since 2.5
     
    10086}
    10187
    102 /**
    103  * Retrieves the translated string from the translate().
     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
     93/**
     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()
     
    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
     
    123115 * @param string $domain Optional. Domain to retrieve the translated text
    124116 */
    125 function _e($text, $domain = 'default') {
    126     echo translate($text, $domain);
    127 }
    128 
    129 /**
    130  * Retrieve context translated string.
     117function _e( $text, $domain = 'default' ) {
     118    echo translate( $text, $domain );
     119}
     120
     121/**
     122 * Retrieve translated string with vertical bar context
    131123 *
    132124 * Quite a few times, there will be collisions with similar translatable text
     
    148140function _c($text, $domain = 'default') {
    149141    return translate_with_context($text, $domain);
     142}
     143
     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);
    150152}
    151153
     
    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     }
    185 }
    186 
    187 /**
    188  * @see __ngettext() An alias of __ngettext
    189  *
    190  */
    191 function _n() {
    192     $args = func_get_args();
    193     return call_user_func_array('__ngettext', $args);
     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 );
    194180}
    195181
     
    200186 */
    201187function _nc( $single, $plural, $number, $domain = 'default' ) {
    202     return before_last_bar( __ngettext( $single, $plural, $number, $domain ) );
     188    return before_last_bar( _n( $single, $plural, $number, $domain ) );
     189}
     190
     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
     197/**
     198 * @deprecated Use _n_noop()
     199 */
     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
     
    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);
    229 }
    230 
    231 /**
    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);
     229function _n_noop( $single, $plural, $number = 1, $domain = 'default' ) {
     230    return array( $single, $plural );
    238231}
    239232
     
    259252    global $l10n;
    260253
    261     if ( is_readable($mofile))
    262         $input = new CachedFileReader($mofile);
    263     else
    264         return;
    265 
    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);
     254    if ( !is_readable($mofile)) return;
     255   
     256    $mo = new MO();
     257    $mo->import_from_file( $mofile );
     258
     259    if (isset($l10n[$domain]))
     260        $mo->merge_with( $l10n[$domain] );
     261       
     262    $l10n[$domain] = &$mo;
    276263}
    277264
     
    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?>
  • trunk/wp-settings.php

    r10513 r10584  
    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
Note: See TracChangeset for help on using the changeset viewer.