Skip to content

Commit

Permalink
load_textdomain() cached
Browse files Browse the repository at this point in the history
Much better performance gained after the MO object has been cached on the local storage.
  • Loading branch information
itma committed Jun 21, 2022
1 parent 29cfbec commit cb1e3d2
Showing 1 changed file with 105 additions and 43 deletions.
148 changes: 105 additions & 43 deletions wp-includes/l10n.php
@@ -1,4 +1,7 @@
<?php

define('WP_l10n_CACHE_DIR', ABSPATH . 'wp-cache/l10n/');

/**
* Core Translation API
*
Expand Down Expand Up @@ -110,7 +113,7 @@ function get_user_locale( $user_id = 0 ) {
}

/**
* Determines the current locale desired for the request.
* Determine the current locale desired for the request.
*
* @since 5.0.0
*
Expand Down Expand Up @@ -167,7 +170,7 @@ function determine_locale() {
}

/**
* Retrieves the translation of $text.
* Retrieve the translation of $text.
*
* If there is no translation, or the text domain isn't loaded, the original text is returned.
*
Expand Down Expand Up @@ -213,7 +216,7 @@ function translate( $text, $domain = 'default' ) {
}

/**
* Removes last item on a pipe-delimited string.
* Remove last item on a pipe-delimited string.
*
* Meant for removing the last item in a string, such as 'Role name|User role'. The original
* string will be returned if no pipe '|' characters are found in the string.
Expand All @@ -233,7 +236,7 @@ function before_last_bar( $string ) {
}

/**
* Retrieves the translation of $text in the context defined in $context.
* Retrieve the translation of $text in the context defined in $context.
*
* If there is no translation, or the text domain isn't loaded, the original text is returned.
*
Expand Down Expand Up @@ -282,7 +285,7 @@ function translate_with_gettext_context( $text, $context, $domain = 'default' )
}

/**
* Retrieves the translation of $text.
* Retrieve the translation of $text.
*
* If there is no translation, or the text domain isn't loaded, the original text is returned.
*
Expand All @@ -298,7 +301,7 @@ function __( $text, $domain = 'default' ) {
}

/**
* Retrieves the translation of $text and escapes it for safe use in an attribute.
* Retrieve the translation of $text and escapes it for safe use in an attribute.
*
* If there is no translation, or the text domain isn't loaded, the original text is returned.
*
Expand All @@ -314,7 +317,7 @@ function esc_attr__( $text, $domain = 'default' ) {
}

/**
* Retrieves the translation of $text and escapes it for safe use in HTML output.
* Retrieve the translation of $text and escapes it for safe use in HTML output.
*
* If there is no translation, or the text domain isn't loaded, the original text
* is escaped and returned.
Expand All @@ -331,7 +334,7 @@ function esc_html__( $text, $domain = 'default' ) {
}

/**
* Displays translated text.
* Display translated text.
*
* @since 1.2.0
*
Expand All @@ -344,7 +347,7 @@ function _e( $text, $domain = 'default' ) {
}

/**
* Displays translated text that has been escaped for safe use in an attribute.
* Display translated text that has been escaped for safe use in an attribute.
*
* Encodes `< > & " '` (less than, greater than, ampersand, double quote, single quote).
* Will never double encode entities.
Expand All @@ -362,7 +365,7 @@ function esc_attr_e( $text, $domain = 'default' ) {
}

/**
* Displays translated text that has been escaped for safe use in HTML output.
* Display translated text that has been escaped for safe use in HTML output.
*
* If there is no translation, or the text domain isn't loaded, the original text
* is escaped and displayed.
Expand All @@ -380,7 +383,7 @@ function esc_html_e( $text, $domain = 'default' ) {
}

/**
* Retrieves translated string with gettext context.
* Retrieve translated string with gettext context.
*
* Quite a few times, there will be collisions with similar translatable text
* found in more than two places, but with different translated context.
Expand All @@ -401,7 +404,7 @@ function _x( $text, $context, $domain = 'default' ) {
}

/**
* Displays translated string with gettext context.
* Display translated string with gettext context.
*
* @since 3.0.0
*
Expand All @@ -415,7 +418,7 @@ function _ex( $text, $context, $domain = 'default' ) {
}

/**
* Translates string with gettext context, and escapes it for safe use in an attribute.
* Translate string with gettext context, and escapes it for safe use in an attribute.
*
* If there is no translation, or the text domain isn't loaded, the original text
* is escaped and returned.
Expand All @@ -433,7 +436,7 @@ function esc_attr_x( $text, $context, $domain = 'default' ) {
}

/**
* Translates string with gettext context, and escapes it for safe use in HTML output.
* Translate string with gettext context, and escapes it for safe use in HTML output.
*
* If there is no translation, or the text domain isn't loaded, the original text
* is escaped and returned.
Expand Down Expand Up @@ -587,12 +590,12 @@ function _nx( $single, $plural, $number, $context, $domain = 'default' ) {
* @return array {
* Array of translation information for the strings.
*
* @type string $0 Singular form to be localized. No longer used.
* @type string $1 Plural form to be localized. No longer used.
* @type string $singular Singular form to be localized.
* @type string $plural Plural form to be localized.
* @type null $context Context information for the translators.
* @type string|null $domain Text domain.
* @type string $0 Singular form to be localized. No longer used.
* @type string $1 Plural form to be localized. No longer used.
* @type string $singular Singular form to be localized.
* @type string $plural Plural form to be localized.
* @type null $context Context information for the translators.
* @type string $domain Text domain.
* }
*/
function _n_noop( $singular, $plural, $domain = null ) {
Expand Down Expand Up @@ -654,7 +657,7 @@ function _nx_noop( $singular, $plural, $context, $domain = null ) {
}

/**
* Translates and returns the singular or plural form of a string that's been registered
* Translates and retrieves the singular or plural form of a string that's been registered
* with _n_noop() or _nx_noop().
*
* Used when you want to use a translatable plural string once the number is known.
Expand All @@ -667,18 +670,11 @@ function _nx_noop( $singular, $plural, $context, $domain = null ) {
*
* @since 3.1.0
*
* @param array $nooped_plural {
* Array that is usually a return value from _n_noop() or _nx_noop().
*
* @type string $singular Singular form to be localized.
* @type string $plural Plural form to be localized.
* @type string|null $context Context information for the translators.
* @type string|null $domain Text domain.
* }
* @param array $nooped_plural Array with singular, plural, and context keys, usually the result of _n_noop() or _nx_noop().
* @param int $count Number of objects.
* @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. If $nooped_plural contains
* a text domain passed to _n_noop() or _nx_noop(), it will override this value. Default 'default'.
* @return string Either $singular or $plural translated text.
* @return string Either $single or $plural translated text.
*/
function translate_nooped_plural( $nooped_plural, $count, $domain = 'default' ) {
if ( $nooped_plural['domain'] ) {
Expand All @@ -693,7 +689,59 @@ function translate_nooped_plural( $nooped_plural, $count, $domain = 'default' )
}

/**
* Loads a .mo file into the text domain $domain.
* Load a .mo file from file previously processed by
* the {@link https://developer.wordpress.org/reference/functions/load_textdomain/
* load_textdomain() function}.
*
* @param string $domain Text domain. Unique identifier for retrieving translated strings.
* @return bool True on success, false on failure.
*/
function load_textdomain_from_cache( $domain ) {
global $l10n;
if (is_dir(WP_l10n_CACHE_DIR)) {
if (!isset($l10n[ $domain ])) {
$l10n_mocachedfilepath = WP_l10n_CACHE_DIR . $domain;
if (is_file($l10n_mocachedfilepath)) {
$l10n_mofilecontent = file_get_contents($l10n_mocachedfilepath);
$l10n[ $domain ] = unserialize($l10n_mofilecontent);
return true;
}
}
}

return false;
}

/**
* Cache a .mo file to the local storage as previously processed by
* the {@link https://developer.wordpress.org/reference/functions/load_textdomain/
* load_textdomain() function}.
*
* @param string $domain Text domain. Unique identifier for retrieving translated strings.
* @param string $moobject A MO object
* @return bool True on success, false on failure.
*/
function cache_textdomain( $domain, $moobject, $cachettl = 86400 ) {

$l10n_cacheDirExists = true;
if (!is_dir(WP_l10n_CACHE_DIR)) {
if (mkdir(WP_l10n_CACHE_DIR, 0777, true) === false) {
$l10n_cacheDirExists = false;
}
}
if ($l10n_cacheDirExists === true) {
$l10n_mocachedfilepath = WP_l10n_CACHE_DIR . $domain;
if (is_file($l10n_mocachedfilepath) === false ||
((time() - filemtime($l10n_mocachedfilepath)) > $cachettl)) {
return file_put_contents($l10n_mocachedfilepath, serialize($moobject));
}
}

return false;
}

/**
* Load a .mo file into the text domain $domain.
*
* If the text domain already exists, the translations will be merged. If both
* sets have the same string, the translation from the original value will be taken.
Expand All @@ -713,6 +761,13 @@ function translate_nooped_plural( $nooped_plural, $count, $domain = 'default' )
function load_textdomain( $domain, $mofile ) {
global $l10n, $l10n_unloaded;

/**
* Loads the mo file from the file cache
*/
if ($l10n_mofilecontent = load_textdomain_from_cache($domain)) {
return $l10n_mofilecontent;
}

$l10n_unloaded = (array) $l10n_unloaded;

/**
Expand Down Expand Up @@ -769,11 +824,18 @@ function load_textdomain( $domain, $mofile ) {

$l10n[ $domain ] = &$mo;

/**
* Caches the processed MO file so that it doesn't have to be processed by each request
* @param string $domain Text domain. Unique identifier for retrieving translated strings.
* @param string $mo MO object.
*/
cache_textdomain($domain, $mo);

return true;
}

/**
* Unloads translations for a text domain.
* Unload translations for a text domain.
*
* @since 3.0.0
*
Expand Down Expand Up @@ -825,7 +887,7 @@ function unload_textdomain( $domain ) {
}

/**
* Loads default translated strings based on locale.
* Load default translated strings based on locale.
*
* Loads the .mo file in WP_LANG_DIR constant path from WordPress root.
* The translated (.mo) file is named based on the locale.
Expand Down Expand Up @@ -911,7 +973,7 @@ function load_plugin_textdomain( $domain, $deprecated = false, $plugin_rel_path
}

/**
* Loads the translated strings for a plugin residing in the mu-plugins directory.
* Load the translated strings for a plugin residing in the mu-plugins directory.
*
* @since 3.0.0
* @since 4.6.0 The function now tries to load the .mo file from the languages directory first.
Expand All @@ -938,7 +1000,7 @@ function load_muplugin_textdomain( $domain, $mu_plugin_rel_path = '' ) {
}

/**
* Loads the theme's translated strings.
* Load the theme's translated strings.
*
* If the current locale exists as a .mo file in the theme's root directory, it
* will be included in the translated strings by the $domain.
Expand Down Expand Up @@ -979,7 +1041,7 @@ function load_theme_textdomain( $domain, $path = false ) {
}

/**
* Loads the child themes translated strings.
* Load the child themes translated strings.
*
* If the current locale exists as a .mo file in the child themes
* root directory, it will be included in the translated strings by the $domain.
Expand Down Expand Up @@ -1306,7 +1368,7 @@ function _get_path_to_translation_from_lang_dir( $domain ) {
}

/**
* Returns the Translations instance for a text domain.
* Return the Translations instance for a text domain.
*
* If there isn't one, returns empty Translations instance.
*
Expand All @@ -1332,7 +1394,7 @@ function get_translations_for_domain( $domain ) {
}

/**
* Determines whether there are translations for the text domain.
* Whether there are translations for the text domain.
*
* @since 3.0.0
*
Expand Down Expand Up @@ -1371,7 +1433,7 @@ function translate_user_role( $name, $domain = 'default' ) {
}

/**
* Gets all available languages based on the presence of *.mo files in a given directory.
* Get all available languages based on the presence of *.mo files in a given directory.
*
* The default directory is WP_LANG_DIR.
*
Expand Down Expand Up @@ -1408,7 +1470,7 @@ function get_available_languages( $dir = null ) {
}

/**
* Gets installed translations.
* Get installed translations.
*
* Looks in the wp-content/languages directory for translations of
* plugins or themes.
Expand Down Expand Up @@ -1464,7 +1526,7 @@ function wp_get_installed_translations( $type ) {
}

/**
* Extracts headers from a PO file.
* Extract headers from a PO file.
*
* @since 3.7.0
*
Expand All @@ -1489,7 +1551,7 @@ function wp_get_pomo_file_data( $po_file ) {
}

/**
* Displays or returns a Language selector.
* Language selector.
*
* @since 4.0.0
* @since 4.3.0 Introduced the `echo` argument.
Expand Down Expand Up @@ -1719,7 +1781,7 @@ function restore_current_locale() {
}

/**
* Determines whether switch_to_locale() is in effect.
* Whether switch_to_locale() is in effect.
*
* @since 4.7.0
*
Expand Down

0 comments on commit cb1e3d2

Please sign in to comment.