Make WordPress Core

Ticket #10971: l10n-translation-noop-performance-2.patch

File l10n-translation-noop-performance-2.patch, 2.8 KB (added by johanee, 15 years ago)

Non-object approach take 2

  • wp-includes/l10n.php

     
    5555 * @return string Translated text
    5656 */
    5757function translate( $text, $domain = 'default' ) {
    58         $translations = &get_translations_for_domain( $domain );
    59         return apply_filters('gettext', $translations->translate($text), $text, $domain);
     58        global $l10n;
     59
     60        $translation = isset($l10n[$domain]) ? $l10n[$domain]->translate($text) : $text;
     61        return apply_filters('gettext', $translation, $text, $domain);
    6062}
    6163
    6264function before_last_bar( $string ) {
     
    8486}
    8587
    8688function translate_with_gettext_context( $text, $context, $domain = 'default' ) {
    87         $translations = &get_translations_for_domain( $domain );
    88         return apply_filters( 'gettext_with_context', $translations->translate( $text, $context ), $text, $context, $domain);
     89        global $l10n;
     90
     91        $translation = isset($l10n[$domain]) ? $l10n[$domain]->translate($text, $context) : $text;
     92        return apply_filters('gettext_with_context', $translation, $text, $context, $domain);
    8993}
    9094
    9195/**
     
    236240 * @return string Either $single or $plural translated text
    237241 */
    238242function _n($single, $plural, $number, $domain = 'default') {
    239         $translations = &get_translations_for_domain( $domain );
    240         $translation = $translations->translate_plural( $single, $plural, $number );
     243        global $l10n;
     244
     245        if ( isset($l10n[$domain]) )
     246                $translation = $l10n[$domain]->translate_plural( $single, $plural, $number );
     247        else
     248                $translation = 1 == $number ? $single : $plural;
     249
    241250        return apply_filters( 'ngettext', $translation, $single, $plural, $number, $domain );
    242251}
    243252
     
    251260}
    252261
    253262function _nx($single, $plural, $number, $context, $domain = 'default') {
    254         $translations = &get_translations_for_domain( $domain );
    255         $translation = $translations->translate_plural( $single, $plural, $number, $context );
     263        global $l10n;
     264
     265        if ( isset($l10n[$domain]) )
     266                $translation = $l10n[$domain]->translate_plural( $single, $plural, $number, $context );
     267        else
     268                $translation = 1 == $number ? $single : $plural;
     269
    256270        return apply_filters( 'ngettext_with_context', $translation, $single, $plural, $number, $context, $domain );
    257271}
    258272
     
    396410}
    397411
    398412/**
    399  * Returns the Translations instance for a domain. If there isn't one,
    400  * returns empty Translations instance.
    401  *
    402  * @param string $domain
    403  * @return object A Translation instance
    404  */
    405 function &get_translations_for_domain( $domain ) {
    406         global $l10n;
    407         $empty = &new Translations;
    408         if ( isset($l10n[$domain]) )
    409                 return $l10n[$domain];
    410         else
    411                 return $empty;
    412 }
    413 
    414 /**
    415413 * Translates role name. Since the role names are in the database and
    416414 * not in the source there are dummy gettext calls to get them into the POT
    417415 * file and this function properly translates them back.