diff options
Diffstat (limited to 'srcs/wordpress/wp-includes/l10n.php')
| -rw-r--r-- | srcs/wordpress/wp-includes/l10n.php | 1631 |
1 files changed, 0 insertions, 1631 deletions
diff --git a/srcs/wordpress/wp-includes/l10n.php b/srcs/wordpress/wp-includes/l10n.php deleted file mode 100644 index 419ebc3..0000000 --- a/srcs/wordpress/wp-includes/l10n.php +++ /dev/null @@ -1,1631 +0,0 @@ -<?php -/** - * Core Translation API - * - * @package WordPress - * @subpackage i18n - * @since 1.2.0 - */ - -/** - * Retrieves the current locale. - * - * If the locale is set, then it will filter the locale in the {@see 'locale'} - * filter hook and return the value. - * - * If the locale is not set already, then the WPLANG constant is used if it is - * defined. Then it is filtered through the {@see 'locale'} filter hook and - * the value for the locale global set and the locale is returned. - * - * The process to get the locale should only be done once, but the locale will - * always be filtered using the {@see 'locale'} hook. - * - * @since 1.5.0 - * - * @global string $locale - * @global string $wp_local_package - * - * @return string The locale of the blog or from the {@see 'locale'} hook. - */ -function get_locale() { - global $locale, $wp_local_package; - - if ( isset( $locale ) ) { - /** - * Filters the locale ID of the WordPress installation. - * - * @since 1.5.0 - * - * @param string $locale The locale ID. - */ - return apply_filters( 'locale', $locale ); - } - - if ( isset( $wp_local_package ) ) { - $locale = $wp_local_package; - } - - // WPLANG was defined in wp-config. - if ( defined( 'WPLANG' ) ) { - $locale = WPLANG; - } - - // If multisite, check options. - if ( is_multisite() ) { - // Don't check blog option when installing. - if ( wp_installing() ) { - $ms_locale = get_site_option( 'WPLANG' ); - } else { - $ms_locale = get_option( 'WPLANG' ); - if ( false === $ms_locale ) { - $ms_locale = get_site_option( 'WPLANG' ); - } - } - - if ( $ms_locale !== false ) { - $locale = $ms_locale; - } - } else { - $db_locale = get_option( 'WPLANG' ); - if ( $db_locale !== false ) { - $locale = $db_locale; - } - } - - if ( empty( $locale ) ) { - $locale = 'en_US'; - } - - /** This filter is documented in wp-includes/l10n.php */ - return apply_filters( 'locale', $locale ); -} - -/** - * Retrieves the locale of a user. - * - * If the user has a locale set to a non-empty string then it will be - * returned. Otherwise it returns the locale of get_locale(). - * - * @since 4.7.0 - * - * @param int|WP_User $user_id 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 ); - } - - if ( ! $user ) { - return get_locale(); - } - - $locale = $user->locale; - return $locale ? $locale : get_locale(); -} - -/** - * Determine the current locale desired for the request. - * - * @since 5.0.0 - * - * @global string $pagenow - * - * @return string The determined locale. - */ -function determine_locale() { - /** - * Filters the locale for the current request prior to the default determination process. - * - * Using this filter allows to override the default logic, effectively short-circuiting the function. - * - * @since 5.0.0 - * - * @param string|null The locale to return and short-circuit, or null as default. - */ - $determined_locale = apply_filters( 'pre_determine_locale', null ); - if ( ! empty( $determined_locale ) && is_string( $determined_locale ) ) { - return $determined_locale; - } - - $determined_locale = get_locale(); - - if ( is_admin() ) { - $determined_locale = get_user_locale(); - } - - if ( isset( $_GET['_locale'] ) && 'user' === $_GET['_locale'] && wp_is_json_request() ) { - $determined_locale = get_user_locale(); - } - - if ( ! empty( $_GET['wp_lang'] ) && ! empty( $GLOBALS['pagenow'] ) && 'wp-login.php' === $GLOBALS['pagenow'] ) { - $determined_locale = sanitize_text_field( $_GET['wp_lang'] ); - } - - /** - * Filters the locale for the current request. - * - * @since 5.0.0 - * - * @param string $locale The locale. - */ - return apply_filters( 'determine_locale', $determined_locale ); -} - -/** - * Retrieve 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 - * - * @param string $text Text to translate. - * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. - * Default 'default'. - * @return string Translated text - */ -function translate( $text, $domain = 'default' ) { - $translations = get_translations_for_domain( $domain ); - $translation = $translations->translate( $text ); - - /** - * Filters text with its translation. - * - * @since 2.0.11 - * - * @param string $translation Translated text. - * @param string $text Text to translate. - * @param string $domain Text domain. Unique identifier for retrieving translated strings. - */ - return apply_filters( 'gettext', $translation, $text, $domain ); -} - -/** - * 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. - * - * @since 2.8.0 - * - * @param string $string A pipe-delimited string. - * @return string Either $string or everything before the last pipe. - */ -function before_last_bar( $string ) { - $last_bar = strrpos( $string, '|' ); - if ( false === $last_bar ) { - return $string; - } else { - return substr( $string, 0, $last_bar ); - } -} - -/** - * 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. - * - * *Note:* Don't use translate_with_gettext_context() directly, use _x() or related functions. - * - * @since 2.8.0 - * - * @param string $text Text to translate. - * @param string $context Context information for the translators. - * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. - * Default 'default'. - * @return string Translated text on success, original text on failure. - */ -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. - * - * @since 2.8.0 - * - * @param string $translation Translated text. - * @param string $text Text to translate. - * @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 ); -} - -/** - * Retrieve the translation of $text. - * - * If there is no translation, or the text domain isn't loaded, the original text is returned. - * - * @since 2.1.0 - * - * @param string $text Text to translate. - * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. - * Default 'default'. - * @return string Translated text. - */ -function __( $text, $domain = 'default' ) { - return translate( $text, $domain ); -} - -/** - * 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. - * - * @since 2.8.0 - * - * @param string $text Text to translate. - * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. - * Default 'default'. - * @return string Translated text on success, original text on failure. - */ -function esc_attr__( $text, $domain = 'default' ) { - return esc_attr( translate( $text, $domain ) ); -} - -/** - * 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. - * - * @since 2.8.0 - * - * @param string $text Text to translate. - * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. - * Default 'default'. - * @return string Translated text - */ -function esc_html__( $text, $domain = 'default' ) { - return esc_html( translate( $text, $domain ) ); -} - -/** - * Display translated text. - * - * @since 1.2.0 - * - * @param string $text Text to translate. - * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. - * Default 'default'. - */ -function _e( $text, $domain = 'default' ) { - echo translate( $text, $domain ); -} - -/** - * Display translated text that has been escaped for safe use in an attribute. - * - * @since 2.8.0 - * - * @param string $text Text to translate. - * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. - * Default 'default'. - */ -function esc_attr_e( $text, $domain = 'default' ) { - echo esc_attr( translate( $text, $domain ) ); -} - -/** - * Display translated text that has been escaped for safe use in HTML output. - * - * @since 2.8.0 - * - * @param string $text Text to translate. - * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. - * Default 'default'. - */ -function esc_html_e( $text, $domain = 'default' ) { - echo esc_html( translate( $text, $domain ) ); -} - -/** - * 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. - * - * By including the context in the pot file, translators can translate the two - * strings differently. - * - * @since 2.8.0 - * - * @param string $text Text to translate. - * @param string $context Context information for the translators. - * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. - * Default 'default'. - * @return string Translated context string without pipe. - */ -function _x( $text, $context, $domain = 'default' ) { - return translate_with_gettext_context( $text, $context, $domain ); -} - -/** - * Display translated string with gettext context. - * - * @since 3.0.0 - * - * @param string $text Text to translate. - * @param string $context Context information for the translators. - * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. - * Default 'default'. - * @return string Translated context string without pipe. - */ -function _ex( $text, $context, $domain = 'default' ) { - echo _x( $text, $context, $domain ); -} - -/** - * Translate string with gettext context, and escapes it for safe use in an attribute. - * - * @since 2.8.0 - * - * @param string $text Text to translate. - * @param string $context Context information for the translators. - * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. - * Default 'default'. - * @return string Translated text - */ -function esc_attr_x( $text, $context, $domain = 'default' ) { - return esc_attr( translate_with_gettext_context( $text, $context, $domain ) ); -} - -/** - * Translate string with gettext context, and escapes it for safe use in HTML output. - * - * @since 2.9.0 - * - * @param string $text Text to translate. - * @param string $context Context information for the translators. - * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. - * Default 'default'. - * @return string Translated text. - */ -function esc_html_x( $text, $context, $domain = 'default' ) { - return esc_html( translate_with_gettext_context( $text, $context, $domain ) ); -} - -/** - * Translates and retrieves the singular or plural form based on the supplied number. - * - * Used when you want to use the appropriate form of a string based on whether a - * number is singular or plural. - * - * Example: - * - * printf( _n( '%s person', '%s people', $count, 'text-domain' ), number_format_i18n( $count ) ); - * - * @since 2.8.0 - * - * @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 int $number The number to compare against to use either the singular or plural form. - * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. - * Default 'default'. - * @return string The translated singular or plural form. - */ -function _n( $single, $plural, $number, $domain = 'default' ) { - $translations = get_translations_for_domain( $domain ); - $translation = $translations->translate_plural( $single, $plural, $number ); - - /** - * Filters the singular or plural form of a string. - * - * @since 2.2.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 $domain Text domain. Unique identifier for retrieving translated strings. - */ - return apply_filters( 'ngettext', $translation, $single, $plural, $number, $domain ); -} - -/** - * Translates and retrieves the singular or plural form based on the supplied number, with gettext context. - * - * This is a hybrid of _n() and _x(). It supports context and plurals. - * - * Used when you want to use the appropriate form of a string with context based on whether a - * number is singular or plural. - * - * Example of a generic phrase which is disambiguated via the context parameter: - * - * printf( _nx( '%s group', '%s groups', $people, 'group of people', 'text-domain' ), number_format_i18n( $people ) ); - * printf( _nx( '%s group', '%s groups', $animals, 'group of animals', 'text-domain' ), number_format_i18n( $animals ) ); - * - * @since 2.8.0 - * - * @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 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 Optional. Text domain. Unique identifier for retrieving translated strings. - * Default 'default'. - * @return string The translated singular or plural form. - */ -function _nx( $single, $plural, $number, $context, $domain = 'default' ) { - $translations = get_translations_for_domain( $domain ); - $translation = $translations->translate_plural( $single, $plural, $number, $context ); - - /** - * Filters the singular or plural form of a string with gettext context. - * - * @since 2.8.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. - * @param string $domain Text domain. Unique identifier for retrieving translated strings. - */ - return apply_filters( 'ngettext_with_context', $translation, $single, $plural, $number, $context, $domain ); -} - -/** - * Registers plural strings in POT file, but does not translate them. - * - * Used when you want to keep structures with translatable plural - * strings and use them later when the number is known. - * - * Example: - * - * $message = _n_noop( '%s post', '%s posts', 'text-domain' ); - * ... - * printf( translate_nooped_plural( $message, $count, 'text-domain' ), number_format_i18n( $count ) ); - * - * @since 2.5.0 - * - * @param string $singular Singular form to be localized. - * @param string $plural Plural form to be localized. - * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. - * Default null. - * @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. - * } - */ -function _n_noop( $singular, $plural, $domain = null ) { - return array( - 0 => $singular, - 1 => $plural, - 'singular' => $singular, - 'plural' => $plural, - 'context' => null, - 'domain' => $domain, - ); -} - -/** - * Registers plural strings with gettext context in POT file, but does not translate them. - * - * Used when you want to keep structures with translatable plural - * strings and use them later when the number is known. - * - * Example of a generic phrase which is disambiguated via the context parameter: - * - * $messages = array( - * 'people' => _nx_noop( '%s group', '%s groups', 'people', 'text-domain' ), - * 'animals' => _nx_noop( '%s group', '%s groups', 'animals', 'text-domain' ), - * ); - * ... - * $message = $messages[ $type ]; - * printf( translate_nooped_plural( $message, $count, 'text-domain' ), number_format_i18n( $count ) ); - * - * @since 2.8.0 - * - * @param string $singular Singular form to be localized. - * @param string $plural Plural form to be localized. - * @param string $context Context information for the translators. - * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings. - * Default null. - * @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 $2 Context information for the translators. No longer used. - * @type string $singular Singular form to be localized. - * @type string $plural Plural form to be localized. - * @type string $context Context information for the translators. - * @type string $domain Text domain. - * } - */ -function _nx_noop( $singular, $plural, $context, $domain = null ) { - return array( - 0 => $singular, - 1 => $plural, - 2 => $context, - 'singular' => $singular, - 'plural' => $plural, - 'context' => $context, - 'domain' => $domain, - ); -} - -/** - * 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. - * - * Example: - * - * $message = _n_noop( '%s post', '%s posts', 'text-domain' ); - * ... - * printf( translate_nooped_plural( $message, $count, 'text-domain' ), number_format_i18n( $count ) ); - * - * @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 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. - */ -function translate_nooped_plural( $nooped_plural, $count, $domain = 'default' ) { - if ( $nooped_plural['domain'] ) { - $domain = $nooped_plural['domain']; - } - - if ( $nooped_plural['context'] ) { - return _nx( $nooped_plural['singular'], $nooped_plural['plural'], $count, $nooped_plural['context'], $domain ); - } else { - return _n( $nooped_plural['singular'], $nooped_plural['plural'], $count, $domain ); - } -} - -/** - * 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. - * - * On success, the .mo file will be placed in the $l10n global by $domain - * and will be a MO object. - * - * @since 1.5.0 - * - * @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 $mofile Path to the .mo file. - * @return bool True on success, false on failure. - */ -function load_textdomain( $domain, $mofile ) { - global $l10n, $l10n_unloaded; - - $l10n_unloaded = (array) $l10n_unloaded; - - /** - * Filters whether to override the .mo file loading. - * - * @since 2.9.0 - * - * @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. - */ - $plugin_override = apply_filters( 'override_load_textdomain', false, $domain, $mofile ); - - if ( true == $plugin_override ) { - unset( $l10n_unloaded[ $domain ] ); - - return true; - } - - /** - * Fires before the MO translation file is loaded. - * - * @since 2.9.0 - * - * @param string $domain Text domain. Unique identifier for retrieving translated strings. - * @param string $mofile Path to the .mo file. - */ - do_action( 'load_textdomain', $domain, $mofile ); - - /** - * Filters MO file path for loading translations for a specific text domain. - * - * @since 2.9.0 - * - * @param string $mofile Path to the MO file. - * @param string $domain Text domain. Unique identifier for retrieving translated strings. - */ - $mofile = apply_filters( 'load_textdomain_mofile', $mofile, $domain ); - - if ( ! is_readable( $mofile ) ) { - return false; - } - - $mo = new MO(); - if ( ! $mo->import_from_file( $mofile ) ) { - return false; - } - - if ( isset( $l10n[ $domain ] ) ) { - $mo->merge_with( $l10n[ $domain ] ); - } - - unset( $l10n_unloaded[ $domain ] ); - - $l10n[ $domain ] = &$mo; - - return true; -} - -/** - * Unload translations for a text domain. - * - * @since 3.0.0 - * - * @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. - * @return bool Whether textdomain was unloaded. - */ -function unload_textdomain( $domain ) { - global $l10n, $l10n_unloaded; - - $l10n_unloaded = (array) $l10n_unloaded; - - /** - * Filters whether to override the text domain unloading. - * - * @since 3.0.0 - * - * @param bool $override Whether to override the text domain unloading. Default false. - * @param string $domain Text domain. Unique identifier for retrieving translated strings. - */ - $plugin_override = apply_filters( 'override_unload_textdomain', false, $domain ); - - if ( $plugin_override ) { - $l10n_unloaded[ $domain ] = true; - - return true; - } - - /** - * Fires before the text domain is unloaded. - * - * @since 3.0.0 - * - * @param string $domain Text domain. Unique identifier for retrieving translated strings. - */ - do_action( 'unload_textdomain', $domain ); - - if ( isset( $l10n[ $domain ] ) ) { - unset( $l10n[ $domain ] ); - - $l10n_unloaded[ $domain ] = true; - - return true; - } - - return false; -} - -/** - * 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. - * - * @see load_textdomain() - * - * @since 1.5.0 - * - * @param string $locale Optional. Locale to load. Default is the value of get_locale(). - * @return bool Whether the textdomain was loaded. - */ -function load_default_textdomain( $locale = null ) { - if ( null === $locale ) { - $locale = determine_locale(); - } - - // Unload previously loaded strings so we can switch translations. - unload_textdomain( 'default' ); - - $return = load_textdomain( 'default', WP_LANG_DIR . "/$locale.mo" ); - - 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" ); - return $return; - } - - if ( is_admin() || wp_installing() || ( defined( 'WP_REPAIRING' ) && WP_REPAIRING ) ) { - load_textdomain( 'default', WP_LANG_DIR . "/admin-$locale.mo" ); - } - - if ( is_network_admin() || ( defined( 'WP_INSTALLING_NETWORK' ) && WP_INSTALLING_NETWORK ) ) { - load_textdomain( 'default', WP_LANG_DIR . "/admin-network-$locale.mo" ); - } - - return $return; -} - -/** - * Loads a plugin's translated strings. - * - * If the path is not given then it will be the root of the plugin directory. - * - * The .mo file should be named based on the text domain with a dash, and then the locale exactly. - * - * @since 1.5.0 - * @since 4.6.0 The function now tries to load the .mo file from the languages directory first. - * - * @param string $domain Unique identifier for retrieving translated strings - * @param string|false $deprecated Optional. Deprecated. Use the $plugin_rel_path parameter instead. - * Default false. - * @param string|false $plugin_rel_path Optional. Relative path to WP_PLUGIN_DIR where the .mo file resides. - * Default false. - * @return bool True when textdomain is successfully loaded, false otherwise. - */ -function load_plugin_textdomain( $domain, $deprecated = false, $plugin_rel_path = false ) { - /** - * Filters a plugin's locale. - * - * @since 3.0.0 - * - * @param string $locale The plugin's current locale. - * @param string $domain Text domain. Unique identifier for retrieving translated strings. - */ - $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 ) ) { - return true; - } - - if ( false !== $plugin_rel_path ) { - $path = WP_PLUGIN_DIR . '/' . trim( $plugin_rel_path, '/' ); - } elseif ( false !== $deprecated ) { - _deprecated_argument( __FUNCTION__, '2.7.0' ); - $path = ABSPATH . trim( $deprecated, '/' ); - } else { - $path = WP_PLUGIN_DIR; - } - - return load_textdomain( $domain, $path . '/' . $mofile ); -} - -/** - * 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. - * - * @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 = '' ) { - /** 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 ) ) { - return true; - } - - $path = WPMU_PLUGIN_DIR . '/' . ltrim( $mu_plugin_rel_path, '/' ); - - return load_textdomain( $domain, $path . '/' . $mofile ); -} - -/** - * 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. - * - * The .mo files must be named based on the locale exactly. - * - * @since 1.5.0 - * @since 4.6.0 The function now tries to load the .mo file from the languages directory first. - * - * @param string $domain Text domain. Unique identifier for retrieving translated strings. - * @param string $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 ) { - /** - * Filters a theme's locale. - * - * @since 3.0.0 - * - * @param string $locale The theme's current locale. - * @param string $domain Text domain. Unique identifier for retrieving translated strings. - */ - $locale = apply_filters( 'theme_locale', determine_locale(), $domain ); - - $mofile = $domain . '-' . $locale . '.mo'; - - // Try to load from the languages directory first. - if ( load_textdomain( $domain, WP_LANG_DIR . '/themes/' . $mofile ) ) { - return true; - } - - if ( ! $path ) { - $path = get_template_directory(); - } - - return load_textdomain( $domain, $path . '/' . $locale . '.mo' ); -} - -/** - * 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. - * - * The .mo files must be named based on the locale exactly. - * - * @since 2.9.0 - * - * @param string $domain Text domain. Unique identifier for retrieving translated strings. - * @param string $path Optional. Path to the directory containing the .mo file. - * Default false. - * @return bool True when the theme textdomain is successfully loaded, false otherwise. - */ -function load_child_theme_textdomain( $domain, $path = false ) { - if ( ! $path ) { - $path = get_stylesheet_directory(); - } - return load_theme_textdomain( $domain, $path ); -} - -/** - * Loads the script translated strings. - * - * @since 5.0.0 - * @since 5.0.2 Uses load_script_translations() to load translation data. - * @since 5.1.0 The `$domain` parameter was made optional. - * - * @see WP_Scripts::set_translations() - * - * @param string $handle Name of the script to register a translation domain to. - * @param string $domain Optional. Text domain. Default 'default'. - * @param string $path Optional. The full file path to the directory containing translation files. - * - * @return false|string False if the script textdomain could not be loaded, the translated strings - * in JSON encoding otherwise. - */ -function load_script_textdomain( $handle, $domain = 'default', $path = null ) { - $wp_scripts = wp_scripts(); - - if ( ! isset( $wp_scripts->registered[ $handle ] ) ) { - return false; - } - - $path = untrailingslashit( $path ); - $locale = determine_locale(); - - // If a path was given and the handle file exists simply return it. - $file_base = $domain === 'default' ? $locale : $domain . '-' . $locale; - $handle_filename = $file_base . '-' . $handle . '.json'; - - if ( $path ) { - $translations = load_script_translations( $path . '/' . $handle_filename, $handle, $domain ); - - if ( $translations ) { - return $translations; - } - } - - $src = $wp_scripts->registered[ $handle ]->src; - - if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $wp_scripts->content_url && 0 === strpos( $src, $wp_scripts->content_url ) ) ) { - $src = $wp_scripts->base_url . $src; - } - - $relative = false; - $languages_path = WP_LANG_DIR; - - $src_url = wp_parse_url( $src ); - $content_url = wp_parse_url( content_url() ); - $plugins_url = wp_parse_url( plugins_url() ); - $site_url = wp_parse_url( site_url() ); - - // 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( $src_url['host'] ) || $src_url['host'] === $content_url['host'] ) - ) { - // Make the src relative the specific plugin or theme. - if ( isset( $content_url['path'] ) ) { - $relative = substr( $src_url['path'], strlen( $content_url['path'] ) ); - } else { - $relative = $src_url['path']; - } - $relative = trim( $relative, '/' ); - $relative = explode( '/', $relative ); - - $languages_path = WP_LANG_DIR . '/' . $relative[0]; - - $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( $src_url['host'] ) || $src_url['host'] === $plugins_url['host'] ) - ) { - // Make the src relative the specific plugin. - if ( isset( $plugins_url['path'] ) ) { - $relative = substr( $src_url['path'], strlen( $plugins_url['path'] ) ); - } else { - $relative = $src_url['path']; - } - $relative = trim( $relative, '/' ); - $relative = explode( '/', $relative ); - - $languages_path = WP_LANG_DIR . '/plugins'; - - $relative = array_slice( $relative, 1 ); // Remove <plugin name>. - $relative = implode( '/', $relative ); - } elseif ( ! isset( $src_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 ) ) { - // Make the src relative to the WP root. - $relative = substr( $src_url['path'], strlen( $site_url['path'] ) ); - $relative = trim( $relative, '/' ); - } - } - - /** - * Filters the relative path of scripts used for finding translation files. - * - * @since 5.0.2 - * - * @param string $relative The relative path of the script. False if it could not be determined. - * @param string $src The full source url of the script. - */ - $relative = apply_filters( 'load_script_textdomain_relative_path', $relative, $src ); - - // If the source is not from WP. - if ( false === $relative ) { - return load_script_translations( false, $handle, $domain ); - } - - // Translations are always based on the unminified filename. - if ( substr( $relative, -7 ) === '.min.js' ) { - $relative = substr( $relative, 0, -7 ) . '.js'; - } - - $md5_filename = $file_base . '-' . md5( $relative ) . '.json'; - - if ( $path ) { - $translations = load_script_translations( $path . '/' . $md5_filename, $handle, $domain ); - - if ( $translations ) { - return $translations; - } - } - - $translations = load_script_translations( $languages_path . '/' . $md5_filename, $handle, $domain ); - - if ( $translations ) { - return $translations; - } - - return load_script_translations( false, $handle, $domain ); -} - -/** - * Loads the translation data for the given script handle and text domain. - * - * @since 5.0.2 - * - * @param string|false $file Path to the translation file to load. False if there isn't one. - * @param string $handle Name of the script to register a translation domain to. - * @param string $domain The text domain. - * @return string|false The JSON-encoded translated strings for the given script handle and text domain. False if there are none. - */ -function load_script_translations( $file, $handle, $domain ) { - /** - * Pre-filters script translations for the given file, script handle and text domain. - * - * Returning a non-null value allows to override the default logic, effectively short-circuiting the function. - * - * @since 5.0.2 - * - * @param string|false|null $translations JSON-encoded translation data. Default null. - * @param string|false $file Path to the translation file to load. False if there isn't one. - * @param string $handle Name of the script to register a translation domain to. - * @param string $domain The text domain. - */ - $translations = apply_filters( 'pre_load_script_translations', null, $file, $handle, $domain ); - - if ( null !== $translations ) { - return $translations; - } - - /** |
