Ticket #17268: wp_gettext.patch

File wp_gettext.patch, 4.1 KB (added by linushoppe, 2 years ago)

Patch to reduce the used php-memory

  • wp-includes/

    old new  
    4747 
    4848        if ( empty( $locale ) ) 
    4949                $locale = 'en_US'; 
    50  
     50         
     51        putenv('LC_ALL=' . $locale); 
     52        setlocale(LC_ALL, $locale); 
     53         
    5154        return apply_filters( 'locale', $locale ); 
    5255} 
    5356 
     
    6568 * @return string Translated text 
    6669 */ 
    6770function translate( $text, $domain = 'default' ) { 
    68         $translations = &get_translations_for_domain( $domain ); 
    69         return apply_filters( 'gettext', $translations->translate( $text ), $text, $domain ); 
     71        if (function_exists ('dgettext')) 
     72          $translation = dgettext ($domain, $text); 
     73        else { 
     74          $translations = &get_translations_for_domain( $domain ); 
     75          $translation = $translations->translate( $text ); 
     76        } 
     77         
     78        return apply_filters( 'gettext', $translation, $text, $domain ); 
    7079} 
    7180 
    7281function before_last_bar( $string ) { 
     
    7887} 
    7988 
    8089function translate_with_gettext_context( $text, $context, $domain = 'default' ) { 
    81         $translations = &get_translations_for_domain( $domain ); 
    82         return apply_filters( 'gettext_with_context', $translations->translate( $text, $context ), $text, $context, $domain ); 
     90        if (function_exists ('dgettext')) 
     91          $translation = dgettext ($domain, $text); # TODO 
     92        else { 
     93          $translations = &get_translations_for_domain( $domain ); 
     94          $translation = $translations->translate( $text, $context ); 
     95        } 
     96         
     97        return apply_filters( 'gettext_with_context', $translation, $text, $context, $domain ); 
    8398} 
    8499 
    85100/** 
     
    236251 * @return string Either $single or $plural translated text 
    237252 */ 
    238253function _n( $single, $plural, $number, $domain = 'default' ) { 
    239         $translations = &get_translations_for_domain( $domain ); 
    240         $translation = $translations->translate_plural( $single, $plural, $number ); 
     254        if (function_exists ('dngettext'))   
     255          $translation = dngettext ($domain, $single, $plural, $number); 
     256        else { 
     257          $translations = &get_translations_for_domain( $domain ); 
     258          $translation = $translations->translate_plural( $single, $plural, $number ); 
     259        } 
     260         
    241261        return apply_filters( 'ngettext', $translation, $single, $plural, $number, $domain ); 
    242262} 
    243263 
     
    249269 * 
    250270 */ 
    251271function _nx($single, $plural, $number, $context, $domain = 'default') { 
    252         $translations = &get_translations_for_domain( $domain ); 
    253         $translation = $translations->translate_plural( $single, $plural, $number, $context ); 
     272        if (function_exists ('dngettext')) 
     273          $translation = dngettext ($domain, $single, $plural, $number); # TODO 
     274        else { 
     275          $translations = &get_translations_for_domain( $domain ); 
     276          $translation = $translations->translate_plural( $single, $plural, $number, $context ); 
     277        } 
     278         
    254279        return apply_filters( 'ngettext_with_context', $translation, $single, $plural, $number, $context, $domain ); 
    255280} 
    256281 
     
    313338        } 
    314339 
    315340        do_action( 'load_textdomain', $domain, $mofile ); 
    316  
     341         
    317342        $mofile = apply_filters( 'load_textdomain_mofile', $mofile, $domain ); 
    318  
     343         
    319344        if ( !is_readable( $mofile ) ) return false; 
     345         
     346        // Try to use gettext whenever available 
     347        if (function_exists ('bindtextdomain')) { 
     348          // Do an automatic setup 
     349          $Locale = basename ($mofile, '.mo'); 
     350           
     351          if (($p = strrpos ($Locale, '-')) !== false) 
     352            $Locale = substr ($Locale, $p + 1); 
     353           
     354          if (!is_file ('./wp-lang/' . $Locale . '/LC_MESSAGES/' . $domain . '.mo') && 
     355              (is_dir ('./wp-lang/' . $Locale . '/LC_MESSAGES') || mkdir ('./wp-lang/' . $Locale . '/LC_MESSAGES', 0775, true))) 
     356            copy ($mofile, './wp-lang/' . $Locale . '/LC_MESSAGES/' . $domain . '.mo'); 
     357           
     358          // Configure gettext 
     359          bindtextdomain ($domain, './wp-lang/'); 
     360          bind_textdomain_codeset ($domain, 'UTF-8'); 
     361           
     362          // Register a NOOP-Hanlder for this domain - just to keep consistency 
     363          $l10n[$domain] = new NOOP_Translations; 
     364           
     365          return true; 
     366        } 
    320367 
    321368        $mo = new MO(); 
    322369        if ( !$mo->import_from_file( $mofile ) ) return false; 
     
    468515function &get_translations_for_domain( $domain ) { 
    469516        global $l10n; 
    470517        if ( !isset( $l10n[$domain] ) ) { 
    471                 $l10n[$domain] = &new NOOP_Translations; 
     518                $l10n[$domain] = new NOOP_Translations; 
    472519        } 
    473520        return $l10n[$domain]; 
    474521}