Make WordPress Core


Ignore:
Timestamp:
06/23/2020 04:41:02 AM (4 years ago)
Author:
whyisjake
Message:

I18N: Introduce domain specific i18n gettext hooks.

Adding context to allow for more specific targeting. New hooks:

  • gettext-{$domain}
  • gettext_with_context-{$domain}
  • ngettext-{$domain}
  • ngettext_with_context-{$domain}

Fixes #49518.

Props geminilabs, garrett-eclipse, davidbaumwald, johnbillion, whyisjake.

File:
1 edited

Legend:

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

    r48109 r48131  
    166166 *
    167167 * @since 2.2.0
     168 * @since 5.5.0 Introduced gettext-{$domain} filter.
    168169 *
    169170 * @param string $text   Text to translate.
     
    185186     * @param string $domain       Text domain. Unique identifier for retrieving translated strings.
    186187     */
    187     return apply_filters( 'gettext', $translation, $text, $domain );
     188    $translation = apply_filters( 'gettext', $translation, $text, $domain );
     189
     190    /**
     191     * Filters text with its translation for a domain.
     192     *
     193     * @since 5.5.0
     194     *
     195     * @param string $translation  Translated text.
     196     * @param string $text         Text to translate.
     197     */
     198    return apply_filters( 'gettext-' . $domain, $translation, $text );
    188199}
    189200
     
    216227 *
    217228 * @since 2.8.0
     229 * @since 5.5.0 Introduced gettext_with_context-{$domain} filter.
    218230 *
    219231 * @param string $text    Text to translate.
     
    226238    $translations = get_translations_for_domain( $domain );
    227239    $translation  = $translations->translate( $text, $context );
     240
    228241    /**
    229242     * Filters text with its translation based on context information.
     
    236249     * @param string $domain       Text domain. Unique identifier for retrieving translated strings.
    237250     */
    238     return apply_filters( 'gettext_with_context', $translation, $text, $context, $domain );
     251    $translation = apply_filters( 'gettext_with_context', $translation, $text, $context, $domain );
     252
     253    /**
     254     * Filters text with its translation based on context information for a domain.
     255     *
     256     * @since 5.5.0
     257     *
     258     * @param string $translation  Translated text.
     259     * @param string $text         Text to translate.
     260     * @param string $context      Context information for the translators.
     261     */
     262    return apply_filters( 'gettext_with_context-' . $domain, $translation, $text, $context );
    239263}
    240264
     
    420444 *
    421445 * @since 2.8.0
     446 * @since 5.5.0 Introduced ngettext-{$domain} filter.
    422447 *
    423448 * @param string $single The text to be used if the number is singular.
     
    443468     * @param string $domain      Text domain. Unique identifier for retrieving translated strings.
    444469     */
    445     return apply_filters( 'ngettext', $translation, $single, $plural, $number, $domain );
     470    $translation = apply_filters( 'ngettext', $translation, $single, $plural, $number, $domain );
     471
     472    /**
     473     * Filters the singular or plural form of a string for a domain.
     474     *
     475     * @since 5.5.0
     476     *
     477     * @param string $translation Translated text.
     478     * @param string $single      The text to be used if the number is singular.
     479     * @param string $plural      The text to be used if the number is plural.
     480     * @param string $number      The number to compare against to use either the singular or plural form.
     481     */
     482    return apply_filters( 'ngettext-' . $domain, $translation, $single, $plural, $number );
    446483}
    447484
     
    460497 *
    461498 * @since 2.8.0
     499 * @since 5.5.0 Introduced ngettext_with_context-{$domain} filter.
    462500 *
    463501 * @param string $single  The text to be used if the number is singular.
     
    485523     * @param string $domain      Text domain. Unique identifier for retrieving translated strings.
    486524     */
    487     return apply_filters( 'ngettext_with_context', $translation, $single, $plural, $number, $context, $domain );
     525    $translation = apply_filters( 'ngettext_with_context', $translation, $single, $plural, $number, $context, $domain );
     526
     527    /**
     528     * Filters the singular or plural form of a string with gettext context for a domain.
     529     *
     530     * @since 5.5.0
     531     *
     532     * @param string $translation Translated text.
     533     * @param string $single      The text to be used if the number is singular.
     534     * @param string $plural      The text to be used if the number is plural.
     535     * @param string $number      The number to compare against to use either the singular or plural form.
     536     * @param string $context     Context information for the translators.
     537     */
     538    return apply_filters( 'ngettext_with_context-' . $domain, $translation, $single, $plural, $number, $context );
    488539}
    489540
Note: See TracChangeset for help on using the changeset viewer.