--- a/wp/wp-includes/l10n.php Thu Sep 29 08:06:27 2022 +0200
+++ b/wp/wp-includes/l10n.php Fri Sep 05 18:40:08 2025 +0200
@@ -88,29 +88,31 @@
*
* @since 4.7.0
*
- * @param int|WP_User $user_id User's ID or a WP_User object. Defaults to current user.
+ * @param int|WP_User $user User's ID or a WP_User object. Defaults to current user.
* @return string The locale of the user.
*/
-function get_user_locale( $user_id = 0 ) {
- $user = false;
- if ( 0 === $user_id && function_exists( 'wp_get_current_user' ) ) {
- $user = wp_get_current_user();
- } elseif ( $user_id instanceof WP_User ) {
- $user = $user_id;
- } elseif ( $user_id && is_numeric( $user_id ) ) {
- $user = get_user_by( 'id', $user_id );
+function get_user_locale( $user = 0 ) {
+ $user_object = false;
+
+ if ( 0 === $user && function_exists( 'wp_get_current_user' ) ) {
+ $user_object = wp_get_current_user();
+ } elseif ( $user instanceof WP_User ) {
+ $user_object = $user;
+ } elseif ( $user && is_numeric( $user ) ) {
+ $user_object = get_user_by( 'id', $user );
}
- if ( ! $user ) {
+ if ( ! $user_object ) {
return get_locale();
}
- $locale = $user->locale;
+ $locale = $user_object->locale;
+
return $locale ? $locale : get_locale();
}
/**
- * Determine the current locale desired for the request.
+ * Determines the current locale desired for the request.
*
* @since 5.0.0
*
@@ -130,30 +132,37 @@
*/
$determined_locale = apply_filters( 'pre_determine_locale', null );
- if ( ! empty( $determined_locale ) && is_string( $determined_locale ) ) {
+ if ( $determined_locale && is_string( $determined_locale ) ) {
return $determined_locale;
}
- $determined_locale = get_locale();
-
- if ( is_admin() ) {
+ if (
+ isset( $GLOBALS['pagenow'] ) && 'wp-login.php' === $GLOBALS['pagenow'] &&
+ ( ! empty( $_GET['wp_lang'] ) || ! empty( $_COOKIE['wp_lang'] ) )
+ ) {
+ if ( ! empty( $_GET['wp_lang'] ) ) {
+ $determined_locale = sanitize_locale_name( $_GET['wp_lang'] );
+ } else {
+ $determined_locale = sanitize_locale_name( $_COOKIE['wp_lang'] );
+ }
+ } elseif (
+ is_admin() ||
+ ( isset( $_GET['_locale'] ) && 'user' === $_GET['_locale'] && wp_is_json_request() )
+ ) {
$determined_locale = get_user_locale();
- }
-
- if ( isset( $_GET['_locale'] ) && 'user' === $_GET['_locale'] && wp_is_json_request() ) {
- $determined_locale = get_user_locale();
+ } elseif (
+ ( ! empty( $_REQUEST['language'] ) || isset( $GLOBALS['wp_local_package'] ) )
+ && wp_installing()
+ ) {
+ if ( ! empty( $_REQUEST['language'] ) ) {
+ $determined_locale = sanitize_locale_name( $_REQUEST['language'] );
+ } else {
+ $determined_locale = $GLOBALS['wp_local_package'];
+ }
}
- $wp_lang = '';
-
- if ( ! empty( $_GET['wp_lang'] ) ) {
- $wp_lang = sanitize_text_field( $_GET['wp_lang'] );
- } elseif ( ! empty( $_COOKIE['wp_lang'] ) ) {
- $wp_lang = sanitize_text_field( $_COOKIE['wp_lang'] );
- }
-
- if ( ! empty( $wp_lang ) && ! empty( $GLOBALS['pagenow'] ) && 'wp-login.php' === $GLOBALS['pagenow'] ) {
- $determined_locale = $wp_lang;
+ if ( ! $determined_locale ) {
+ $determined_locale = get_locale();
}
/**
@@ -161,20 +170,20 @@
*
* @since 5.0.0
*
- * @param string $locale The locale.
+ * @param string $determined_locale The locale.
*/
return apply_filters( 'determine_locale', $determined_locale );
}
/**
- * Retrieve the translation of $text.
+ * Retrieves the translation of $text.
*
* If there is no translation, or the text domain isn't loaded, the original text is returned.
*
* *Note:* Don't use translate() directly, use __() or related functions.
*
* @since 2.2.0
- * @since 5.5.0 Introduced gettext-{$domain} filter.
+ * @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.
@@ -213,34 +222,34 @@
}
/**
- * Remove last item on a pipe-delimited string.
+ * Removes 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.
*
* @since 2.8.0
*
- * @param string $string A pipe-delimited string.
- * @return string Either $string or everything before the last pipe.
+ * @param string $text A pipe-delimited string.
+ * @return string Either $text or everything before the last pipe.
*/
-function before_last_bar( $string ) {
- $last_bar = strrpos( $string, '|' );
+function before_last_bar( $text ) {
+ $last_bar = strrpos( $text, '|' );
if ( false === $last_bar ) {
- return $string;
+ return $text;
} else {
- return substr( $string, 0, $last_bar );
+ return substr( $text, 0, $last_bar );
}
}
/**
- * Retrieve the translation of $text in the context defined in $context.
+ * Retrieves 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.
*
* *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.
+ * @since 5.5.0 Introduced `gettext_with_context-{$domain}` filter.
*
* @param string $text Text to translate.
* @param string $context Context information for the translators.
@@ -282,7 +291,7 @@
}
/**
- * Retrieve the translation of $text.
+ * Retrieves the translation of $text.
*
* If there is no translation, or the text domain isn't loaded, the original text is returned.
*
@@ -298,7 +307,7 @@
}
/**
- * Retrieve the translation of $text and escapes it for safe use in an attribute.
+ * Retrieves 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.
*
@@ -314,7 +323,7 @@
}
/**
- * Retrieve the translation of $text and escapes it for safe use in HTML output.
+ * Retrieves 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.
@@ -331,7 +340,7 @@
}
/**
- * Display translated text.
+ * Displays translated text.
*
* @since 1.2.0
*
@@ -344,7 +353,7 @@
}
/**
- * Display translated text that has been escaped for safe use in an attribute.
+ * Displays 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.
@@ -362,7 +371,7 @@
}
/**
- * Display translated text that has been escaped for safe use in HTML output.
+ * Displays 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.
@@ -380,7 +389,7 @@
}
/**
- * Retrieve translated string with gettext context.
+ * Retrieves 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.
@@ -401,7 +410,7 @@
}
/**
- * Display translated string with gettext context.
+ * Displays translated string with gettext context.
*
* @since 3.0.0
*
@@ -415,7 +424,7 @@
}
/**
- * Translate string with gettext context, and escapes it for safe use in an attribute.
+ * Translates 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.
@@ -433,7 +442,7 @@
}
/**
- * Translate string with gettext context, and escapes it for safe use in HTML output.
+ * Translates 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.
@@ -461,7 +470,7 @@
* 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.
+ * @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.
@@ -482,7 +491,7 @@
* @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 int $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.
*/
$translation = apply_filters( 'ngettext', $translation, $single, $plural, $number, $domain );
@@ -497,7 +506,7 @@
* @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 int $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.
*/
$translation = apply_filters( "ngettext_{$domain}", $translation, $single, $plural, $number, $domain );
@@ -519,7 +528,7 @@
* 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.
+ * @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.
@@ -541,7 +550,7 @@
* @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 int $number The number to compare against to use either the singular or plural form.
* @param string $context Context information for the translators.
* @param string $domain Text domain. Unique identifier for retrieving translated strings.
*/
@@ -557,7 +566,7 @@
* @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 int $number The number to compare against to use either the singular or plural form.
* @param string $context Context information for the translators.
* @param string $domain Text domain. Unique identifier for retrieving translated strings.
*/
@@ -587,12 +596,12 @@
* @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 $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|null $domain Text domain.
* }
*/
function _n_noop( $singular, $plural, $domain = null ) {
@@ -654,7 +663,7 @@
}
/**
- * Translates and retrieves the singular or plural form of a string that's been registered
+ * Translates and returns 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.
@@ -667,11 +676,18 @@
*
* @since 3.1.0
*
- * @param array $nooped_plural Array with singular, plural, and context keys, usually the result of _n_noop() or _nx_noop().
+ * @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 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 $single or $plural translated text.
+ * @return string Either $singular or $plural translated text.
*/
function translate_nooped_plural( $nooped_plural, $count, $domain = 'default' ) {
if ( $nooped_plural['domain'] ) {
@@ -686,7 +702,7 @@
}
/**
- * Load a .mo file into the text domain $domain.
+ * Loads 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.
@@ -695,29 +711,61 @@
* and will be a MO object.
*
* @since 1.5.0
+ * @since 6.1.0 Added the `$locale` parameter.
*
- * @global MO[] $l10n An array of all currently loaded text domains.
- * @global MO[] $l10n_unloaded An array of all text domains that have been unloaded again.
+ * @global MO[] $l10n An array of all currently loaded text domains.
+ * @global MO[] $l10n_unloaded An array of all text domains that have been unloaded again.
+ * @global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry.
*
* @param string $domain Text domain. Unique identifier for retrieving translated strings.
* @param string $mofile Path to the .mo file.
+ * @param string $locale Optional. Locale. Default is the current locale.
* @return bool True on success, false on failure.
*/
-function load_textdomain( $domain, $mofile ) {
- global $l10n, $l10n_unloaded;
+function load_textdomain( $domain, $mofile, $locale = null ) {
+ /** @var WP_Textdomain_Registry $wp_textdomain_registry */
+ global $l10n, $l10n_unloaded, $wp_textdomain_registry;
$l10n_unloaded = (array) $l10n_unloaded;
+ if ( ! is_string( $domain ) ) {
+ return false;
+ }
+
+ /**
+ * Filters whether to short-circuit loading .mo file.
+ *
+ * Returning a non-null value from the filter will effectively short-circuit
+ * the loading, returning the passed value instead.
+ *
+ * @since 6.3.0
+ *
+ * @param bool|null $loaded The result of loading a .mo file. Default null.
+ * @param string $domain Text domain. Unique identifier for retrieving translated strings.
+ * @param string $mofile Path to the MO file.
+ * @param string|null $locale Locale.
+ */
+ $loaded = apply_filters( 'pre_load_textdomain', null, $domain, $mofile, $locale );
+ if ( null !== $loaded ) {
+ if ( true === $loaded ) {
+ unset( $l10n_unloaded[ $domain ] );
+ }
+
+ return $loaded;
+ }
+
/**
* Filters whether to override the .mo file loading.
*
* @since 2.9.0
+ * @since 6.2.0 Added the `$locale` parameter.
*
- * @param bool $override Whether to override the .mo file loading. Default false.
- * @param string $domain Text domain. Unique identifier for retrieving translated strings.
- * @param string $mofile Path to the MO file.
+ * @param bool $override Whether to override the .mo file loading. Default false.
+ * @param string $domain Text domain. Unique identifier for retrieving translated strings.
+ * @param string $mofile Path to the MO file.
+ * @param string|null $locale Locale.
*/
- $plugin_override = apply_filters( 'override_load_textdomain', false, $domain, $mofile );
+ $plugin_override = apply_filters( 'override_load_textdomain', false, $domain, $mofile, $locale );
if ( true === (bool) $plugin_override ) {
unset( $l10n_unloaded[ $domain ] );
@@ -745,38 +793,89 @@
*/
$mofile = apply_filters( 'load_textdomain_mofile', $mofile, $domain );
- if ( ! is_readable( $mofile ) ) {
- return false;
+ if ( ! $locale ) {
+ $locale = determine_locale();
}
- $mo = new MO();
- if ( ! $mo->import_from_file( $mofile ) ) {
- return false;
+ $i18n_controller = WP_Translation_Controller::get_instance();
+
+ // Ensures the correct locale is set as the current one, in case it was filtered.
+ $i18n_controller->set_locale( $locale );
+
+ /**
+ * Filters the preferred file format for translation files.
+ *
+ * Can be used to disable the use of PHP files for translations.
+ *
+ * @since 6.5.0
+ *
+ * @param string $preferred_format Preferred file format. Possible values: 'php', 'mo'. Default: 'php'.
+ * @param string $domain The text domain.
+ */
+ $preferred_format = apply_filters( 'translation_file_format', 'php', $domain );
+ if ( ! in_array( $preferred_format, array( 'php', 'mo' ), true ) ) {
+ $preferred_format = 'php';
+ }
+
+ $translation_files = array();
+
+ if ( 'mo' !== $preferred_format ) {
+ $translation_files[] = substr_replace( $mofile, ".l10n.$preferred_format", - strlen( '.mo' ) );
}
- if ( isset( $l10n[ $domain ] ) ) {
- $mo->merge_with( $l10n[ $domain ] );
+ $translation_files[] = $mofile;
+
+ foreach ( $translation_files as $file ) {
+ /**
+ * Filters the file path for loading translations for the given text domain.
+ *
+ * Similar to the {@see 'load_textdomain_mofile'} filter with the difference that
+ * the file path could be for an MO or PHP file.
+ *
+ * @since 6.5.0
+ * @since 6.6.0 Added the `$locale` parameter.
+ *
+ * @param string $file Path to the translation file to load.
+ * @param string $domain The text domain.
+ * @param string $locale The locale.
+ */
+ $file = (string) apply_filters( 'load_translation_file', $file, $domain, $locale );
+
+ $success = $i18n_controller->load_file( $file, $domain, $locale );
+
+ if ( $success ) {
+ if ( isset( $l10n[ $domain ] ) && $l10n[ $domain ] instanceof MO ) {
+ $i18n_controller->load_file( $l10n[ $domain ]->get_filename(), $domain, $locale );
+ }
+
+ // Unset NOOP_Translations reference in get_translations_for_domain().
+ unset( $l10n[ $domain ] );
+
+ $l10n[ $domain ] = new WP_Translations( $i18n_controller, $domain );
+
+ $wp_textdomain_registry->set( $domain, $locale, dirname( $file ) );
+
+ return true;
+ }
}
- unset( $l10n_unloaded[ $domain ] );
-
- $l10n[ $domain ] = &$mo;
-
- return true;
+ return false;
}
/**
- * Unload translations for a text domain.
+ * Unloads translations for a text domain.
*
* @since 3.0.0
+ * @since 6.1.0 Added the `$reloadable` parameter.
*
* @global MO[] $l10n An array of all currently loaded text domains.
* @global MO[] $l10n_unloaded An array of all text domains that have been unloaded again.
*
- * @param string $domain Text domain. Unique identifier for retrieving translated strings.
+ * @param string $domain Text domain. Unique identifier for retrieving translated strings.
+ * @param bool $reloadable Whether the text domain can be loaded just-in-time again.
* @return bool Whether textdomain was unloaded.
*/
-function unload_textdomain( $domain ) {
+function unload_textdomain( $domain, $reloadable = false ) {
global $l10n, $l10n_unloaded;
$l10n_unloaded = (array) $l10n_unloaded;
@@ -785,14 +884,18 @@
* Filters whether to override the text domain unloading.
*
* @since 3.0.0
+ * @since 6.1.0 Added the `$reloadable` parameter.
*
- * @param bool $override Whether to override the text domain unloading. Default false.
- * @param string $domain Text domain. Unique identifier for retrieving translated strings.
+ * @param bool $override Whether to override the text domain unloading. Default false.
+ * @param string $domain Text domain. Unique identifier for retrieving translated strings.
+ * @param bool $reloadable Whether the text domain can be loaded just-in-time again.
*/
- $plugin_override = apply_filters( 'override_unload_textdomain', false, $domain );
+ $plugin_override = apply_filters( 'override_unload_textdomain', false, $domain, $reloadable );
if ( $plugin_override ) {
- $l10n_unloaded[ $domain ] = true;
+ if ( ! $reloadable ) {
+ $l10n_unloaded[ $domain ] = true;
+ }
return true;
}
@@ -801,15 +904,30 @@
* Fires before the text domain is unloaded.
*
* @since 3.0.0
+ * @since 6.1.0 Added the `$reloadable` parameter.
*
- * @param string $domain Text domain. Unique identifier for retrieving translated strings.
+ * @param string $domain Text domain. Unique identifier for retrieving translated strings.
+ * @param bool $reloadable Whether the text domain can be loaded just-in-time again.
*/
- do_action( 'unload_textdomain', $domain );
+ do_action( 'unload_textdomain', $domain, $reloadable );
+
+ // Since multiple locales are supported, reloadable text domains don't actually need to be unloaded.
+ if ( ! $reloadable ) {
+ WP_Translation_Controller::get_instance()->unload_textdomain( $domain );
+ }
if ( isset( $l10n[ $domain ] ) ) {
+ if ( $l10n[ $domain ] instanceof NOOP_Translations ) {
+ unset( $l10n[ $domain ] );
+
+ return false;
+ }
+
unset( $l10n[ $domain ] );
- $l10n_unloaded[ $domain ] = true;
+ if ( ! $reloadable ) {
+ $l10n_unloaded[ $domain ] = true;
+ }
return true;
}
@@ -818,7 +936,7 @@
}
/**
- * Load default translated strings based on locale.
+ * Loads 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.
@@ -836,21 +954,21 @@
}
// Unload previously loaded strings so we can switch translations.
- unload_textdomain( 'default' );
+ unload_textdomain( 'default', true );
- $return = load_textdomain( 'default', WP_LANG_DIR . "/$locale.mo" );
+ $return = load_textdomain( 'default', WP_LANG_DIR . "/$locale.mo", $locale );
if ( ( is_multisite() || ( defined( 'WP_INSTALLING_NETWORK' ) && WP_INSTALLING_NETWORK ) ) && ! file_exists( WP_LANG_DIR . "/admin-$locale.mo" ) ) {
- load_textdomain( 'default', WP_LANG_DIR . "/ms-$locale.mo" );
+ load_textdomain( 'default', WP_LANG_DIR . "/ms-$locale.mo", $locale );
return $return;
}
if ( is_admin() || wp_installing() || ( defined( 'WP_REPAIRING' ) && WP_REPAIRING ) ) {
- load_textdomain( 'default', WP_LANG_DIR . "/admin-$locale.mo" );
+ load_textdomain( 'default', WP_LANG_DIR . "/admin-$locale.mo", $locale );
}
if ( is_network_admin() || ( defined( 'WP_INSTALLING_NETWORK' ) && WP_INSTALLING_NETWORK ) ) {
- load_textdomain( 'default', WP_LANG_DIR . "/admin-network-$locale.mo" );
+ load_textdomain( 'default', WP_LANG_DIR . "/admin-network-$locale.mo", $locale );
}
return $return;
@@ -874,6 +992,13 @@
* @return bool True when textdomain is successfully loaded, false otherwise.
*/
function load_plugin_textdomain( $domain, $deprecated = false, $plugin_rel_path = false ) {
+ /** @var WP_Textdomain_Registry $wp_textdomain_registry */
+ global $wp_textdomain_registry;
+
+ if ( ! is_string( $domain ) ) {
+ return false;
+ }
+
/**
* Filters a plugin's locale.
*
@@ -887,7 +1012,7 @@
$mofile = $domain . '-' . $locale . '.mo';
// Try to load from the languages directory first.
- if ( load_textdomain( $domain, WP_LANG_DIR . '/plugins/' . $mofile ) ) {
+ if ( load_textdomain( $domain, WP_LANG_DIR . '/plugins/' . $mofile, $locale ) ) {
return true;
}
@@ -900,38 +1025,51 @@
$path = WP_PLUGIN_DIR;
}
- return load_textdomain( $domain, $path . '/' . $mofile );
+ $wp_textdomain_registry->set_custom_path( $domain, $path );
+
+ return load_textdomain( $domain, $path . '/' . $mofile, $locale );
}
/**
- * Load the translated strings for a plugin residing in the mu-plugins directory.
+ * Loads 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.
*
+ * @global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry.
+ *
* @param string $domain Text domain. Unique identifier for retrieving translated strings.
* @param string $mu_plugin_rel_path Optional. Relative to `WPMU_PLUGIN_DIR` directory in which the .mo
* file resides. Default empty string.
* @return bool True when textdomain is successfully loaded, false otherwise.
*/
function load_muplugin_textdomain( $domain, $mu_plugin_rel_path = '' ) {
+ /** @var WP_Textdomain_Registry $wp_textdomain_registry */
+ global $wp_textdomain_registry;
+
+ if ( ! is_string( $domain ) ) {
+ return false;
+ }
+
/** This filter is documented in wp-includes/l10n.php */
$locale = apply_filters( 'plugin_locale', determine_locale(), $domain );
$mofile = $domain . '-' . $locale . '.mo';
// Try to load from the languages directory first.
- if ( load_textdomain( $domain, WP_LANG_DIR . '/plugins/' . $mofile ) ) {
+ if ( load_textdomain( $domain, WP_LANG_DIR . '/plugins/' . $mofile, $locale ) ) {
return true;
}
$path = WPMU_PLUGIN_DIR . '/' . ltrim( $mu_plugin_rel_path, '/' );
- return load_textdomain( $domain, $path . '/' . $mofile );
+ $wp_textdomain_registry->set_custom_path( $domain, $path );
+
+ return load_textdomain( $domain, $path . '/' . $mofile, $locale );
}
/**
- * Load the theme's translated strings.
+ * Loads 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.
@@ -941,12 +1079,21 @@
* @since 1.5.0
* @since 4.6.0 The function now tries to load the .mo file from the languages directory first.
*
+ * @global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry.
+ *
* @param string $domain Text domain. Unique identifier for retrieving translated strings.
* @param string|false $path Optional. Path to the directory containing the .mo file.
* Default false.
* @return bool True when textdomain is successfully loaded, false otherwise.
*/
function load_theme_textdomain( $domain, $path = false ) {
+ /** @var WP_Textdomain_Registry $wp_textdomain_registry */
+ global $wp_textdomain_registry;
+
+ if ( ! is_string( $domain ) ) {
+ return false;
+ }
+
/**
* Filters a theme's locale.
*
@@ -960,7 +1107,7 @@
$mofile = $domain . '-' . $locale . '.mo';
// Try to load from the languages directory first.
- if ( load_textdomain( $domain, WP_LANG_DIR . '/themes/' . $mofile ) ) {
+ if ( load_textdomain( $domain, WP_LANG_DIR . '/themes/' . $mofile, $locale ) ) {
return true;
}
@@ -968,13 +1115,15 @@
$path = get_template_directory();
}
- return load_textdomain( $domain, $path . '/' . $locale . '.mo' );
+ $wp_textdomain_registry->set_custom_path( $domain, $path );
+
+ return load_textdomain( $domain, $path . '/' . $locale . '.mo', $locale );
}
/**
- * Load the child themes translated strings.
+ * Loads the child theme's translated strings.
*
- * If the current locale exists as a .mo file in the child themes
+ * If the current locale exists as a .mo file in the child theme's
* root directory, it will be included in the translated strings by the $domain.
*
* The .mo files must be named based on the locale exactly.
@@ -1008,7 +1157,7 @@
* @return string|false The translated strings in JSON encoding on success,
* false if the script textdomain could not be loaded.
*/
-function load_script_textdomain( $handle, $domain = 'default', $path = null ) {
+function load_script_textdomain( $handle, $domain = 'default', $path = '' ) {
$wp_scripts = wp_scripts();
if ( ! isset( $wp_scripts->registered[ $handle ] ) ) {
@@ -1032,7 +1181,7 @@
$src = $wp_scripts->registered[ $handle ]->src;
- if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $wp_scripts->content_url && 0 === strpos( $src, $wp_scripts->content_url ) ) ) {
+ if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $wp_scripts->content_url && str_starts_with( $src, $wp_scripts->content_url ) ) ) {
$src = $wp_scripts->base_url . $src;
}
@@ -1046,7 +1195,7 @@
// If the host is the same or it's a relative URL.
if (
- ( ! isset( $content_url['path'] ) || strpos( $src_url['path'], $content_url['path'] ) === 0 ) &&
+ ( ! isset( $content_url['path'] ) || str_starts_with( $src_url['path'], $content_url['path'] ) ) &&
( ! isset( $src_url['host'] ) || ! isset( $content_url['host'] ) || $src_url['host'] === $content_url['host'] )
) {
// Make the src relative the specific plugin or theme.
@@ -1058,12 +1207,12 @@
$relative = trim( $relative, '/' );
$relative = explode( '/', $relative );
- $languages_path = WP_LANG_DIR . '/' . $relative[0];
+ $languages_path = WP_LANG_DIR . '/plugins';
$relative = array_slice( $relative, 2 ); // Remove plugins/<plugin name> or themes/<theme name>.
$relative = implode( '/', $relative );
} elseif (
- ( ! isset( $plugins_url['path'] ) || strpos( $src_url['path'], $plugins_url['path'] ) === 0 ) &&
+ ( ! isset( $plugins_url['path'] ) || str_starts_with( $src_url['path'], $plugins_url['path'] ) ) &&
( ! isset( $src_url['host'] ) || ! isset( $plugins_url['host'] ) || $src_url['host'] === $plugins_url['host'] )
) {
// Make the src relative the specific plugin.
@@ -1082,7 +1231,7 @@
} elseif ( ! isset( $src_url['host'] ) || ! isset( $site_url['host'] ) || $src_url['host'] === $site_url['host'] ) {
if ( ! isset( $site_url['path'] ) ) {
$relative = trim( $src_url['path'], '/' );
- } elseif ( ( strpos( $src_url['path'], trailingslashit( $site_url['path'] ) ) === 0 ) ) {
+ } elseif ( str_starts_with( $src_url['path'], trailingslashit( $site_url['path'] ) ) ) {
// Make the src relative to the WP root.
$relative = substr( $src_url['path'], strlen( $site_url['path'] ) );
$relative = trim( $relative, '/' );
@@ -1105,7 +1254,7 @@
}
// Translations are always based on the unminified filename.
- if ( substr( $relative, -7 ) === '.min.js' ) {
+ if ( str_ends_with( $relative, '.min.js' ) ) {
$relative = substr( $relative, 0, -7 ) . '.js';
}
@@ -1189,23 +1338,24 @@
}
/**
- * Loads plugin and theme textdomains just-in-time.
+ * Loads plugin and theme text domains just-in-time.
*
* When a textdomain is encountered for the first time, we try to load
* the translation file from `wp-content/languages`, removing the need
- * to call load_plugin_texdomain() or load_theme_texdomain().
+ * to call load_plugin_textdomain() or load_theme_textdomain().
*
* @since 4.6.0
* @access private
*
- * @see get_translations_for_domain()
- * @global MO[] $l10n_unloaded An array of all text domains that have been unloaded again.
+ * @global MO[] $l10n_unloaded An array of all text domains that have been unloaded again.
+ * @global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry.
*
* @param string $domain Text domain. Unique identifier for retrieving translated strings.
* @return bool True when the textdomain is successfully loaded, false otherwise.
*/
function _load_textdomain_just_in_time( $domain ) {
- global $l10n_unloaded;
+ /** @var WP_Textdomain_Registry $wp_textdomain_registry */
+ global $l10n_unloaded, $wp_textdomain_registry;
$l10n_unloaded = (array) $l10n_unloaded;
@@ -1214,98 +1364,35 @@
return false;
}
- $translation_path = _get_path_to_translation( $domain );
- if ( false === $translation_path ) {
+ if ( ! $wp_textdomain_registry->has( $domain ) ) {
return false;
}
- return load_textdomain( $domain, $translation_path );
-}
-
-/**
- * Gets the path to a translation file for loading a textdomain just in time.
- *
- * Caches the retrieved results internally.
- *
- * @since 4.7.0
- * @access private
- *
- * @see _load_textdomain_just_in_time()
- *
- * @param string $domain Text domain. Unique identifier for retrieving translated strings.
- * @param bool $reset Whether to reset the internal cache. Used by the switch to locale functionality.
- * @return string|false The path to the translation file or false if no translation file was found.
- */
-function _get_path_to_translation( $domain, $reset = false ) {
- static $available_translations = array();
-
- if ( true === $reset ) {
- $available_translations = array();
+ $locale = determine_locale();
+ $path = $wp_textdomain_registry->get( $domain, $locale );
+ if ( ! $path ) {
+ return false;
+ }
+ // Themes with their language directory outside of WP_LANG_DIR have a different file name.
+ $template_directory = trailingslashit( get_template_directory() );
+ $stylesheet_directory = trailingslashit( get_stylesheet_directory() );
+ if ( str_starts_with( $path, $template_directory ) || str_starts_with( $path, $stylesheet_directory ) ) {
+ $mofile = "{$path}{$locale}.mo";
+ } else {
+ $mofile = "{$path}{$domain}-{$locale}.mo";
}
- if ( ! isset( $available_translations[ $domain ] ) ) {
- $available_translations[ $domain ] = _get_path_to_translation_from_lang_dir( $domain );
- }
-
- return $available_translations[ $domain ];
+ return load_textdomain( $domain, $mofile, $locale );
}
/**
- * Gets the path to a translation file in the languages directory for the current locale.
- *
- * Holds a cached list of available .mo files to improve performance.
- *
- * @since 4.7.0
- * @access private
- *
- * @see _get_path_to_translation()
- *
- * @param string $domain Text domain. Unique identifier for retrieving translated strings.
- * @return string|false The path to the translation file or false if no translation file was found.
- */
-function _get_path_to_translation_from_lang_dir( $domain ) {
- static $cached_mofiles = null;
-
- if ( null === $cached_mofiles ) {
- $cached_mofiles = array();
-
- $locations = array(
- WP_LANG_DIR . '/plugins',
- WP_LANG_DIR . '/themes',
- );
-
- foreach ( $locations as $location ) {
- $mofiles = glob( $location . '/*.mo' );
- if ( $mofiles ) {
- $cached_mofiles = array_merge( $cached_mofiles, $mofiles );
- }
- }
- }
-
- $locale = determine_locale();
- $mofile = "{$domain}-{$locale}.mo";
-
- $path = WP_LANG_DIR . '/plugins/' . $mofile;
- if ( in_array( $path, $cached_mofiles, true ) ) {
- return $path;
- }
-
- $path = WP_LANG_DIR . '/themes/' . $mofile;
- if ( in_array( $path, $cached_mofiles, true ) ) {
- return $path;
- }
-
- return false;
-}
-
-/**
- * Return the Translations instance for a text domain.
+ * Returns the Translations instance for a text domain.
*
* If there isn't one, returns empty Translations instance.
*
* @since 2.8.0
*
- * @global MO[] $l10n
+ * @global MO[] $l10n An array of all currently loaded text domains.
*
* @param string $domain Text domain. Unique identifier for retrieving translated strings.
* @return Translations|NOOP_Translations A Translations instance.
@@ -1318,25 +1405,27 @@
static $noop_translations = null;
if ( null === $noop_translations ) {
- $noop_translations = new NOOP_Translations;
+ $noop_translations = new NOOP_Translations();
}
+ $l10n[ $domain ] = &$noop_translations;
+
return $noop_translations;
}
/**
- * Whether there are translations for the text domain.
+ * Determines whether there are translations for the text domain.
*
* @since 3.0.0
*
- * @global MO[] $l10n
+ * @global MO[] $l10n An array of all currently loaded text domains.
*
* @param string $domain Text domain. Unique identifier for retrieving translated strings.
* @return bool Whether there are translations.
*/
function is_textdomain_loaded( $domain ) {
global $l10n;
- return isset( $l10n[ $domain ] );
+ return isset( $l10n[ $domain ] ) && ! $l10n[ $domain ] instanceof NOOP_Translations;
}
/**
@@ -1364,26 +1453,36 @@
}
/**
- * Get all available languages based on the presence of *.mo files in a given directory.
+ * Gets all available languages based on the presence of *.mo and *.l10n.php files in a given directory.
*
* The default directory is WP_LANG_DIR.
*
* @since 3.0.0
* @since 4.7.0 The results are now filterable with the {@see 'get_available_languages'} filter.
+ * @since 6.5.0 The initial file list is now cached and also takes into account *.l10n.php files.
+ *
+ * @global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry.
*
* @param string $dir A directory to search for language files.
* Default WP_LANG_DIR.
- * @return string[] An array of language codes or an empty array if no languages are present. Language codes are formed by stripping the .mo extension from the language file names.
+ * @return string[] An array of language codes or an empty array if no languages are present.
+ * Language codes are formed by stripping the file extension from the language file names.
*/
function get_available_languages( $dir = null ) {
+ global $wp_textdomain_registry;
+
$languages = array();
- $lang_files = glob( ( is_null( $dir ) ? WP_LANG_DIR : $dir ) . '/*.mo' );
+ $path = is_null( $dir ) ? WP_LANG_DIR : $dir;
+ $lang_files = $wp_textdomain_registry->get_language_files_from_path( $path );
+
if ( $lang_files ) {
foreach ( $lang_files as $lang_file ) {
$lang_file = basename( $lang_file, '.mo' );
- if ( 0 !== strpos( $lang_file, 'continents-cities' ) && 0 !== strpos( $lang_file, 'ms-' ) &&
- 0 !== strpos( $lang_file, 'admin-' ) ) {
+ $lang_file = basename( $lang_file, '.l10n.php' );
+
+ if ( ! str_starts_with( $lang_file, 'continents-cities' ) && ! str_starts_with( $lang_file, 'ms-' ) &&
+ ! str_starts_with( $lang_file, 'admin-' ) ) {
$languages[] = $lang_file;
}
}
@@ -1397,36 +1496,36 @@
* @param string[] $languages An array of available language codes.
* @param string $dir The directory where the language files were found.
*/
- return apply_filters( 'get_available_languages', $languages, $dir );
+ return apply_filters( 'get_available_languages', array_unique( $languages ), $dir );
}
/**
- * Get installed translations.
+ * Gets installed translations.
*
* Looks in the wp-content/languages directory for translations of
* plugins or themes.
*
* @since 3.7.0
*
+ * @global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry.
+ *
* @param string $type What to search for. Accepts 'plugins', 'themes', 'core'.
* @return array Array of language data.
*/
function wp_get_installed_translations( $type ) {
+ global $wp_textdomain_registry;
+
if ( 'themes' !== $type && 'plugins' !== $type && 'core' !== $type ) {
return array();
}
- $dir = 'core' === $type ? '' : "/$type";
+ $dir = 'core' === $type ? WP_LANG_DIR : WP_LANG_DIR . "/$type";
- if ( ! is_dir( WP_LANG_DIR ) ) {
+ if ( ! is_dir( $dir ) ) {
return array();
}
- if ( $dir && ! is_dir( WP_LANG_DIR . $dir ) ) {
- return array();
- }
-
- $files = scandir( WP_LANG_DIR . $dir );
+ $files = $wp_textdomain_registry->get_language_files_from_path( $dir );
if ( ! $files ) {
return array();
}
@@ -1434,16 +1533,7 @@
$language_data = array();
foreach ( $files as $file ) {
- if ( '.' === $file[0] || is_dir( WP_LANG_DIR . "$dir/$file" ) ) {
- continue;
- }
- if ( substr( $file, -3 ) !== '.po' ) {
- continue;
- }
- if ( ! preg_match( '/(?:(.+)-)?([a-z]{2,3}(?:_[A-Z]{2})?(?:_[a-z0-9]+)?).po/', $file, $match ) ) {
- continue;
- }
- if ( ! in_array( substr( $file, 0, -3 ) . '.mo', $files, true ) ) {
+ if ( ! preg_match( '/(?:(.+)-)?([a-z]{2,3}(?:_[A-Z]{2})?(?:_[a-z0-9]+)?)\.(?:mo|l10n\.php)/', basename( $file ), $match ) ) {
continue;
}
@@ -1451,13 +1541,31 @@
if ( '' === $textdomain ) {
$textdomain = 'default';
}
- $language_data[ $textdomain ][ $language ] = wp_get_pomo_file_data( WP_LANG_DIR . "$dir/$file" );
+
+ if ( str_ends_with( $file, '.mo' ) ) {
+ $pofile = substr_replace( $file, '.po', - strlen( '.mo' ) );
+
+ if ( ! file_exists( $pofile ) ) {
+ continue;
+ }
+
+ $language_data[ $textdomain ][ $language ] = wp_get_pomo_file_data( $pofile );
+ } else {
+ $pofile = substr_replace( $file, '.po', - strlen( '.l10n.php' ) );
+
+ // If both a PO and a PHP file exist, prefer the PO file.
+ if ( file_exists( $pofile ) ) {
+ continue;
+ }
+
+ $language_data[ $textdomain ][ $language ] = wp_get_l10n_php_file_data( $file );
+ }
}
return $language_data;
}
/**
- * Extract headers from a PO file.
+ * Extracts headers from a PO file.
*
* @since 3.7.0
*
@@ -1482,7 +1590,42 @@
}
/**
- * Language selector.
+ * Extracts headers from a PHP translation file.
+ *
+ * @since 6.6.0
+ *
+ * @param string $php_file Path to a `.l10n.php` file.
+ * @return string[] Array of file header values keyed by header name.
+ */
+function wp_get_l10n_php_file_data( $php_file ) {
+ $data = (array) include $php_file;
+
+ unset( $data['messages'] );
+ $headers = array(
+ 'POT-Creation-Date' => 'pot-creation-date',
+ 'PO-Revision-Date' => 'po-revision-date',
+ 'Project-Id-Version' => 'project-id-version',
+ 'X-Generator' => 'x-generator',
+ );
+
+ $result = array(
+ 'POT-Creation-Date' => '',
+ 'PO-Revision-Date' => '',
+ 'Project-Id-Version' => '',
+ 'X-Generator' => '',
+ );
+
+ foreach ( $headers as $po_header => $php_header ) {
+ if ( isset( $data[ $php_header ] ) ) {
+ $result[ $po_header ] = $data[ $php_header ];
+ }
+ }
+
+ return $result;
+}
+
+/**
+ * Displays or returns a Language selector.
*
* @since 4.0.0
* @since 4.3.0 Introduced the `echo` argument.
@@ -1498,7 +1641,7 @@
*
* @type string $id ID attribute of the select element. Default 'locale'.
* @type string $name Name attribute of the select element. Default 'locale'.
- * @type array $languages List of installed languages, contain only the locales.
+ * @type string[] $languages List of installed languages, contain only the locales.
* Default empty array.
* @type array $translations List of available translations. Default result of
* wp_get_available_translations().
@@ -1676,10 +1819,35 @@
/* @var WP_Locale_Switcher $wp_locale_switcher */
global $wp_locale_switcher;
+ if ( ! $wp_locale_switcher ) {
+ return false;
+ }
+
return $wp_locale_switcher->switch_to_locale( $locale );
}
/**
+ * Switches the translations according to the given user's locale.
+ *
+ * @since 6.2.0
+ *
+ * @global WP_Locale_Switcher $wp_locale_switcher WordPress locale switcher object.
+ *
+ * @param int $user_id User ID.
+ * @return bool True on success, false on failure.
+ */
+function switch_to_user_locale( $user_id ) {
+ /* @var WP_Locale_Switcher $wp_locale_switcher */
+ global $wp_locale_switcher;
+
+ if ( ! $wp_locale_switcher ) {
+ return false;
+ }
+
+ return $wp_locale_switcher->switch_to_user_locale( $user_id );
+}
+
+/**
* Restores the translations according to the previous locale.
*
* @since 4.7.0
@@ -1692,6 +1860,10 @@
/* @var WP_Locale_Switcher $wp_locale_switcher */
global $wp_locale_switcher;
+ if ( ! $wp_locale_switcher ) {
+ return false;
+ }
+
return $wp_locale_switcher->restore_previous_locale();
}
@@ -1708,11 +1880,15 @@
/* @var WP_Locale_Switcher $wp_locale_switcher */
global $wp_locale_switcher;
+ if ( ! $wp_locale_switcher ) {
+ return false;
+ }
+
return $wp_locale_switcher->restore_current_locale();
}
/**
- * Whether switch_to_locale() is in effect.
+ * Determines whether switch_to_locale() is in effect.
*
* @since 4.7.0
*
@@ -1783,5 +1959,32 @@
function wp_get_list_item_separator() {
global $wp_locale;
+ if ( ! ( $wp_locale instanceof WP_Locale ) ) {
+ // Default value of WP_Locale::get_list_item_separator().
+ /* translators: Used between list items, there is a space after the comma. */
+ return __( ', ' );
+ }
+
return $wp_locale->get_list_item_separator();
}
+
+/**
+ * Retrieves the word count type based on the locale.
+ *
+ * @since 6.2.0
+ *
+ * @global WP_Locale $wp_locale WordPress date and time locale object.
+ *
+ * @return string Locale-specific word count type. Possible values are `characters_excluding_spaces`,
+ * `characters_including_spaces`, or `words`. Defaults to `words`.
+ */
+function wp_get_word_count_type() {
+ global $wp_locale;
+
+ if ( ! ( $wp_locale instanceof WP_Locale ) ) {
+ // Default value of WP_Locale::get_word_count_type().
+ return 'words';
+ }
+
+ return $wp_locale->get_word_count_type();
+}