From 8b483b77b90ec75ed7a37c1c9d0b2ac3762c899b Mon Sep 17 00:00:00 2001 From: Jake Spurlock Date: Tue, 23 Jun 2020 04:41:02 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@48131 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/l10n.php | 59 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 55 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/l10n.php b/src/wp-includes/l10n.php index 3ffa709643..75eaa0581c 100644 --- a/src/wp-includes/l10n.php +++ b/src/wp-includes/l10n.php @@ -165,6 +165,7 @@ function determine_locale() { * *Note:* Don't use translate() directly, use __() or related functions. * * @since 2.2.0 + * @since 5.5.0 Introduced gettext-{$domain} filter. * * @param string $text Text to translate. * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. @@ -184,7 +185,17 @@ function translate( $text, $domain = 'default' ) { * @param string $text Text to translate. * @param string $domain Text domain. Unique identifier for retrieving translated strings. */ - return apply_filters( 'gettext', $translation, $text, $domain ); + $translation = apply_filters( 'gettext', $translation, $text, $domain ); + + /** + * Filters text with its translation for a domain. + * + * @since 5.5.0 + * + * @param string $translation Translated text. + * @param string $text Text to translate. + */ + return apply_filters( 'gettext-' . $domain, $translation, $text ); } /** @@ -215,6 +226,7 @@ function before_last_bar( $string ) { * *Note:* Don't use translate_with_gettext_context() directly, use _x() or related functions. * * @since 2.8.0 + * @since 5.5.0 Introduced gettext_with_context-{$domain} filter. * * @param string $text Text to translate. * @param string $context Context information for the translators. @@ -225,6 +237,7 @@ function before_last_bar( $string ) { function translate_with_gettext_context( $text, $context, $domain = 'default' ) { $translations = get_translations_for_domain( $domain ); $translation = $translations->translate( $text, $context ); + /** * Filters text with its translation based on context information. * @@ -235,7 +248,18 @@ function translate_with_gettext_context( $text, $context, $domain = 'default' ) * @param string $context Context information for the translators. * @param string $domain Text domain. Unique identifier for retrieving translated strings. */ - return apply_filters( 'gettext_with_context', $translation, $text, $context, $domain ); + $translation = apply_filters( 'gettext_with_context', $translation, $text, $context, $domain ); + + /** + * Filters text with its translation based on context information for a domain. + * + * @since 5.5.0 + * + * @param string $translation Translated text. + * @param string $text Text to translate. + * @param string $context Context information for the translators. + */ + return apply_filters( 'gettext_with_context-' . $domain, $translation, $text, $context ); } /** @@ -419,6 +443,7 @@ function esc_html_x( $text, $context, $domain = 'default' ) { * printf( _n( '%s person', '%s people', $count, 'text-domain' ), number_format_i18n( $count ) ); * * @since 2.8.0 + * @since 5.5.0 Introduced ngettext-{$domain} filter. * * @param string $single The text to be used if the number is singular. * @param string $plural The text to be used if the number is plural. @@ -442,7 +467,19 @@ function _n( $single, $plural, $number, $domain = 'default' ) { * @param string $number The number to compare against to use either the singular or plural form. * @param string $domain Text domain. Unique identifier for retrieving translated strings. */ - return apply_filters( 'ngettext', $translation, $single, $plural, $number, $domain ); + $translation = apply_filters( 'ngettext', $translation, $single, $plural, $number, $domain ); + + /** + * Filters the singular or plural form of a string for a domain. + * + * @since 5.5.0 + * + * @param string $translation Translated text. + * @param string $single The text to be used if the number is singular. + * @param string $plural The text to be used if the number is plural. + * @param string $number The number to compare against to use either the singular or plural form. + */ + return apply_filters( 'ngettext-' . $domain, $translation, $single, $plural, $number ); } /** @@ -459,6 +496,7 @@ function _n( $single, $plural, $number, $domain = 'default' ) { * printf( _nx( '%s group', '%s groups', $animals, 'group of animals', 'text-domain' ), number_format_i18n( $animals ) ); * * @since 2.8.0 + * @since 5.5.0 Introduced ngettext_with_context-{$domain} filter. * * @param string $single The text to be used if the number is singular. * @param string $plural The text to be used if the number is plural. @@ -484,7 +522,20 @@ function _nx( $single, $plural, $number, $context, $domain = 'default' ) { * @param string $context Context information for the translators. * @param string $domain Text domain. Unique identifier for retrieving translated strings. */ - return apply_filters( 'ngettext_with_context', $translation, $single, $plural, $number, $context, $domain ); + $translation = apply_filters( 'ngettext_with_context', $translation, $single, $plural, $number, $context, $domain ); + + /** + * Filters the singular or plural form of a string with gettext context for a domain. + * + * @since 5.5.0 + * + * @param string $translation Translated text. + * @param string $single The text to be used if the number is singular. + * @param string $plural The text to be used if the number is plural. + * @param string $number The number to compare against to use either the singular or plural form. + * @param string $context Context information for the translators. + */ + return apply_filters( 'ngettext_with_context-' . $domain, $translation, $single, $plural, $number, $context ); } /**