WordPress.org

Make WordPress Core

Opened 85 minutes ago

Last modified 30 minutes ago

#49518 new enhancement

Consider adding domain-specific i18N filter hooks

Reported by: geminilabs Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 5.3.2
Component: I18N Keywords:
Focuses: Cc:

Description

This would be useful in cases where plugins provide a search/replace option to translate plugin text. An example plugin is Site Reviews https://wordpress.org/plugins/site-reviews/ (which I develop and maintain). I optimise this process by calling a domain specific hook within each i18n filter hook, and perform any additional things inside these domain-specific hooks.

The problem with not having domain-specific i18n filter hooks is that the amount of times the i18n filter hooks are called are increased with every active plugin.

Even though there is little-to-no performance impact (by calling a domain specific hook within each i18n filter hook and only performing any additional things inside these domain-specific hooks), it still results in thousands, sometimes millions of function calls on every page load.

For example, on one system there were 14 million function calls on each page load due to this feature (even though the total combined execution time of these function calls was only ~<0.3s).

Here are the domain-specific i18n filter hooks I am proposing:

<?php
/**
 * Filters text with its translation for a domain.
 *
 * @since 5.4.0
 *
 * @param string $translation  Translated text.
 * @param string $text         Text to translate.
 */
$translation = apply_filters( 'gettext-' . $domain, $translation, $text );
<?php
/**
 * Filters text with its translation based on context information for a domain.
 *
 * @since 5.4.0
 *
 * @param string $translation  Translated text.
 * @param string $text         Text to translate.
 * @param string $context      Context information for the translators.
 */
$translation = apply_filters( 'gettext_with_context-' . $domain, $translation, $text, $context );
<?php
/**
 * Filters the singular or plural form of a string for a domain.
 *
 * @since 5.4.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.
 */
$translation = apply_filters( 'ngettext-' . $domain, $translation, $single, $plural, $number );
<?php
/**
 * Filters the singular or plural form of a string with gettext context for a domain.
 *
 * @since 5.4.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.
 */
$translation = apply_filters( 'ngettext_with_context-' . $domain, $translation, $single, $plural, $number, $context );

Attachments (2)

screenshot-11.png (51.8 KB) - added by geminilabs 73 minutes ago.
The Translation feature of Site Reviews
Function Call Count.png (78.2 KB) - added by geminilabs 28 minutes ago.

Download all attachments as: .zip

Change History (4)

@geminilabs
73 minutes ago

The Translation feature of Site Reviews

#2 @geminilabs
30 minutes ago

Typo correction: I meant to say, "14 million function calls each hour due to this feature"

Note: See TracTickets for help on using tickets.