WordPress.org

Make WordPress Core

Ticket #8128: contextual-ngettext.diff

File contextual-ngettext.diff, 1.9 KB (added by nbachiyski, 6 years ago)

Adds a ngettext variant, which supports contexts

  • wp-includes/l10n.php

     
    7070                return apply_filters('gettext', $text, $text, $domain); 
    7171} 
    7272 
     73function before_last_bar( $string ) { 
     74        $last_bar = strrpos( $string, '|' ); 
     75        if ( false == $last_bar ) 
     76                return $string; 
     77        else 
     78                return substr( $string, 0, $last_bar ); 
     79} 
     80 
    7381/** 
    7482 * Retrieve the translated text and strip context. 
    7583 * 
     
    8694 * @param string $domain Domain to retrieve the translated text 
    8795 * @return string Translated text 
    8896 */ 
    89 function translate_with_context($text, $domain = 'default') { 
    90         $whole = translate($text, $domain); 
    91         $last_bar = strrpos($whole, '|'); 
    92         if ( false == $last_bar ) { 
    93                 return $whole; 
    94         } else { 
    95                 return substr($whole, 0, $last_bar); 
    96         } 
     97function translate_with_context( $text, $domain = 'default' ) { 
     98        return before_last_bar( translate( $text, $domain ) ); 
     99 
    97100} 
    98101 
    99102/** 
     
    182185} 
    183186 
    184187/** 
     188 * @see __ngettext() An alias of __ngettext 
     189 * 
     190 */ 
     191function _n() { 
     192        $args = func_get_args(); 
     193        return call_user_func_array('__ngettext', $args); 
     194} 
     195 
     196/** 
     197 * @see _n() A version of _n(), which supports contexts -- 
     198 * strips everything from the translation after the last bar 
     199 * 
     200 */ 
     201function _nc( $single, $plural, $number, $domain = 'default' ) { 
     202        return before_last_bar( __ngettext( $single, $plural, $number, $domain ) ); 
     203} 
     204 
     205/** 
    185206 * Register plural strings in POT file, but don't translate them. 
    186207 * 
    187208 * Used when you want do keep structures with translatable plural strings and 
     
    208229} 
    209230 
    210231/** 
     232 * @see __ngettext_noop() An alias of __ngettext_noop() 
     233 * 
     234 */ 
     235function _n_noop() { 
     236        $args = func_get_args(); 
     237        return call_user_func_array('__ngettext_noop', $args); 
     238} 
     239 
     240/** 
    211241 * Loads MO file into the list of domains. 
    212242 * 
    213243 * If the domain already exists, the inclusion will fail. If the MO file is not