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.



Built from https://develop.svn.wordpress.org/trunk@48131


git-svn-id: http://core.svn.wordpress.org/trunk@47900 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
whyisjake 2020-06-23 04:43:10 +00:00
parent e7f9aca8dd
commit 98fc60aabe
2 changed files with 56 additions and 5 deletions

View File

@ -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 );
}
/**

View File

@ -13,7 +13,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.5-alpha-48130';
$wp_version = '5.5-alpha-48131';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.