2004-04-16 03:56:04 +02:00
< ? php
2008-01-06 02:28:34 +01:00
/**
2015-09-24 07:47:24 +02:00
* Core Translation API
2008-01-06 02:28:34 +01:00
*
* @ package WordPress
* @ subpackage i18n
2015-09-24 07:47:24 +02:00
* @ since 1.2 . 0
2008-01-06 02:28:34 +01:00
*/
/**
2015-09-24 07:50:24 +02:00
* Retrieves the current locale .
2008-01-06 02:28:34 +01:00
*
2015-09-24 07:50:24 +02:00
* If the locale is set , then it will filter the locale in the { @ see 'locale' }
* filter hook and return the value .
2008-01-06 02:28:34 +01:00
*
2008-08-30 23:23:43 +02:00
* If the locale is not set already , then the WPLANG constant is used if it is
2015-09-24 07:50:24 +02:00
* defined . Then it is filtered through the { @ see 'locale' } filter hook and
* the value for the locale global set and the locale is returned .
2008-01-06 02:28:34 +01:00
*
2011-09-30 00:57:43 +02:00
* The process to get the locale should only be done once , but the locale will
2015-09-24 07:50:24 +02:00
* always be filtered using the { @ see 'locale' } hook .
2008-01-06 02:28:34 +01:00
*
* @ since 1.5 . 0
*
2020-02-10 04:30:06 +01:00
* @ global string $locale The current locale .
* @ global string $wp_local_package Locale code of the package .
2015-05-26 20:13:25 +02:00
*
2015-09-24 07:51:23 +02:00
* @ return string The locale of the blog or from the { @ see 'locale' } hook .
2008-01-06 02:28:34 +01:00
*/
2005-01-30 00:17:10 +01:00
function get_locale () {
2016-11-01 08:38:31 +01:00
global $locale , $wp_local_package ;
2004-04-26 04:00:08 +02:00
2014-06-18 21:58:15 +02:00
if ( isset ( $locale ) ) {
2020-12-21 19:27:04 +01:00
/** This filter is documented in wp-includes/l10n.php */
2006-12-03 22:09:24 +01:00
return apply_filters ( 'locale' , $locale );
2014-06-18 21:58:15 +02:00
}
2005-01-30 00:17:10 +01:00
2014-08-26 21:59:16 +02:00
if ( isset ( $wp_local_package ) ) {
$locale = $wp_local_package ;
}
// WPLANG was defined in wp-config.
if ( defined ( 'WPLANG' ) ) {
2006-01-10 23:53:40 +01:00
$locale = WPLANG ;
2014-08-26 21:59:16 +02:00
}
2006-02-12 08:53:23 +01:00
2010-01-29 20:03:51 +01:00
// If multisite, check options.
2011-10-25 07:09:03 +02:00
if ( is_multisite () ) {
// Don't check blog option when installing.
2019-07-03 01:42:58 +02:00
if ( wp_installing () ) {
2015-10-07 19:11:25 +02:00
$ms_locale = get_site_option ( 'WPLANG' );
2019-07-03 01:42:58 +02:00
} else {
$ms_locale = get_option ( 'WPLANG' );
if ( false === $ms_locale ) {
$ms_locale = get_site_option ( 'WPLANG' );
}
2014-08-26 21:59:16 +02:00
}
2010-01-29 20:03:51 +01:00
2020-02-09 17:55:09 +01:00
if ( false !== $ms_locale ) {
2010-01-29 20:03:51 +01:00
$locale = $ms_locale ;
2014-08-26 21:59:16 +02:00
}
} else {
2014-06-18 21:58:15 +02:00
$db_locale = get_option ( 'WPLANG' );
2020-02-09 17:55:09 +01:00
if ( false !== $db_locale ) {
2014-06-18 21:58:15 +02:00
$locale = $db_locale ;
}
2010-01-29 20:03:51 +01:00
}
2014-08-26 21:59:16 +02:00
if ( empty ( $locale ) ) {
2008-05-04 19:28:42 +02:00
$locale = 'en_US' ;
2014-08-26 21:59:16 +02:00
}
2004-04-16 03:56:04 +02:00
2020-12-21 19:27:04 +01:00
/**
* Filters the locale ID of the WordPress installation .
*
* @ since 1.5 . 0
*
* @ param string $locale The locale ID .
*/
2009-11-21 10:28:32 +01:00
return apply_filters ( 'locale' , $locale );
2005-01-30 00:17:10 +01:00
}
2004-04-16 03:56:04 +02:00
2016-10-03 09:04:29 +02:00
/**
2016-10-26 16:15:32 +02:00
* Retrieves the locale of a user .
2016-10-03 09:04:29 +02:00
*
* 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
*
2022-07-15 15:45:11 +02:00
* @ param int | WP_User $user User ' s ID or a WP_User object . Defaults to current user .
2016-10-26 16:15:32 +02:00
* @ return string The locale of the user .
2016-10-03 09:04:29 +02:00
*/
2022-07-15 15:45:11 +02:00
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 );
2016-10-26 16:15:32 +02:00
}
2022-07-15 15:45:11 +02:00
if ( ! $user_object ) {
2016-10-26 16:15:32 +02:00
return get_locale ();
}
2016-10-03 09:04:29 +02:00
2022-07-15 15:45:11 +02:00
$locale = $user_object -> locale ;
2016-10-26 16:15:32 +02:00
return $locale ? $locale : get_locale ();
2016-10-03 09:04:29 +02:00
}
2018-12-14 02:32:39 +01:00
/**
2022-06-07 07:18:13 +02:00
* Determines the current locale desired for the request .
2018-12-14 02:32:39 +01:00
*
* @ since 5.0 . 0
*
2022-04-04 20:26:06 +02:00
* @ global string $pagenow The filename of the current screen .
2018-12-14 02:32:39 +01:00
*
* @ 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
*
2019-10-27 20:11:02 +01:00
* @ param string | null $locale The locale to return and short - circuit . Default null .
2018-12-14 02:32:39 +01:00
*/
$determined_locale = apply_filters ( 'pre_determine_locale' , null );
2020-06-23 12:02:10 +02:00
2023-05-26 19:57:23 +02:00
if ( $determined_locale && is_string ( $determined_locale ) ) {
2018-12-14 02:32:39 +01:00
return $determined_locale ;
}
2023-05-26 19:57:23 +02:00
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' ] );
}
2023-06-23 18:03:25 +02:00
} elseif (
is_admin () ||
( isset ( $_GET [ '_locale' ] ) && 'user' === $_GET [ '_locale' ] && wp_is_json_request () )
2023-05-26 19:57:23 +02:00
) {
2018-12-14 02:32:39 +01:00
$determined_locale = get_user_locale ();
2024-01-15 19:57:14 +01:00
} 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' ];
}
2023-06-23 18:03:25 +02:00
}
if ( ! $determined_locale ) {
2023-05-26 19:57:23 +02:00
$determined_locale = get_locale ();
2018-12-14 02:32:39 +01:00
}
/**
* Filters the locale for the current request .
*
* @ since 5.0 . 0
*
2023-12-30 12:46:24 +01:00
* @ param string $determined_locale The locale .
2018-12-14 02:32:39 +01:00
*/
return apply_filters ( 'determine_locale' , $determined_locale );
}
2008-01-06 02:28:34 +01:00
/**
2022-06-07 07:18:13 +02:00
* Retrieves the translation of $text .
2013-09-16 22:17:09 +02:00
*
2013-09-24 00:28:10 +02:00
* If there is no translation , or the text domain isn ' t loaded , the original text is returned .
2013-09-16 22:17:09 +02:00
*
2015-10-02 22:45:24 +02:00
* * Note :* Don ' t use translate () directly , use __ () or related functions .
2008-01-06 02:28:34 +01:00
*
* @ since 2.2 . 0
Docs: Update various DocBlocks and inline comments per the documentation standards.
Includes minor formatting edits for consistency.
Follow-up to [53/tests], [12179], [12946], [35288], [37884], [38810], [38928], [46596], [48131], [52955], [53548], [53813], [53873], [54118], [54316], [54420], [54421], [54803].
See #56792.
Built from https://develop.svn.wordpress.org/trunk@54855
git-svn-id: http://core.svn.wordpress.org/trunk@54407 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-11-17 19:15:19 +01:00
* @ since 5.5 . 0 Introduced `gettext-{$domain}` filter .
2008-01-06 02:28:34 +01:00
*
2013-09-16 22:17:09 +02:00
* @ param string $text Text to translate .
2013-09-24 00:28:10 +02:00
* @ param string $domain Optional . Text domain . Unique identifier for retrieving translated strings .
2015-10-02 22:45:24 +02:00
* Default 'default' .
2019-10-26 19:30:03 +02:00
* @ return string Translated text .
2008-01-06 02:28:34 +01:00
*/
2009-02-17 06:03:29 +01:00
function translate ( $text , $domain = 'default' ) {
2012-10-04 20:09:30 +02:00
$translations = get_translations_for_domain ( $domain );
2016-08-26 11:38:28 +02:00
$translation = $translations -> translate ( $text );
2014-11-24 06:47:23 +01:00
2013-09-23 02:38:12 +02:00
/**
2016-05-22 20:48:28 +02:00
* Filters text with its translation .
2013-09-23 02:38:12 +02:00
*
* @ since 2.0 . 11
*
2020-06-23 12:02:10 +02:00
* @ param string $translation Translated text .
* @ param string $text Text to translate .
* @ param string $domain Text domain . Unique identifier for retrieving translated strings .
2013-09-23 02:38:12 +02:00
*/
2020-06-23 06:43:10 +02:00
$translation = apply_filters ( 'gettext' , $translation , $text , $domain );
/**
* Filters text with its translation for a domain .
*
2021-09-21 20:21:00 +02:00
* The dynamic portion of the hook name , `$domain` , refers to the text domain .
2020-06-23 12:02:10 +02:00
*
2020-06-23 06:43:10 +02:00
* @ since 5.5 . 0
*
2020-06-23 12:02:10 +02:00
* @ param string $translation Translated text .
* @ param string $text Text to translate .
* @ param string $domain Text domain . Unique identifier for retrieving translated strings .
2020-06-23 06:43:10 +02:00
*/
2020-06-23 12:02:10 +02:00
$translation = apply_filters ( " gettext_ { $domain } " , $translation , $text , $domain );
return $translation ;
2004-04-16 03:56:04 +02:00
}
2013-09-16 22:17:09 +02:00
/**
2022-06-07 07:18:13 +02:00
* Removes last item on a pipe - delimited string .
2013-09-16 22:17:09 +02:00
*
* 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
*
Code Modernization: Rename parameters that use reserved keywords in `wp-includes/l10n.php`.
While using reserved PHP keywords as parameter name labels is allowed, in the context of function calls using named parameters in PHP 8.0+, this will easily lead to confusion. To avoid that, it is recommended not to use reserved keywords as function parameter names.
This commit renames the `$string` parameter to `$text` in `before_last_bar()`.
Follow-up to [52946], [52996], [52997], [52998], [53003], [53014], [53029], [53039], [53116], [53117], [53137], [53174], [53184], [53185], [53192], [53193], [53198], [53203], [53207], [53215], [53216], [53220], [53230], [53232], [53236], [53239], [53240], [53242], [53243], [53245], [53246], [53257], [53269], [53270], [53271], [53272], [53273], [53274], [53275], [53276], [53277], [53281], [53283], [53284], [53285], [53287], [53364], [53365], [54927], [54929], [54930], [54931], [54932], [54933].
Props jrf, aristath, poena, justinahinon, SergeyBiryukov.
See #56788.
Built from https://develop.svn.wordpress.org/trunk@54938
git-svn-id: http://core.svn.wordpress.org/trunk@54490 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-12-06 14:03:18 +01:00
* @ param string $text A pipe - delimited string .
* @ return string Either $text or everything before the last pipe .
2013-09-16 22:17:09 +02:00
*/
Code Modernization: Rename parameters that use reserved keywords in `wp-includes/l10n.php`.
While using reserved PHP keywords as parameter name labels is allowed, in the context of function calls using named parameters in PHP 8.0+, this will easily lead to confusion. To avoid that, it is recommended not to use reserved keywords as function parameter names.
This commit renames the `$string` parameter to `$text` in `before_last_bar()`.
Follow-up to [52946], [52996], [52997], [52998], [53003], [53014], [53029], [53039], [53116], [53117], [53137], [53174], [53184], [53185], [53192], [53193], [53198], [53203], [53207], [53215], [53216], [53220], [53230], [53232], [53236], [53239], [53240], [53242], [53243], [53245], [53246], [53257], [53269], [53270], [53271], [53272], [53273], [53274], [53275], [53276], [53277], [53281], [53283], [53284], [53285], [53287], [53364], [53365], [54927], [54929], [54930], [54931], [54932], [54933].
Props jrf, aristath, poena, justinahinon, SergeyBiryukov.
See #56788.
Built from https://develop.svn.wordpress.org/trunk@54938
git-svn-id: http://core.svn.wordpress.org/trunk@54490 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-12-06 14:03:18 +01:00
function before_last_bar ( $text ) {
$last_bar = strrpos ( $text , '|' );
2015-12-16 09:04:26 +01:00
if ( false === $last_bar ) {
Code Modernization: Rename parameters that use reserved keywords in `wp-includes/l10n.php`.
While using reserved PHP keywords as parameter name labels is allowed, in the context of function calls using named parameters in PHP 8.0+, this will easily lead to confusion. To avoid that, it is recommended not to use reserved keywords as function parameter names.
This commit renames the `$string` parameter to `$text` in `before_last_bar()`.
Follow-up to [52946], [52996], [52997], [52998], [53003], [53014], [53029], [53039], [53116], [53117], [53137], [53174], [53184], [53185], [53192], [53193], [53198], [53203], [53207], [53215], [53216], [53220], [53230], [53232], [53236], [53239], [53240], [53242], [53243], [53245], [53246], [53257], [53269], [53270], [53271], [53272], [53273], [53274], [53275], [53276], [53277], [53281], [53283], [53284], [53285], [53287], [53364], [53365], [54927], [54929], [54930], [54931], [54932], [54933].
Props jrf, aristath, poena, justinahinon, SergeyBiryukov.
See #56788.
Built from https://develop.svn.wordpress.org/trunk@54938
git-svn-id: http://core.svn.wordpress.org/trunk@54490 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-12-06 14:03:18 +01:00
return $text ;
2015-12-16 09:04:26 +01:00
} else {
Code Modernization: Rename parameters that use reserved keywords in `wp-includes/l10n.php`.
While using reserved PHP keywords as parameter name labels is allowed, in the context of function calls using named parameters in PHP 8.0+, this will easily lead to confusion. To avoid that, it is recommended not to use reserved keywords as function parameter names.
This commit renames the `$string` parameter to `$text` in `before_last_bar()`.
Follow-up to [52946], [52996], [52997], [52998], [53003], [53014], [53029], [53039], [53116], [53117], [53137], [53174], [53184], [53185], [53192], [53193], [53198], [53203], [53207], [53215], [53216], [53220], [53230], [53232], [53236], [53239], [53240], [53242], [53243], [53245], [53246], [53257], [53269], [53270], [53271], [53272], [53273], [53274], [53275], [53276], [53277], [53281], [53283], [53284], [53285], [53287], [53364], [53365], [54927], [54929], [54930], [54931], [54932], [54933].
Props jrf, aristath, poena, justinahinon, SergeyBiryukov.
See #56788.
Built from https://develop.svn.wordpress.org/trunk@54938
git-svn-id: http://core.svn.wordpress.org/trunk@54490 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-12-06 14:03:18 +01:00
return substr ( $text , 0 , $last_bar );
2015-12-16 09:04:26 +01:00
}
2008-11-25 23:25:21 +01:00
}
2013-09-16 22:17:09 +02:00
/**
2022-06-07 07:18:13 +02:00
* Retrieves the translation of $text in the context defined in $context .
2013-09-16 22:17:09 +02:00
*
2019-10-26 19:30:03 +02:00
* If there is no translation , or the text domain isn ' t loaded , the original text is returned .
2013-09-16 22:17:09 +02:00
*
2015-10-02 22:45:24 +02:00
* * Note :* Don ' t use translate_with_gettext_context () directly , use _x () or related functions .
*
2013-09-16 22:17:09 +02:00
* @ since 2.8 . 0
Docs: Update various DocBlocks and inline comments per the documentation standards.
Includes minor formatting edits for consistency.
Follow-up to [53/tests], [12179], [12946], [35288], [37884], [38810], [38928], [46596], [48131], [52955], [53548], [53813], [53873], [54118], [54316], [54420], [54421], [54803].
See #56792.
Built from https://develop.svn.wordpress.org/trunk@54855
git-svn-id: http://core.svn.wordpress.org/trunk@54407 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-11-17 19:15:19 +01:00
* @ since 5.5 . 0 Introduced `gettext_with_context-{$domain}` filter .
2013-09-16 22:17:09 +02:00
*
* @ param string $text Text to translate .
* @ param string $context Context information for the translators .
2013-09-24 00:28:10 +02:00
* @ param string $domain Optional . Text domain . Unique identifier for retrieving translated strings .
2015-10-02 22:45:24 +02:00
* Default 'default' .
2013-09-16 22:17:09 +02:00
* @ return string Translated text on success , original text on failure .
*/
2009-02-17 06:03:29 +01:00
function translate_with_gettext_context ( $text , $context , $domain = 'default' ) {
2012-10-04 20:09:30 +02:00
$translations = get_translations_for_domain ( $domain );
2016-08-26 11:38:28 +02:00
$translation = $translations -> translate ( $text , $context );
2020-06-23 06:43:10 +02:00
2013-09-23 02:38:12 +02:00
/**
2016-05-22 20:48:28 +02:00
* Filters text with its translation based on context information .
2013-09-23 02:38:12 +02:00
*
* @ since 2.8 . 0
*
2020-06-23 12:02:10 +02:00
* @ 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 .
2013-09-23 02:38:12 +02:00
*/
2020-06-23 06:43:10 +02:00
$translation = apply_filters ( 'gettext_with_context' , $translation , $text , $context , $domain );
/**
* Filters text with its translation based on context information for a domain .
*
2021-09-21 20:21:00 +02:00
* The dynamic portion of the hook name , `$domain` , refers to the text domain .
2020-06-23 12:02:10 +02:00
*
2020-06-23 06:43:10 +02:00
* @ since 5.5 . 0
*
2020-06-23 12:02:10 +02:00
* @ 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 .
2020-06-23 06:43:10 +02:00
*/
2020-06-23 12:02:10 +02:00
$translation = apply_filters ( " gettext_with_context_ { $domain } " , $translation , $text , $context , $domain );
return $translation ;
2009-02-17 06:03:29 +01:00
}
2008-01-06 02:28:34 +01:00
/**
2022-06-07 07:18:13 +02:00
* Retrieves the translation of $text .
2015-09-24 07:43:26 +02:00
*
* If there is no translation , or the text domain isn ' t loaded , the original text is returned .
2008-01-06 02:28:34 +01:00
*
* @ since 2.1 . 0
*
2013-09-16 22:17:09 +02:00
* @ param string $text Text to translate .
2013-09-24 00:28:10 +02:00
* @ param string $domain Optional . Text domain . Unique identifier for retrieving translated strings .
2015-09-24 07:43:26 +02:00
* Default 'default' .
2013-09-16 22:17:09 +02:00
* @ return string Translated text .
2008-01-06 02:28:34 +01:00
*/
2009-02-17 06:03:29 +01:00
function __ ( $text , $domain = 'default' ) {
return translate ( $text , $domain );
2007-04-12 23:15:44 +02:00
}
2009-04-27 23:58:04 +02:00
/**
2022-06-07 07:18:13 +02:00
* Retrieves the translation of $text and escapes it for safe use in an attribute .
2013-09-16 22:17:09 +02:00
*
2013-09-24 00:28:10 +02:00
* If there is no translation , or the text domain isn ' t loaded , the original text is returned .
2009-04-27 23:58:04 +02:00
*
* @ since 2.8 . 0
*
2013-09-16 22:17:09 +02:00
* @ param string $text Text to translate .
2013-09-24 00:28:10 +02:00
* @ param string $domain Optional . Text domain . Unique identifier for retrieving translated strings .
2015-10-02 22:45:24 +02:00
* Default 'default' .
2013-09-16 22:17:09 +02:00
* @ return string Translated text on success , original text on failure .
2009-04-27 23:58:04 +02:00
*/
2009-05-05 21:43:53 +02:00
function esc_attr__ ( $text , $domain = 'default' ) {
return esc_attr ( translate ( $text , $domain ) );
2009-04-27 23:58:04 +02:00
}
2009-05-18 17:11:07 +02:00
/**
2022-06-07 07:18:13 +02:00
* Retrieves the translation of $text and escapes it for safe use in HTML output .
2013-09-16 22:17:09 +02:00
*
2017-06-25 08:41:41 +02:00
* If there is no translation , or the text domain isn ' t loaded , the original text
2018-02-26 16:57:32 +01:00
* is escaped and returned .
2009-05-18 17:11:07 +02:00
*
* @ since 2.8 . 0
*
2013-09-16 22:17:09 +02:00
* @ param string $text Text to translate .
2013-09-24 00:28:10 +02:00
* @ param string $domain Optional . Text domain . Unique identifier for retrieving translated strings .
2015-10-02 22:45:24 +02:00
* Default 'default' .
2019-10-26 19:30:03 +02:00
* @ return string Translated text .
2009-05-18 17:11:07 +02:00
*/
function esc_html__ ( $text , $domain = 'default' ) {
return esc_html ( translate ( $text , $domain ) );
}
2008-01-06 02:28:34 +01:00
/**
2022-06-07 07:18:13 +02:00
* Displays translated text .
2008-01-06 02:28:34 +01:00
*
* @ since 1.2 . 0
*
2013-09-16 22:17:09 +02:00
* @ param string $text Text to translate .
2013-09-24 00:28:10 +02:00
* @ param string $domain Optional . Text domain . Unique identifier for retrieving translated strings .
2015-10-02 22:45:24 +02:00
* Default 'default' .
2008-01-06 02:28:34 +01:00
*/
2009-02-17 06:03:29 +01:00
function _e ( $text , $domain = 'default' ) {
echo translate ( $text , $domain );
2004-04-16 03:56:04 +02:00
}
2009-04-27 23:58:04 +02:00
/**
2022-06-07 07:18:13 +02:00
* Displays translated text that has been escaped for safe use in an attribute .
2009-04-27 23:58:04 +02:00
*
2019-10-26 19:30:03 +02:00
* Encodes `< > & " '` ( less than , greater than , ampersand , double quote , single quote ) .
* Will never double encode entities .
*
* If you need the value for use in PHP , use esc_attr__ () .
*
2009-04-27 23:58:04 +02:00
* @ since 2.8 . 0
*
2013-09-16 22:17:09 +02:00
* @ param string $text Text to translate .
2013-09-24 00:28:10 +02:00
* @ param string $domain Optional . Text domain . Unique identifier for retrieving translated strings .
2015-10-02 22:45:24 +02:00
* Default 'default' .
2009-04-27 23:58:04 +02:00
*/
2009-05-05 21:43:53 +02:00
function esc_attr_e ( $text , $domain = 'default' ) {
echo esc_attr ( translate ( $text , $domain ) );
2009-04-27 23:58:04 +02:00
}
2009-05-18 17:11:07 +02:00
/**
2022-06-07 07:18:13 +02:00
* Displays translated text that has been escaped for safe use in HTML output .
2009-05-18 17:11:07 +02:00
*
2019-10-26 19:30:03 +02:00
* If there is no translation , or the text domain isn ' t loaded , the original text
* is escaped and displayed .
*
* If you need the value for use in PHP , use esc_html__ () .
*
2009-05-18 17:11:07 +02:00
* @ since 2.8 . 0
*
2013-09-16 22:17:09 +02:00
* @ param string $text Text to translate .
2013-09-24 00:28:10 +02:00
* @ param string $domain Optional . Text domain . Unique identifier for retrieving translated strings .
2015-10-02 22:45:24 +02:00
* Default 'default' .
2009-05-18 17:11:07 +02:00
*/
function esc_html_e ( $text , $domain = 'default' ) {
echo esc_html ( translate ( $text , $domain ) );
}
2008-01-06 02:28:34 +01:00
/**
2022-06-07 07:18:13 +02:00
* Retrieves translated string with gettext context .
2008-01-06 02:28:34 +01:00
*
2008-08-30 23:23:43 +02:00
* Quite a few times , there will be collisions with similar translatable text
2013-09-16 22:17:09 +02:00
* found in more than two places , but with different translated context .
2008-01-06 02:28:34 +01:00
*
2013-09-16 22:17:09 +02:00
* By including the context in the pot file , translators can translate the two
2011-09-30 00:57:43 +02:00
* strings differently .
2008-01-06 02:28:34 +01:00
*
2010-02-13 08:28:19 +01:00
* @ since 2.8 . 0
2008-01-06 02:28:34 +01:00
*
2013-09-16 22:17:09 +02:00
* @ param string $text Text to translate .
* @ param string $context Context information for the translators .
2013-09-24 00:28:10 +02:00
* @ param string $domain Optional . Text domain . Unique identifier for retrieving translated strings .
2015-10-02 22:45:24 +02:00
* Default 'default' .
2013-09-16 22:17:09 +02:00
* @ return string Translated context string without pipe .
2008-01-06 02:28:34 +01:00
*/
2011-05-14 11:50:20 +02:00
function _x ( $text , $context , $domain = 'default' ) {
return translate_with_gettext_context ( $text , $context , $domain );
2009-02-17 06:03:29 +01:00
}
2010-05-14 23:46:25 +02:00
/**
2022-06-07 07:18:13 +02:00
* Displays translated string with gettext context .
2010-05-26 04:42:15 +02:00
*
2010-05-14 23:46:25 +02:00
* @ since 3.0 . 0
*
2013-09-16 22:17:09 +02:00
* @ param string $text Text to translate .
* @ param string $context Context information for the translators .
2013-09-24 00:28:10 +02:00
* @ param string $domain Optional . Text domain . Unique identifier for retrieving translated strings .
2015-10-02 22:45:24 +02:00
* Default 'default' .
2010-05-14 23:46:25 +02:00
*/
2011-05-14 11:50:20 +02:00
function _ex ( $text , $context , $domain = 'default' ) {
echo _x ( $text , $context , $domain );
2010-05-14 23:46:25 +02:00
}
2013-07-28 23:26:10 +02:00
/**
2022-06-07 07:18:13 +02:00
* Translates string with gettext context , and escapes it for safe use in an attribute .
2013-07-28 23:26:10 +02:00
*
2019-10-26 19:30:03 +02:00
* If there is no translation , or the text domain isn ' t loaded , the original text
* is escaped and returned .
*
2013-07-28 23:26:10 +02:00
* @ since 2.8 . 0
*
2013-09-16 22:17:09 +02:00
* @ param string $text Text to translate .
* @ param string $context Context information for the translators .
2013-09-24 00:28:10 +02:00
* @ param string $domain Optional . Text domain . Unique identifier for retrieving translated strings .
2015-10-02 22:45:24 +02:00
* Default 'default' .
2019-10-26 19:30:03 +02:00
* @ return string Translated text .
2013-07-28 23:26:10 +02:00
*/
function esc_attr_x ( $text , $context , $domain = 'default' ) {
return esc_attr ( translate_with_gettext_context ( $text , $context , $domain ) );
2009-04-27 23:58:04 +02:00
}
2013-07-28 23:26:10 +02:00
/**
2022-06-07 07:18:13 +02:00
* Translates string with gettext context , and escapes it for safe use in HTML output .
2013-07-28 23:26:10 +02:00
*
2019-10-26 19:30:03 +02:00
* If there is no translation , or the text domain isn ' t loaded , the original text
* is escaped and returned .
*
2013-07-28 23:26:10 +02:00
* @ since 2.9 . 0
*
2013-09-16 22:17:09 +02:00
* @ param string $text Text to translate .
* @ param string $context Context information for the translators .
2013-09-24 00:28:10 +02:00
* @ param string $domain Optional . Text domain . Unique identifier for retrieving translated strings .
2015-10-02 22:45:24 +02:00
* Default 'default' .
2013-09-16 22:17:09 +02:00
* @ return string Translated text .
2013-07-28 23:26:10 +02:00
*/
function esc_html_x ( $text , $context , $domain = 'default' ) {
return esc_html ( translate_with_gettext_context ( $text , $context , $domain ) );
2009-11-19 23:49:16 +01:00
}
2008-01-06 02:28:34 +01:00
/**
2015-10-02 22:45:24 +02:00
* Translates and retrieves the singular or plural form based on the supplied number .
2008-01-06 02:28:34 +01:00
*
2015-10-02 22:45:24 +02:00
* Used when you want to use the appropriate form of a string based on whether a
* number is singular or plural .
*
* Example :
2008-01-06 02:28:34 +01:00
*
2016-08-26 11:38:28 +02:00
* printf ( _n ( '%s person' , '%s people' , $count , 'text-domain' ), number_format_i18n ( $count ) );
2008-01-06 02:28:34 +01:00
*
2010-02-13 08:28:19 +01:00
* @ since 2.8 . 0
Docs: Update various DocBlocks and inline comments per the documentation standards.
Includes minor formatting edits for consistency.
Follow-up to [53/tests], [12179], [12946], [35288], [37884], [38810], [38928], [46596], [48131], [52955], [53548], [53813], [53873], [54118], [54316], [54420], [54421], [54803].
See #56792.
Built from https://develop.svn.wordpress.org/trunk@54855
git-svn-id: http://core.svn.wordpress.org/trunk@54407 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-11-17 19:15:19 +01:00
* @ since 5.5 . 0 Introduced `ngettext-{$domain}` filter .
2013-09-16 22:17:09 +02:00
*
2015-10-02 22:45:24 +02:00
* @ 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 .
2013-09-24 00:28:10 +02:00
* @ param string $domain Optional . Text domain . Unique identifier for retrieving translated strings .
2015-10-02 22:45:24 +02:00
* Default 'default' .
* @ return string The translated singular or plural form .
2008-01-06 02:28:34 +01:00
*/
2009-11-21 10:28:32 +01:00
function _n ( $single , $plural , $number , $domain = 'default' ) {
2012-10-04 20:09:30 +02:00
$translations = get_translations_for_domain ( $domain );
2016-08-26 11:38:28 +02:00
$translation = $translations -> translate_plural ( $single , $plural , $number );
2015-10-02 22:45:24 +02:00
2013-09-23 02:38:12 +02:00
/**
2016-05-22 20:48:28 +02:00
* Filters the singular or plural form of a string .
2013-09-23 02:38:12 +02:00
*
* @ since 2.2 . 0
*
* @ param string $translation Translated text .
2015-10-02 22:45:24 +02:00
* @ 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 .
2022-07-10 02:07:16 +02:00
* @ param int $number The number to compare against to use either the singular or plural form .
2013-09-23 02:38:12 +02:00
* @ param string $domain Text domain . Unique identifier for retrieving translated strings .
*/
2020-06-23 06:43:10 +02:00
$translation = apply_filters ( 'ngettext' , $translation , $single , $plural , $number , $domain );
/**
* Filters the singular or plural form of a string for a domain .
*
2021-09-21 20:21:00 +02:00
* The dynamic portion of the hook name , `$domain` , refers to the text domain .
2020-06-23 12:02:10 +02:00
*
2020-06-23 06:43:10 +02:00
* @ since 5.5 . 0
*
* @ param string $translation Translated text .
* @ param string $single The text to be used if the number is singular .
* @ param string $plural The text to be used if the number is plural .
2022-07-10 02:07:16 +02:00
* @ param int $number The number to compare against to use either the singular or plural form .
2020-06-23 12:02:10 +02:00
* @ param string $domain Text domain . Unique identifier for retrieving translated strings .
2020-06-23 06:43:10 +02:00
*/
2020-06-23 12:02:10 +02:00
$translation = apply_filters ( " ngettext_ { $domain } " , $translation , $single , $plural , $number , $domain );
return $translation ;
2004-10-18 04:27:35 +02:00
}
2008-11-25 23:25:21 +01:00
/**
2015-10-02 22:45:24 +02:00
* Translates and retrieves the singular or plural form based on the supplied number , with gettext context .
2009-12-19 11:51:29 +01:00
*
2015-10-02 22:45:24 +02:00
* 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 .
*
2016-08-26 11:38:28 +02:00
* Example of a generic phrase which is disambiguated via the context parameter :
2015-10-02 22:45:24 +02:00
*
2016-08-26 11:38:28 +02:00
* 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 ) );
2008-11-25 23:25:21 +01:00
*
2013-09-16 22:17:09 +02:00
* @ since 2.8 . 0
Docs: Update various DocBlocks and inline comments per the documentation standards.
Includes minor formatting edits for consistency.
Follow-up to [53/tests], [12179], [12946], [35288], [37884], [38810], [38928], [46596], [48131], [52955], [53548], [53813], [53873], [54118], [54316], [54420], [54421], [54803].
See #56792.
Built from https://develop.svn.wordpress.org/trunk@54855
git-svn-id: http://core.svn.wordpress.org/trunk@54407 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-11-17 19:15:19 +01:00
* @ since 5.5 . 0 Introduced `ngettext_with_context-{$domain}` filter .
2013-09-16 22:17:09 +02:00
*
2015-10-02 22:45:24 +02:00
* @ 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 .
2013-09-16 22:17:09 +02:00
* @ param string $context Context information for the translators .
2013-09-24 00:28:10 +02:00
* @ param string $domain Optional . Text domain . Unique identifier for retrieving translated strings .
2015-10-02 22:45:24 +02:00
* Default 'default' .
* @ return string The translated singular or plural form .
2008-11-25 23:25:21 +01:00
*/
2017-12-01 00:11:00 +01:00
function _nx ( $single , $plural , $number , $context , $domain = 'default' ) {
2012-10-04 20:09:30 +02:00
$translations = get_translations_for_domain ( $domain );
2016-08-26 11:38:28 +02:00
$translation = $translations -> translate_plural ( $single , $plural , $number , $context );
2015-10-02 22:45:24 +02:00
2013-09-23 02:38:12 +02:00
/**
2016-05-22 20:48:28 +02:00
* Filters the singular or plural form of a string with gettext context .
2013-09-23 02:38:12 +02:00
*
* @ since 2.8 . 0
*
* @ param string $translation Translated text .
2015-10-02 22:45:24 +02:00
* @ 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 .
2022-07-10 02:07:16 +02:00
* @ param int $number The number to compare against to use either the singular or plural form .
2013-09-23 02:38:12 +02:00
* @ param string $context Context information for the translators .
* @ param string $domain Text domain . Unique identifier for retrieving translated strings .
*/
2020-06-23 06:43:10 +02:00
$translation = apply_filters ( 'ngettext_with_context' , $translation , $single , $plural , $number , $context , $domain );
/**
* Filters the singular or plural form of a string with gettext context for a domain .
*
2021-09-21 20:21:00 +02:00
* The dynamic portion of the hook name , `$domain` , refers to the text domain .
2020-06-23 12:02:10 +02:00
*
2020-06-23 06:43:10 +02:00
* @ since 5.5 . 0
*
* @ param string $translation Translated text .
* @ param string $single The text to be used if the number is singular .
* @ param string $plural The text to be used if the number is plural .
2022-07-10 02:07:16 +02:00
* @ param int $number The number to compare against to use either the singular or plural form .
2020-06-23 06:43:10 +02:00
* @ param string $context Context information for the translators .
2020-06-23 12:02:10 +02:00
* @ param string $domain Text domain . Unique identifier for retrieving translated strings .
2020-06-23 06:43:10 +02:00
*/
2020-06-23 12:02:10 +02:00
$translation = apply_filters ( " ngettext_with_context_ { $domain } " , $translation , $single , $plural , $number , $context , $domain );
return $translation ;
2008-11-25 23:25:21 +01:00
}
2008-03-19 17:00:09 +01:00
/**
2016-02-28 21:43:26 +01:00
* Registers plural strings in POT file , but does not translate them .
2008-03-19 17:00:09 +01:00
*
2013-09-16 22:17:09 +02:00
* Used when you want to keep structures with translatable plural
2015-10-02 22:45:24 +02:00
* strings and use them later when the number is known .
2008-03-19 17:00:09 +01:00
*
* Example :
2014-11-24 06:47:23 +01:00
*
2016-08-26 11:38:28 +02:00
* $message = _n_noop ( '%s post' , '%s posts' , 'text-domain' );
2014-11-24 06:47:23 +01:00
* ...
2016-08-26 11:38:28 +02:00
* printf ( translate_nooped_plural ( $message , $count , 'text-domain' ), number_format_i18n ( $count ) );
2013-09-16 22:17:09 +02:00
*
* @ since 2.5 . 0
*
2015-10-02 22:45:24 +02:00
* @ param string $singular Singular form to be localized .
* @ param string $plural Plural form to be localized .
2013-09-24 00:28:10 +02:00
* @ param string $domain Optional . Text domain . Unique identifier for retrieving translated strings .
2015-10-02 22:45:24 +02:00
* Default null .
* @ return array {
* Array of translation information for the strings .
*
2022-06-21 00:57:16 +02:00
* @ 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 .
2015-10-02 22:45:24 +02:00
* }
2008-03-19 17:00:09 +01:00
*/
2012-04-30 18:18:35 +02:00
function _n_noop ( $singular , $plural , $domain = null ) {
2017-12-01 00:11:00 +01:00
return array (
0 => $singular ,
1 => $plural ,
'singular' => $singular ,
'plural' => $plural ,
'context' => null ,
'domain' => $domain ,
);
2008-11-25 23:25:21 +01:00
}
2009-03-13 04:53:39 +01:00
/**
2016-02-28 21:43:26 +01:00
* Registers plural strings with gettext context in POT file , but does not translate them .
2015-10-02 22:45:24 +02:00
*
* Used when you want to keep structures with translatable plural
* strings and use them later when the number is known .
*
2016-08-26 11:38:28 +02:00
* Example of a generic phrase which is disambiguated via the context parameter :
2015-10-02 22:45:24 +02:00
*
* $messages = array (
2017-12-01 00:11:00 +01:00
* 'people' => _nx_noop ( '%s group' , '%s groups' , 'people' , 'text-domain' ),
* 'animals' => _nx_noop ( '%s group' , '%s groups' , 'animals' , 'text-domain' ),
2015-10-02 22:45:24 +02:00
* );
* ...
* $message = $messages [ $type ];
2016-08-26 11:38:28 +02:00
* printf ( translate_nooped_plural ( $message , $count , 'text-domain' ), number_format_i18n ( $count ) );
2009-03-18 03:43:45 +01:00
*
2013-09-16 22:17:09 +02:00
* @ since 2.8 . 0
2015-10-02 22:45:24 +02:00
*
* @ param string $singular Singular form to be localized .
* @ param string $plural Plural form to be localized .
2015-12-19 00:16:26 +01:00
* @ param string $context Context information for the translators .
2015-10-02 22:45:24 +02:00
* @ param string $domain Optional . Text domain . Unique identifier for retrieving translated strings .
* Default null .
* @ return array {
* Array of translation information for the strings .
*
2021-12-07 13:20:02 +01:00
* @ 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 | null $domain Text domain .
2015-10-02 22:45:24 +02:00
* }
2009-03-13 04:53:39 +01:00
*/
2012-04-30 18:18:35 +02:00
function _nx_noop ( $singular , $plural , $context , $domain = null ) {
2017-12-01 00:11:00 +01:00
return array (
0 => $singular ,
1 => $plural ,
2 => $context ,
'singular' => $singular ,
'plural' => $plural ,
'context' => $context ,
'domain' => $domain ,
);
2010-10-29 15:12:14 +02:00
}
/**
2022-06-21 00:57:16 +02:00
* Translates and returns the singular or plural form of a string that ' s been registered
2015-10-02 22:45:24 +02:00
* with _n_noop () or _nx_noop () .
*
* Used when you want to use a translatable plural string once the number is known .
*
* Example :
*
2016-08-26 11:38:28 +02:00
* $message = _n_noop ( '%s post' , '%s posts' , 'text-domain' );
2015-10-02 22:45:24 +02:00
* ...
2016-08-26 11:38:28 +02:00
* printf ( translate_nooped_plural ( $message , $count , 'text-domain' ), number_format_i18n ( $count ) );
2013-09-16 22:17:09 +02:00
*
* @ since 3.1 . 0
2010-11-17 19:47:34 +01:00
*
2022-06-21 00:57:16 +02:00
* @ 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 .
* }
2015-10-02 22:45:24 +02:00
* @ param int $count Number of objects .
2013-09-24 00:28:10 +02:00
* @ param string $domain Optional . Text domain . Unique identifier for retrieving translated strings . If $nooped_plural contains
2015-10-02 22:45:24 +02:00
* a text domain passed to _n_noop () or _nx_noop (), it will override this value . Default 'default' .
2022-06-21 00:57:16 +02:00
* @ return string Either $singular or $plural translated text .
2010-10-29 15:12:14 +02:00
*/
function translate_nooped_plural ( $nooped_plural , $count , $domain = 'default' ) {
2017-12-01 00:11:00 +01:00
if ( $nooped_plural [ 'domain' ] ) {
2012-04-30 18:18:35 +02:00
$domain = $nooped_plural [ 'domain' ];
2017-12-01 00:11:00 +01:00
}
2012-04-30 18:18:35 +02:00
2017-12-01 00:11:00 +01:00
if ( $nooped_plural [ 'context' ] ) {
2010-10-29 15:12:14 +02:00
return _nx ( $nooped_plural [ 'singular' ], $nooped_plural [ 'plural' ], $count , $nooped_plural [ 'context' ], $domain );
2017-12-01 00:11:00 +01:00
} else {
2010-10-29 15:12:14 +02:00
return _n ( $nooped_plural [ 'singular' ], $nooped_plural [ 'plural' ], $count , $domain );
2017-12-01 00:11:00 +01:00
}
2009-03-13 04:53:39 +01:00
}
2008-01-06 02:28:34 +01:00
/**
2022-06-07 07:18:13 +02:00
* Loads a . mo file into the text domain $domain .
2008-01-06 02:28:34 +01:00
*
2013-09-24 00:28:10 +02:00
* If the text domain already exists , the translations will be merged . If both
2009-07-01 22:05:14 +02:00
* sets have the same string , the translation from the original value will be taken .
2008-01-06 02:28:34 +01:00
*
2009-05-11 23:15:05 +02:00
* On success , the . mo file will be placed in the $l10n global by $domain
2009-07-01 22:05:14 +02:00
* and will be a MO object .
2008-01-06 02:28:34 +01:00
*
* @ since 1.5 . 0
2022-08-11 14:39:12 +02:00
* @ since 6.1 . 0 Added the `$locale` parameter .
2008-01-06 02:28:34 +01:00
*
2022-08-11 14:39:12 +02:00
* @ 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 .
2015-05-26 20:13:25 +02:00
*
2013-09-24 00:28:10 +02:00
* @ param string $domain Text domain . Unique identifier for retrieving translated strings .
2013-09-16 22:17:09 +02:00
* @ param string $mofile Path to the . mo file .
2022-08-11 14:39:12 +02:00
* @ param string $locale Optional . Locale . Default is the current locale .
2013-09-16 22:17:09 +02:00
* @ return bool True on success , false on failure .
2008-01-06 02:28:34 +01:00
*/
2022-08-11 14:39:12 +02:00
function load_textdomain ( $domain , $mofile , $locale = null ) {
/** @var WP_Textdomain_Registry $wp_textdomain_registry */
global $l10n , $l10n_unloaded , $wp_textdomain_registry ;
2016-06-26 16:26:29 +02:00
$l10n_unloaded = ( array ) $l10n_unloaded ;
2010-01-15 23:11:12 +01:00
2024-04-04 15:56:13 +02:00
if ( ! is_string ( $domain ) ) {
return false ;
}
2023-06-16 14:58:24 +02:00
/**
* 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 ;
}
2013-09-23 02:38:12 +02:00
/**
2016-05-22 20:48:28 +02:00
* Filters whether to override the . mo file loading .
2013-09-23 02:38:12 +02:00
*
* @ since 2.9 . 0
2023-02-02 22:31:18 +01:00
* @ since 6.2 . 0 Added the `$locale` parameter .
2013-09-23 02:38:12 +02:00
*
2023-02-02 22:31:18 +01:00
* @ 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 .
2013-09-23 02:38:12 +02:00
*/
2023-02-02 22:31:18 +01:00
$plugin_override = apply_filters ( 'override_load_textdomain' , false , $domain , $mofile , $locale );
2010-01-15 23:11:12 +01:00
2020-06-23 12:02:10 +02:00
if ( true === ( bool ) $plugin_override ) {
2016-06-26 16:26:29 +02:00
unset ( $l10n_unloaded [ $domain ] );
2009-11-21 10:28:32 +01:00
return true ;
}
2010-01-15 23:11:12 +01:00
2013-09-23 02:38:12 +02:00
/**
* 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 .
*/
2009-11-21 10:28:32 +01:00
do_action ( 'load_textdomain' , $domain , $mofile );
2010-01-15 23:11:12 +01:00
2013-09-23 02:38:12 +02:00
/**
2016-05-22 20:48:28 +02:00
* Filters MO file path for loading translations for a specific text domain .
2013-09-23 02:38:12 +02:00
*
* @ since 2.9 . 0
*
* @ param string $mofile Path to the MO file .
* @ param string $domain Text domain . Unique identifier for retrieving translated strings .
*/
2009-11-21 10:28:32 +01:00
$mofile = apply_filters ( 'load_textdomain_mofile' , $mofile , $domain );
2004-10-18 04:27:35 +02:00
2022-08-11 14:39:12 +02:00
if ( ! $locale ) {
$locale = determine_locale ();
}
2024-01-25 08:53:17 +01:00
$i18n_controller = WP_Translation_Controller :: get_instance ();
2022-08-11 14:39:12 +02:00
I18N: Introduce a more performant localization library.
This introduces a more lightweight library for loading `.mo` translation files which offers increased speed and lower memory usage.
It also supports loading multiple locales at the same time, which makes locale switching faster too.
For plugins interacting with the `$l10n` global variable in core, a shim is added to retain backward compatibility with the existing `pomo` library.
In addition to that, this library supports translations contained in PHP files, avoiding a binary file format and leveraging OPCache if available.
If an `.mo` translation file has a corresponding `.l10n.php` file, the latter will be loaded instead.
This behavior can be adjusted using the new `translation_file_format` and `load_translation_file` filters.
PHP translation files will be typically created by downloading language packs, but can also be generated by plugins.
See https://make.wordpress.org/core/2023/11/08/merging-performant-translations-into-core/ for more context.
Props dd32, swissspidy, flixos90, joemcgill, westonruter, akirk, SergeyBiryukov.
Fixes #59656.
Built from https://develop.svn.wordpress.org/trunk@57337
git-svn-id: http://core.svn.wordpress.org/trunk@56843 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-01-23 14:34:11 +01:00
// 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' ;
2017-12-01 00:11:00 +01:00
}
2008-08-05 23:28:50 +02:00
2024-02-01 20:05:13 +01:00
$translation_files = array ();
I18N: Introduce a more performant localization library.
This introduces a more lightweight library for loading `.mo` translation files which offers increased speed and lower memory usage.
It also supports loading multiple locales at the same time, which makes locale switching faster too.
For plugins interacting with the `$l10n` global variable in core, a shim is added to retain backward compatibility with the existing `pomo` library.
In addition to that, this library supports translations contained in PHP files, avoiding a binary file format and leveraging OPCache if available.
If an `.mo` translation file has a corresponding `.l10n.php` file, the latter will be loaded instead.
This behavior can be adjusted using the new `translation_file_format` and `load_translation_file` filters.
PHP translation files will be typically created by downloading language packs, but can also be generated by plugins.
See https://make.wordpress.org/core/2023/11/08/merging-performant-translations-into-core/ for more context.
Props dd32, swissspidy, flixos90, joemcgill, westonruter, akirk, SergeyBiryukov.
Fixes #59656.
Built from https://develop.svn.wordpress.org/trunk@57337
git-svn-id: http://core.svn.wordpress.org/trunk@56843 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-01-23 14:34:11 +01:00
if ( 'mo' !== $preferred_format ) {
2024-02-01 20:05:13 +01:00
$translation_files [] = substr_replace ( $mofile , " .l10n. $preferred_format " , - strlen ( '.mo' ) );
2017-12-01 00:11:00 +01:00
}
2009-03-18 03:43:45 +01:00
2024-02-01 20:05:13 +01:00
$translation_files [] = $mofile ;
I18N: Introduce a more performant localization library.
This introduces a more lightweight library for loading `.mo` translation files which offers increased speed and lower memory usage.
It also supports loading multiple locales at the same time, which makes locale switching faster too.
For plugins interacting with the `$l10n` global variable in core, a shim is added to retain backward compatibility with the existing `pomo` library.
In addition to that, this library supports translations contained in PHP files, avoiding a binary file format and leveraging OPCache if available.
If an `.mo` translation file has a corresponding `.l10n.php` file, the latter will be loaded instead.
This behavior can be adjusted using the new `translation_file_format` and `load_translation_file` filters.
PHP translation files will be typically created by downloading language packs, but can also be generated by plugins.
See https://make.wordpress.org/core/2023/11/08/merging-performant-translations-into-core/ for more context.
Props dd32, swissspidy, flixos90, joemcgill, westonruter, akirk, SergeyBiryukov.
Fixes #59656.
Built from https://develop.svn.wordpress.org/trunk@57337
git-svn-id: http://core.svn.wordpress.org/trunk@56843 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-01-23 14:34:11 +01:00
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
2024-05-04 19:27:09 +02:00
* @ since 6.6 . 0 Added the `$locale` parameter .
I18N: Introduce a more performant localization library.
This introduces a more lightweight library for loading `.mo` translation files which offers increased speed and lower memory usage.
It also supports loading multiple locales at the same time, which makes locale switching faster too.
For plugins interacting with the `$l10n` global variable in core, a shim is added to retain backward compatibility with the existing `pomo` library.
In addition to that, this library supports translations contained in PHP files, avoiding a binary file format and leveraging OPCache if available.
If an `.mo` translation file has a corresponding `.l10n.php` file, the latter will be loaded instead.
This behavior can be adjusted using the new `translation_file_format` and `load_translation_file` filters.
PHP translation files will be typically created by downloading language packs, but can also be generated by plugins.
See https://make.wordpress.org/core/2023/11/08/merging-performant-translations-into-core/ for more context.
Props dd32, swissspidy, flixos90, joemcgill, westonruter, akirk, SergeyBiryukov.
Fixes #59656.
Built from https://develop.svn.wordpress.org/trunk@57337
git-svn-id: http://core.svn.wordpress.org/trunk@56843 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-01-23 14:34:11 +01:00
*
* @ param string $file Path to the translation file to load .
* @ param string $domain The text domain .
2024-05-04 19:27:09 +02:00
* @ param string $locale The locale .
I18N: Introduce a more performant localization library.
This introduces a more lightweight library for loading `.mo` translation files which offers increased speed and lower memory usage.
It also supports loading multiple locales at the same time, which makes locale switching faster too.
For plugins interacting with the `$l10n` global variable in core, a shim is added to retain backward compatibility with the existing `pomo` library.
In addition to that, this library supports translations contained in PHP files, avoiding a binary file format and leveraging OPCache if available.
If an `.mo` translation file has a corresponding `.l10n.php` file, the latter will be loaded instead.
This behavior can be adjusted using the new `translation_file_format` and `load_translation_file` filters.
PHP translation files will be typically created by downloading language packs, but can also be generated by plugins.
See https://make.wordpress.org/core/2023/11/08/merging-performant-translations-into-core/ for more context.
Props dd32, swissspidy, flixos90, joemcgill, westonruter, akirk, SergeyBiryukov.
Fixes #59656.
Built from https://develop.svn.wordpress.org/trunk@57337
git-svn-id: http://core.svn.wordpress.org/trunk@56843 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-01-23 14:34:11 +01:00
*/
2024-05-04 19:27:09 +02:00
$file = ( string ) apply_filters ( 'load_translation_file' , $file , $domain , $locale );
I18N: Introduce a more performant localization library.
This introduces a more lightweight library for loading `.mo` translation files which offers increased speed and lower memory usage.
It also supports loading multiple locales at the same time, which makes locale switching faster too.
For plugins interacting with the `$l10n` global variable in core, a shim is added to retain backward compatibility with the existing `pomo` library.
In addition to that, this library supports translations contained in PHP files, avoiding a binary file format and leveraging OPCache if available.
If an `.mo` translation file has a corresponding `.l10n.php` file, the latter will be loaded instead.
This behavior can be adjusted using the new `translation_file_format` and `load_translation_file` filters.
PHP translation files will be typically created by downloading language packs, but can also be generated by plugins.
See https://make.wordpress.org/core/2023/11/08/merging-performant-translations-into-core/ for more context.
Props dd32, swissspidy, flixos90, joemcgill, westonruter, akirk, SergeyBiryukov.
Fixes #59656.
Built from https://develop.svn.wordpress.org/trunk@57337
git-svn-id: http://core.svn.wordpress.org/trunk@56843 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-01-23 14:34:11 +01:00
$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 );
2016-06-26 16:26:29 +02:00
I18N: Introduce a more performant localization library.
This introduces a more lightweight library for loading `.mo` translation files which offers increased speed and lower memory usage.
It also supports loading multiple locales at the same time, which makes locale switching faster too.
For plugins interacting with the `$l10n` global variable in core, a shim is added to retain backward compatibility with the existing `pomo` library.
In addition to that, this library supports translations contained in PHP files, avoiding a binary file format and leveraging OPCache if available.
If an `.mo` translation file has a corresponding `.l10n.php` file, the latter will be loaded instead.
This behavior can be adjusted using the new `translation_file_format` and `load_translation_file` filters.
PHP translation files will be typically created by downloading language packs, but can also be generated by plugins.
See https://make.wordpress.org/core/2023/11/08/merging-performant-translations-into-core/ for more context.
Props dd32, swissspidy, flixos90, joemcgill, westonruter, akirk, SergeyBiryukov.
Fixes #59656.
Built from https://develop.svn.wordpress.org/trunk@57337
git-svn-id: http://core.svn.wordpress.org/trunk@56843 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-01-23 14:34:11 +01:00
$wp_textdomain_registry -> set ( $domain , $locale , dirname ( $file ) );
2010-01-15 23:11:12 +01:00
I18N: Introduce a more performant localization library.
This introduces a more lightweight library for loading `.mo` translation files which offers increased speed and lower memory usage.
It also supports loading multiple locales at the same time, which makes locale switching faster too.
For plugins interacting with the `$l10n` global variable in core, a shim is added to retain backward compatibility with the existing `pomo` library.
In addition to that, this library supports translations contained in PHP files, avoiding a binary file format and leveraging OPCache if available.
If an `.mo` translation file has a corresponding `.l10n.php` file, the latter will be loaded instead.
This behavior can be adjusted using the new `translation_file_format` and `load_translation_file` filters.
PHP translation files will be typically created by downloading language packs, but can also be generated by plugins.
See https://make.wordpress.org/core/2023/11/08/merging-performant-translations-into-core/ for more context.
Props dd32, swissspidy, flixos90, joemcgill, westonruter, akirk, SergeyBiryukov.
Fixes #59656.
Built from https://develop.svn.wordpress.org/trunk@57337
git-svn-id: http://core.svn.wordpress.org/trunk@56843 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-01-23 14:34:11 +01:00
return true ;
}
}
2022-08-11 14:39:12 +02:00
2024-02-01 20:05:13 +01:00
return false ;
2004-10-18 04:27:35 +02:00
}
2010-02-23 12:14:35 +01:00
/**
2022-06-07 07:18:13 +02:00
* Unloads translations for a text domain .
2010-03-17 17:27:25 +01:00
*
2010-02-23 12:14:35 +01:00
* @ since 3.0 . 0
2022-08-11 14:39:12 +02:00
* @ since 6.1 . 0 Added the `$reloadable` parameter .
2013-09-16 22:17:09 +02:00
*
2018-03-25 21:33:31 +02:00
* @ 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 .
2015-05-26 20:13:25 +02:00
*
2022-08-11 14:39:12 +02:00
* @ 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 .
2013-09-16 22:17:09 +02:00
* @ return bool Whether textdomain was unloaded .
2010-02-23 12:14:35 +01:00
*/
2022-08-11 14:39:12 +02:00
function unload_textdomain ( $domain , $reloadable = false ) {
2016-06-26 16:26:29 +02:00
global $l10n , $l10n_unloaded ;
$l10n_unloaded = ( array ) $l10n_unloaded ;
2010-02-23 12:14:35 +01:00
2013-09-23 02:38:12 +02:00
/**
2016-05-22 20:48:28 +02:00
* Filters whether to override the text domain unloading .
2013-09-23 02:38:12 +02:00
*
* @ since 3.0 . 0
2022-08-11 14:39:12 +02:00
* @ since 6.1 . 0 Added the `$reloadable` parameter .
2013-09-23 02:38:12 +02:00
*
2022-08-11 14:39:12 +02:00
* @ 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 .
2013-09-23 02:38:12 +02:00
*/
2022-08-11 14:39:12 +02:00
$plugin_override = apply_filters ( 'override_unload_textdomain' , false , $domain , $reloadable );
2010-02-23 12:14:35 +01:00
2016-06-26 16:26:29 +02:00
if ( $plugin_override ) {
2022-08-11 14:39:12 +02:00
if ( ! $reloadable ) {
$l10n_unloaded [ $domain ] = true ;
}
2016-06-26 16:26:29 +02:00
2010-02-23 12:14:35 +01:00
return true ;
2016-06-26 16:26:29 +02:00
}
2010-02-23 12:14:35 +01:00
2013-09-23 02:38:12 +02:00
/**
* Fires before the text domain is unloaded .
*
* @ since 3.0 . 0
2022-08-11 14:39:12 +02:00
* @ since 6.1 . 0 Added the `$reloadable` parameter .
2013-09-23 02:38:12 +02:00
*
2022-08-11 14:39:12 +02:00
* @ 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 .
2013-09-23 02:38:12 +02:00
*/
2022-08-11 14:39:12 +02:00
do_action ( 'unload_textdomain' , $domain , $reloadable );
2010-02-23 12:14:35 +01:00
I18N: Introduce a more performant localization library.
This introduces a more lightweight library for loading `.mo` translation files which offers increased speed and lower memory usage.
It also supports loading multiple locales at the same time, which makes locale switching faster too.
For plugins interacting with the `$l10n` global variable in core, a shim is added to retain backward compatibility with the existing `pomo` library.
In addition to that, this library supports translations contained in PHP files, avoiding a binary file format and leveraging OPCache if available.
If an `.mo` translation file has a corresponding `.l10n.php` file, the latter will be loaded instead.
This behavior can be adjusted using the new `translation_file_format` and `load_translation_file` filters.
PHP translation files will be typically created by downloading language packs, but can also be generated by plugins.
See https://make.wordpress.org/core/2023/11/08/merging-performant-translations-into-core/ for more context.
Props dd32, swissspidy, flixos90, joemcgill, westonruter, akirk, SergeyBiryukov.
Fixes #59656.
Built from https://develop.svn.wordpress.org/trunk@57337
git-svn-id: http://core.svn.wordpress.org/trunk@56843 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-01-23 14:34:11 +01:00
// Since multiple locales are supported, reloadable text domains don't actually need to be unloaded.
if ( ! $reloadable ) {
2024-01-25 08:53:17 +01:00
WP_Translation_Controller :: get_instance () -> unload_textdomain ( $domain );
I18N: Introduce a more performant localization library.
This introduces a more lightweight library for loading `.mo` translation files which offers increased speed and lower memory usage.
It also supports loading multiple locales at the same time, which makes locale switching faster too.
For plugins interacting with the `$l10n` global variable in core, a shim is added to retain backward compatibility with the existing `pomo` library.
In addition to that, this library supports translations contained in PHP files, avoiding a binary file format and leveraging OPCache if available.
If an `.mo` translation file has a corresponding `.l10n.php` file, the latter will be loaded instead.
This behavior can be adjusted using the new `translation_file_format` and `load_translation_file` filters.
PHP translation files will be typically created by downloading language packs, but can also be generated by plugins.
See https://make.wordpress.org/core/2023/11/08/merging-performant-translations-into-core/ for more context.
Props dd32, swissspidy, flixos90, joemcgill, westonruter, akirk, SergeyBiryukov.
Fixes #59656.
Built from https://develop.svn.wordpress.org/trunk@57337
git-svn-id: http://core.svn.wordpress.org/trunk@56843 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-01-23 14:34:11 +01:00
}
2017-12-01 00:11:00 +01:00
if ( isset ( $l10n [ $domain ] ) ) {
2023-05-29 10:36:23 +02:00
if ( $l10n [ $domain ] instanceof NOOP_Translations ) {
unset ( $l10n [ $domain ] );
return false ;
}
2017-12-01 00:11:00 +01:00
unset ( $l10n [ $domain ] );
2016-06-26 16:26:29 +02:00
2022-08-11 14:39:12 +02:00
if ( ! $reloadable ) {
$l10n_unloaded [ $domain ] = true ;
}
2016-06-26 16:26:29 +02:00
2010-02-23 12:14:35 +01:00
return true ;
}
return false ;
}
2008-01-06 02:28:34 +01:00
/**
2022-06-07 07:18:13 +02:00
* Loads default translated strings based on locale .
2013-09-16 22:17:09 +02:00
*
* Loads the . mo file in WP_LANG_DIR constant path from WordPress root .
* The translated ( . mo ) file is named based on the locale .
2008-01-06 02:28:34 +01:00
*
2013-09-16 22:17:09 +02:00
* @ see load_textdomain ()
2008-01-06 02:28:34 +01:00
*
* @ since 1.5 . 0
2014-08-26 21:59:16 +02:00
*
2016-05-02 06:00:28 +02:00
* @ param string $locale Optional . Locale to load . Default is the value of get_locale () .
2014-11-18 19:47:22 +01:00
* @ return bool Whether the textdomain was loaded .
2008-01-06 02:28:34 +01:00
*/
2014-08-26 21:59:16 +02:00
function load_default_textdomain ( $locale = null ) {
if ( null === $locale ) {
2018-12-14 02:32:39 +01:00
$locale = determine_locale ();
2014-08-26 21:59:16 +02:00
}
// Unload previously loaded strings so we can switch translations.
I18N: Introduce a more performant localization library.
This introduces a more lightweight library for loading `.mo` translation files which offers increased speed and lower memory usage.
It also supports loading multiple locales at the same time, which makes locale switching faster too.
For plugins interacting with the `$l10n` global variable in core, a shim is added to retain backward compatibility with the existing `pomo` library.
In addition to that, this library supports translations contained in PHP files, avoiding a binary file format and leveraging OPCache if available.
If an `.mo` translation file has a corresponding `.l10n.php` file, the latter will be loaded instead.
This behavior can be adjusted using the new `translation_file_format` and `load_translation_file` filters.
PHP translation files will be typically created by downloading language packs, but can also be generated by plugins.
See https://make.wordpress.org/core/2023/11/08/merging-performant-translations-into-core/ for more context.
Props dd32, swissspidy, flixos90, joemcgill, westonruter, akirk, SergeyBiryukov.
Fixes #59656.
Built from https://develop.svn.wordpress.org/trunk@57337
git-svn-id: http://core.svn.wordpress.org/trunk@56843 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-01-23 14:34:11 +01:00
unload_textdomain ( 'default' , true );
2007-01-18 18:40:05 +01:00
2022-08-11 14:39:12 +02:00
$return = load_textdomain ( 'default' , WP_LANG_DIR . " / $locale .mo " , $locale );
2010-06-02 22:04:07 +02:00
2017-12-01 00:11:00 +01:00
if ( ( is_multisite () || ( defined ( 'WP_INSTALLING_NETWORK' ) && WP_INSTALLING_NETWORK ) ) && ! file_exists ( WP_LANG_DIR . " /admin- $locale .mo " ) ) {
2022-08-11 14:39:12 +02:00
load_textdomain ( 'default' , WP_LANG_DIR . " /ms- $locale .mo " , $locale );
2014-08-26 21:59:16 +02:00
return $return ;
2010-05-29 17:32:43 +02:00
}
2012-01-27 23:19:56 +01:00
2024-12-02 18:08:20 +01:00
if ( is_admin () || wp_installing () || ( defined ( 'WP_REPAIRING' ) && WP_REPAIRING ) || doing_action ( 'wp_maybe_auto_update' ) ) {
2022-08-11 14:39:12 +02:00
load_textdomain ( 'default' , WP_LANG_DIR . " /admin- $locale .mo " , $locale );
2014-08-26 21:59:16 +02:00
}
2012-01-27 23:19:56 +01:00
2017-12-01 00:11:00 +01:00
if ( is_network_admin () || ( defined ( 'WP_INSTALLING_NETWORK' ) && WP_INSTALLING_NETWORK ) ) {
2022-08-11 14:39:12 +02:00
load_textdomain ( 'default' , WP_LANG_DIR . " /admin-network- $locale .mo " , $locale );
2017-12-01 00:11:00 +01:00
}
2012-01-27 23:19:56 +01:00
2014-08-26 21:59:16 +02:00
return $return ;
2004-10-19 02:18:12 +02:00
}
2008-01-06 02:28:34 +01:00
/**
2016-07-20 18:57:32 +02:00
* Loads a plugin ' s translated strings .
2008-01-06 02:28:34 +01:00
*
2008-08-30 23:23:43 +02:00
* If the path is not given then it will be the root of the plugin directory .
2013-09-16 22:17:09 +02:00
*
2013-09-24 00:28:10 +02:00
* The . mo file should be named based on the text domain with a dash , and then the locale exactly .
2008-01-06 02:28:34 +01:00
*
* @ since 1.5 . 0
2016-05-10 20:50:27 +02:00
* @ since 4.6 . 0 The function now tries to load the . mo file from the languages directory first .
I18N: Do not load translations directly in `load_*_textdomain`.
In [59127], `_doing_it_wrong` warnings were added if plugins or themes load translations too early, either through a manual function call or just-in-time loading.
Because many plugins and themes still manually call `load_plugin_textdomain()`, `load_theme_textdomain()` or `load_muplugin_textdomain()`, even though they don't have to anymore, that caused a lot of warnings.
With this new approach, these functions merely register the translations path in the existing `WP_Textdomain_Registry` and do not immediately try to load the translations anymore. The loading is all handled by the just-in-time functionality.
This way, warnings will only be emitted if triggering the just-in-time loading too early, greatly improving the developer experience and to a degree also performance.
Props swissspidy, sergeybiryukov, mukesh27.
See #44937.
Built from https://develop.svn.wordpress.org/trunk@59157
git-svn-id: http://core.svn.wordpress.org/trunk@58552 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-10-02 15:44:17 +02:00
* @ since 6.7 . 0 Translations are no longer immediately loaded , but handed off to the just - in - time loading mechanism .
2008-01-06 02:28:34 +01:00
*
i18n: Account for `load_*_textdomain()` after JIT loading.
When `load_*_textdomain()` functions are called after WordPress has already attempted just-in-time loading of translations, nothing happens.
This updates the related logic to retry translation loading when a custom path is set to ensure all translations are available.
Additionally, this also fixes cases where an `en_US.mo` file is provided with non-English strings to override the default language.
Follow up to [59157].
Props swissspidy, peterwilsoncc, desrosj, apermo, sergeybiryukov, wildworks, tigriweb, twvania, looswebstudio, stimul, audrasjb, finntown, bluantinoo, timwhitlock, albigdd.
See #62337.
Built from https://develop.svn.wordpress.org/trunk@59430
git-svn-id: http://core.svn.wordpress.org/trunk@58816 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-11-20 03:50:24 +01:00
* @ global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry .
* @ global array < string , WP_Translations | NOOP_Translations > $l10n An array of all currently loaded text domains .
*
2019-04-19 16:21:53 +02:00
* @ 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 .
2014-06-30 15:52:15 +02:00
* @ return bool True when textdomain is successfully loaded , false otherwise .
2008-01-06 02:28:34 +01:00
*/
2013-09-16 22:17:09 +02:00
function load_plugin_textdomain ( $domain , $deprecated = false , $plugin_rel_path = false ) {
2022-08-11 14:39:12 +02:00
/** @var WP_Textdomain_Registry $wp_textdomain_registry */
i18n: Account for `load_*_textdomain()` after JIT loading.
When `load_*_textdomain()` functions are called after WordPress has already attempted just-in-time loading of translations, nothing happens.
This updates the related logic to retry translation loading when a custom path is set to ensure all translations are available.
Additionally, this also fixes cases where an `en_US.mo` file is provided with non-English strings to override the default language.
Follow up to [59157].
Props swissspidy, peterwilsoncc, desrosj, apermo, sergeybiryukov, wildworks, tigriweb, twvania, looswebstudio, stimul, audrasjb, finntown, bluantinoo, timwhitlock, albigdd.
See #62337.
Built from https://develop.svn.wordpress.org/trunk@59430
git-svn-id: http://core.svn.wordpress.org/trunk@58816 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-11-20 03:50:24 +01:00
/** @var array<string, WP_Translations|NOOP_Translations> $l10n */
global $wp_textdomain_registry , $l10n ;
2022-08-11 14:39:12 +02:00
2024-04-04 15:56:13 +02:00
if ( ! is_string ( $domain ) ) {
return false ;
}
2016-05-10 20:50:27 +02:00
if ( false !== $plugin_rel_path ) {
2009-11-21 10:28:32 +01:00
$path = WP_PLUGIN_DIR . '/' . trim ( $plugin_rel_path , '/' );
2015-01-08 08:05:25 +01:00
} elseif ( false !== $deprecated ) {
2016-07-06 14:40:29 +02:00
_deprecated_argument ( __FUNCTION__ , '2.7.0' );
2013-09-16 22:17:09 +02:00
$path = ABSPATH . trim ( $deprecated , '/' );
2010-01-02 11:28:05 +01:00
} else {
2008-06-09 21:39:04 +02:00
$path = WP_PLUGIN_DIR ;
2010-01-02 11:28:05 +01:00
}
2010-01-15 23:11:12 +01:00
2022-10-24 12:02:16 +02:00
$wp_textdomain_registry -> set_custom_path ( $domain , $path );
2022-08-11 14:39:12 +02:00
i18n: Account for `load_*_textdomain()` after JIT loading.
When `load_*_textdomain()` functions are called after WordPress has already attempted just-in-time loading of translations, nothing happens.
This updates the related logic to retry translation loading when a custom path is set to ensure all translations are available.
Additionally, this also fixes cases where an `en_US.mo` file is provided with non-English strings to override the default language.
Follow up to [59157].
Props swissspidy, peterwilsoncc, desrosj, apermo, sergeybiryukov, wildworks, tigriweb, twvania, looswebstudio, stimul, audrasjb, finntown, bluantinoo, timwhitlock, albigdd.
See #62337.
Built from https://develop.svn.wordpress.org/trunk@59430
git-svn-id: http://core.svn.wordpress.org/trunk@58816 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-11-20 03:50:24 +01:00
// If just-in-time loading was triggered before, reset the entry so it can be tried again.
if ( isset ( $l10n [ $domain ] ) && $l10n [ $domain ] instanceof NOOP_Translations ) {
unset ( $l10n [ $domain ] );
}
I18N: Do not load translations directly in `load_*_textdomain`.
In [59127], `_doing_it_wrong` warnings were added if plugins or themes load translations too early, either through a manual function call or just-in-time loading.
Because many plugins and themes still manually call `load_plugin_textdomain()`, `load_theme_textdomain()` or `load_muplugin_textdomain()`, even though they don't have to anymore, that caused a lot of warnings.
With this new approach, these functions merely register the translations path in the existing `WP_Textdomain_Registry` and do not immediately try to load the translations anymore. The loading is all handled by the just-in-time functionality.
This way, warnings will only be emitted if triggering the just-in-time loading too early, greatly improving the developer experience and to a degree also performance.
Props swissspidy, sergeybiryukov, mukesh27.
See #44937.
Built from https://develop.svn.wordpress.org/trunk@59157
git-svn-id: http://core.svn.wordpress.org/trunk@58552 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-10-02 15:44:17 +02:00
return true ;
2004-10-18 04:27:35 +02:00
}
2010-01-26 18:25:31 +01:00
/**
2022-06-07 07:18:13 +02:00
* Loads the translated strings for a plugin residing in the mu - plugins directory .
2010-01-26 18:25:31 +01:00
*
2010-03-26 20:13:36 +01:00
* @ since 3.0 . 0
2016-05-10 20:50:27 +02:00
* @ since 4.6 . 0 The function now tries to load the . mo file from the languages directory first .
I18N: Do not load translations directly in `load_*_textdomain`.
In [59127], `_doing_it_wrong` warnings were added if plugins or themes load translations too early, either through a manual function call or just-in-time loading.
Because many plugins and themes still manually call `load_plugin_textdomain()`, `load_theme_textdomain()` or `load_muplugin_textdomain()`, even though they don't have to anymore, that caused a lot of warnings.
With this new approach, these functions merely register the translations path in the existing `WP_Textdomain_Registry` and do not immediately try to load the translations anymore. The loading is all handled by the just-in-time functionality.
This way, warnings will only be emitted if triggering the just-in-time loading too early, greatly improving the developer experience and to a degree also performance.
Props swissspidy, sergeybiryukov, mukesh27.
See #44937.
Built from https://develop.svn.wordpress.org/trunk@59157
git-svn-id: http://core.svn.wordpress.org/trunk@58552 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-10-02 15:44:17 +02:00
* @ since 6.7 . 0 Translations are no longer immediately loaded , but handed off to the just - in - time loading mechanism .
2010-01-26 18:25:31 +01:00
*
2022-08-11 14:39:12 +02:00
* @ global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry .
i18n: Account for `load_*_textdomain()` after JIT loading.
When `load_*_textdomain()` functions are called after WordPress has already attempted just-in-time loading of translations, nothing happens.
This updates the related logic to retry translation loading when a custom path is set to ensure all translations are available.
Additionally, this also fixes cases where an `en_US.mo` file is provided with non-English strings to override the default language.
Follow up to [59157].
Props swissspidy, peterwilsoncc, desrosj, apermo, sergeybiryukov, wildworks, tigriweb, twvania, looswebstudio, stimul, audrasjb, finntown, bluantinoo, timwhitlock, albigdd.
See #62337.
Built from https://develop.svn.wordpress.org/trunk@59430
git-svn-id: http://core.svn.wordpress.org/trunk@58816 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-11-20 03:50:24 +01:00
* @ global array < string , WP_Translations | NOOP_Translations > $l10n An array of all currently loaded text domains .
2022-08-11 14:39:12 +02:00
*
2013-09-24 00:28:10 +02:00
* @ param string $domain Text domain . Unique identifier for retrieving translated strings .
2016-07-20 18:57:32 +02:00
* @ param string $mu_plugin_rel_path Optional . Relative to `WPMU_PLUGIN_DIR` directory in which the . mo
* file resides . Default empty string .
2013-09-16 22:17:09 +02:00
* @ return bool True when textdomain is successfully loaded , false otherwise .
2010-01-26 18:25:31 +01:00
*/
2010-04-22 13:32:50 +02:00
function load_muplugin_textdomain ( $domain , $mu_plugin_rel_path = '' ) {
2022-08-11 14:39:12 +02:00
/** @var WP_Textdomain_Registry $wp_textdomain_registry */
i18n: Account for `load_*_textdomain()` after JIT loading.
When `load_*_textdomain()` functions are called after WordPress has already attempted just-in-time loading of translations, nothing happens.
This updates the related logic to retry translation loading when a custom path is set to ensure all translations are available.
Additionally, this also fixes cases where an `en_US.mo` file is provided with non-English strings to override the default language.
Follow up to [59157].
Props swissspidy, peterwilsoncc, desrosj, apermo, sergeybiryukov, wildworks, tigriweb, twvania, looswebstudio, stimul, audrasjb, finntown, bluantinoo, timwhitlock, albigdd.
See #62337.
Built from https://develop.svn.wordpress.org/trunk@59430
git-svn-id: http://core.svn.wordpress.org/trunk@58816 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-11-20 03:50:24 +01:00
/** @var array<string, WP_Translations|NOOP_Translations> $l10n */
global $wp_textdomain_registry , $l10n ;
2022-08-11 14:39:12 +02:00
2024-04-04 15:56:13 +02:00
if ( ! is_string ( $domain ) ) {
return false ;
}
2017-04-01 16:26:40 +02:00
$path = WPMU_PLUGIN_DIR . '/' . ltrim ( $mu_plugin_rel_path , '/' );
2016-05-10 20:50:27 +02:00
2022-10-24 12:02:16 +02:00
$wp_textdomain_registry -> set_custom_path ( $domain , $path );
2022-08-11 14:39:12 +02:00
i18n: Account for `load_*_textdomain()` after JIT loading.
When `load_*_textdomain()` functions are called after WordPress has already attempted just-in-time loading of translations, nothing happens.
This updates the related logic to retry translation loading when a custom path is set to ensure all translations are available.
Additionally, this also fixes cases where an `en_US.mo` file is provided with non-English strings to override the default language.
Follow up to [59157].
Props swissspidy, peterwilsoncc, desrosj, apermo, sergeybiryukov, wildworks, tigriweb, twvania, looswebstudio, stimul, audrasjb, finntown, bluantinoo, timwhitlock, albigdd.
See #62337.
Built from https://develop.svn.wordpress.org/trunk@59430
git-svn-id: http://core.svn.wordpress.org/trunk@58816 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-11-20 03:50:24 +01:00
// If just-in-time loading was triggered before, reset the entry so it can be tried again.
if ( isset ( $l10n [ $domain ] ) && $l10n [ $domain ] instanceof NOOP_Translations ) {
unset ( $l10n [ $domain ] );
}
I18N: Do not load translations directly in `load_*_textdomain`.
In [59127], `_doing_it_wrong` warnings were added if plugins or themes load translations too early, either through a manual function call or just-in-time loading.
Because many plugins and themes still manually call `load_plugin_textdomain()`, `load_theme_textdomain()` or `load_muplugin_textdomain()`, even though they don't have to anymore, that caused a lot of warnings.
With this new approach, these functions merely register the translations path in the existing `WP_Textdomain_Registry` and do not immediately try to load the translations anymore. The loading is all handled by the just-in-time functionality.
This way, warnings will only be emitted if triggering the just-in-time loading too early, greatly improving the developer experience and to a degree also performance.
Props swissspidy, sergeybiryukov, mukesh27.
See #44937.
Built from https://develop.svn.wordpress.org/trunk@59157
git-svn-id: http://core.svn.wordpress.org/trunk@58552 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-10-02 15:44:17 +02:00
return true ;
2010-01-26 18:25:31 +01:00
}
2008-01-06 02:28:34 +01:00
/**
2022-06-07 07:18:13 +02:00
* Loads the theme ' s translated strings .
2008-01-06 02:28:34 +01:00
*
* 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
2016-05-10 20:50:27 +02:00
* @ since 4.6 . 0 The function now tries to load the . mo file from the languages directory first .
I18N: Do not load translations directly in `load_*_textdomain`.
In [59127], `_doing_it_wrong` warnings were added if plugins or themes load translations too early, either through a manual function call or just-in-time loading.
Because many plugins and themes still manually call `load_plugin_textdomain()`, `load_theme_textdomain()` or `load_muplugin_textdomain()`, even though they don't have to anymore, that caused a lot of warnings.
With this new approach, these functions merely register the translations path in the existing `WP_Textdomain_Registry` and do not immediately try to load the translations anymore. The loading is all handled by the just-in-time functionality.
This way, warnings will only be emitted if triggering the just-in-time loading too early, greatly improving the developer experience and to a degree also performance.
Props swissspidy, sergeybiryukov, mukesh27.
See #44937.
Built from https://develop.svn.wordpress.org/trunk@59157
git-svn-id: http://core.svn.wordpress.org/trunk@58552 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-10-02 15:44:17 +02:00
* @ since 6.7 . 0 Translations are no longer immediately loaded , but handed off to the just - in - time loading mechanism .
2008-01-06 02:28:34 +01:00
*
2022-08-11 14:39:12 +02:00
* @ global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry .
i18n: Account for `load_*_textdomain()` after JIT loading.
When `load_*_textdomain()` functions are called after WordPress has already attempted just-in-time loading of translations, nothing happens.
This updates the related logic to retry translation loading when a custom path is set to ensure all translations are available.
Additionally, this also fixes cases where an `en_US.mo` file is provided with non-English strings to override the default language.
Follow up to [59157].
Props swissspidy, peterwilsoncc, desrosj, apermo, sergeybiryukov, wildworks, tigriweb, twvania, looswebstudio, stimul, audrasjb, finntown, bluantinoo, timwhitlock, albigdd.
See #62337.
Built from https://develop.svn.wordpress.org/trunk@59430
git-svn-id: http://core.svn.wordpress.org/trunk@58816 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-11-20 03:50:24 +01:00
* @ global array < string , WP_Translations | NOOP_Translations > $l10n An array of all currently loaded text domains .
2022-08-11 14:39:12 +02:00
*
2021-01-05 18:16:11 +01:00
* @ 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 .
2013-09-16 22:17:09 +02:00
* @ return bool True when textdomain is successfully loaded , false otherwise .
2008-01-06 02:28:34 +01:00
*/
2010-02-23 12:14:35 +01:00
function load_theme_textdomain ( $domain , $path = false ) {
2022-08-11 14:39:12 +02:00
/** @var WP_Textdomain_Registry $wp_textdomain_registry */
i18n: Account for `load_*_textdomain()` after JIT loading.
When `load_*_textdomain()` functions are called after WordPress has already attempted just-in-time loading of translations, nothing happens.
This updates the related logic to retry translation loading when a custom path is set to ensure all translations are available.
Additionally, this also fixes cases where an `en_US.mo` file is provided with non-English strings to override the default language.
Follow up to [59157].
Props swissspidy, peterwilsoncc, desrosj, apermo, sergeybiryukov, wildworks, tigriweb, twvania, looswebstudio, stimul, audrasjb, finntown, bluantinoo, timwhitlock, albigdd.
See #62337.
Built from https://develop.svn.wordpress.org/trunk@59430
git-svn-id: http://core.svn.wordpress.org/trunk@58816 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-11-20 03:50:24 +01:00
/** @var array<string, WP_Translations|NOOP_Translations> $l10n */
global $wp_textdomain_registry , $l10n ;
2022-08-11 14:39:12 +02:00
2024-04-04 15:56:13 +02:00
if ( ! is_string ( $domain ) ) {
return false ;
}
2016-05-10 20:50:27 +02:00
if ( ! $path ) {
$path = get_template_directory ();
}
2012-10-31 22:30:33 +01:00
2022-10-24 12:02:16 +02:00
$wp_textdomain_registry -> set_custom_path ( $domain , $path );
2022-08-11 14:39:12 +02:00
i18n: Account for `load_*_textdomain()` after JIT loading.
When `load_*_textdomain()` functions are called after WordPress has already attempted just-in-time loading of translations, nothing happens.
This updates the related logic to retry translation loading when a custom path is set to ensure all translations are available.
Additionally, this also fixes cases where an `en_US.mo` file is provided with non-English strings to override the default language.
Follow up to [59157].
Props swissspidy, peterwilsoncc, desrosj, apermo, sergeybiryukov, wildworks, tigriweb, twvania, looswebstudio, stimul, audrasjb, finntown, bluantinoo, timwhitlock, albigdd.
See #62337.
Built from https://develop.svn.wordpress.org/trunk@59430
git-svn-id: http://core.svn.wordpress.org/trunk@58816 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-11-20 03:50:24 +01:00
// If just-in-time loading was triggered before, reset the entry so it can be tried again.
if ( isset ( $l10n [ $domain ] ) && $l10n [ $domain ] instanceof NOOP_Translations ) {
unset ( $l10n [ $domain ] );
}
I18N: Do not load translations directly in `load_*_textdomain`.
In [59127], `_doing_it_wrong` warnings were added if plugins or themes load translations too early, either through a manual function call or just-in-time loading.
Because many plugins and themes still manually call `load_plugin_textdomain()`, `load_theme_textdomain()` or `load_muplugin_textdomain()`, even though they don't have to anymore, that caused a lot of warnings.
With this new approach, these functions merely register the translations path in the existing `WP_Textdomain_Registry` and do not immediately try to load the translations anymore. The loading is all handled by the just-in-time functionality.
This way, warnings will only be emitted if triggering the just-in-time loading too early, greatly improving the developer experience and to a degree also performance.
Props swissspidy, sergeybiryukov, mukesh27.
See #44937.
Built from https://develop.svn.wordpress.org/trunk@59157
git-svn-id: http://core.svn.wordpress.org/trunk@58552 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-10-02 15:44:17 +02:00
return true ;
2004-04-16 03:56:04 +02:00
}
2004-04-17 07:01:10 +02:00
2009-11-12 22:50:17 +01:00
/**
Docs: Update various DocBlocks and inline comments per the documentation standards.
Includes minor formatting edits for consistency.
Follow-up to [53/tests], [12179], [12946], [35288], [37884], [38810], [38928], [46596], [48131], [52955], [53548], [53813], [53873], [54118], [54316], [54420], [54421], [54803].
See #56792.
Built from https://develop.svn.wordpress.org/trunk@54855
git-svn-id: http://core.svn.wordpress.org/trunk@54407 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-11-17 19:15:19 +01:00
* Loads the child theme ' s translated strings .
2009-11-12 22:50:17 +01:00
*
Docs: Update various DocBlocks and inline comments per the documentation standards.
Includes minor formatting edits for consistency.
Follow-up to [53/tests], [12179], [12946], [35288], [37884], [38810], [38928], [46596], [48131], [52955], [53548], [53813], [53873], [54118], [54316], [54420], [54421], [54803].
See #56792.
Built from https://develop.svn.wordpress.org/trunk@54855
git-svn-id: http://core.svn.wordpress.org/trunk@54407 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-11-17 19:15:19 +01:00
* If the current locale exists as a . mo file in the child theme ' s
2013-09-16 22:17:09 +02:00
* root directory , it will be included in the translated strings by the $domain .
2009-11-12 22:50:17 +01:00
*
* The . mo files must be named based on the locale exactly .
*
* @ since 2.9 . 0
*
2021-01-05 18:16:11 +01:00
* @ 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 .
2013-09-16 22:17:09 +02:00
* @ return bool True when the theme textdomain is successfully loaded , false otherwise .
2009-11-12 22:50:17 +01:00
*/
2010-02-23 12:14:35 +01:00
function load_child_theme_textdomain ( $domain , $path = false ) {
2017-12-01 00:11:00 +01:00
if ( ! $path ) {
2012-10-31 22:30:33 +01:00
$path = get_stylesheet_directory ();
2017-12-01 00:11:00 +01:00
}
2012-10-31 22:30:33 +01:00
return load_theme_textdomain ( $domain , $path );
2009-11-12 22:50:17 +01:00
}
I18N: Add JavaScript translation support.
Adds the `wp_set_script_translations()` function which registers translations for a JavaScript file. This function takes a handle, domain and optionally a path and ensures JavaScript translation files are loaded if they exist.
Merges [43825,43828,43859,43898] from the 5.0 branch to trunk.
Props herregroen, atimmer, omarreiss, nerrad, swissspidy, ocean90, georgestephanis.
Fixes #45103, #45256.
Built from https://develop.svn.wordpress.org/trunk@44169
git-svn-id: http://core.svn.wordpress.org/trunk@43999 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-12-14 06:52:52 +01:00
/**
2018-12-19 04:33:07 +01:00
* Loads the script translated strings .
I18N: Add JavaScript translation support.
Adds the `wp_set_script_translations()` function which registers translations for a JavaScript file. This function takes a handle, domain and optionally a path and ensures JavaScript translation files are loaded if they exist.
Merges [43825,43828,43859,43898] from the 5.0 branch to trunk.
Props herregroen, atimmer, omarreiss, nerrad, swissspidy, ocean90, georgestephanis.
Fixes #45103, #45256.
Built from https://develop.svn.wordpress.org/trunk@44169
git-svn-id: http://core.svn.wordpress.org/trunk@43999 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-12-14 06:52:52 +01:00
*
* @ since 5.0 . 0
2018-12-19 04:33:07 +01:00
* @ since 5.0 . 2 Uses load_script_translations () to load translation data .
2019-01-04 22:12:50 +01:00
* @ since 5.1 . 0 The `$domain` parameter was made optional .
2018-12-19 04:33:07 +01:00
*
* @ see WP_Scripts :: set_translations ()
I18N: Add JavaScript translation support.
Adds the `wp_set_script_translations()` function which registers translations for a JavaScript file. This function takes a handle, domain and optionally a path and ensures JavaScript translation files are loaded if they exist.
Merges [43825,43828,43859,43898] from the 5.0 branch to trunk.
Props herregroen, atimmer, omarreiss, nerrad, swissspidy, ocean90, georgestephanis.
Fixes #45103, #45256.
Built from https://develop.svn.wordpress.org/trunk@44169
git-svn-id: http://core.svn.wordpress.org/trunk@43999 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-12-14 06:52:52 +01:00
*
* @ param string $handle Name of the script to register a translation domain to .
2019-01-04 22:12:50 +01:00
* @ param string $domain Optional . Text domain . Default 'default' .
I18N: Add JavaScript translation support.
Adds the `wp_set_script_translations()` function which registers translations for a JavaScript file. This function takes a handle, domain and optionally a path and ensures JavaScript translation files are loaded if they exist.
Merges [43825,43828,43859,43898] from the 5.0 branch to trunk.
Props herregroen, atimmer, omarreiss, nerrad, swissspidy, ocean90, georgestephanis.
Fixes #45103, #45256.
Built from https://develop.svn.wordpress.org/trunk@44169
git-svn-id: http://core.svn.wordpress.org/trunk@43999 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-12-14 06:52:52 +01:00
* @ param string $path Optional . The full file path to the directory containing translation files .
2021-01-15 21:08:07 +01:00
* @ return string | false The translated strings in JSON encoding on success ,
* false if the script textdomain could not be loaded .
I18N: Add JavaScript translation support.
Adds the `wp_set_script_translations()` function which registers translations for a JavaScript file. This function takes a handle, domain and optionally a path and ensures JavaScript translation files are loaded if they exist.
Merges [43825,43828,43859,43898] from the 5.0 branch to trunk.
Props herregroen, atimmer, omarreiss, nerrad, swissspidy, ocean90, georgestephanis.
Fixes #45103, #45256.
Built from https://develop.svn.wordpress.org/trunk@44169
git-svn-id: http://core.svn.wordpress.org/trunk@43999 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-12-14 06:52:52 +01:00
*/
Code Modernization: Use correct default value for JavaScript translations path.
The `$path` parameter of `load_script_textdomain()` had a default value of `null`, but would be passed onto the `untrailingslashit()` function without any input validation, even though the latter explicitly only expects/supports a string input.
This commit changes the default value for `$path` to an empty string, and adds an `is_string()` check before passing the value to `untrailingslashit()` to fix the issue at the point where the invalid input is incorrectly (not) validated.
Note: Changing the `untrailingslashit()` function is outside the scope of this commit.
Includes:
* Adding a dedicated unit test for this issue.
* Correcting the default value for `$path` from `null` to an empty string in a few related methods and functions:
* `WP_Dependency::set_translations()`
* `WP_Scripts::set_translations()`
* `wp_set_script_translations()`
* `load_script_textdomain()`
This fix also allows to remove a couple of calls to `expectDeprecation()` in unrelated tests.
Fixes an error when running the test suite:
{{{
4) Tests_Dependencies_Scripts::test_wp_external_wp_i18n_print_order
rtrim(): Passing null to parameter #1 ($string) of type string is deprecated
/var/www/src/wp-includes/formatting.php:2782
/var/www/src/wp-includes/l10n.php:1068
/var/www/src/wp-includes/class.wp-scripts.php:605
/var/www/src/wp-includes/class.wp-scripts.php:320
/var/www/src/wp-includes/class.wp-dependencies.php:136
/var/www/src/wp-includes/functions.wp-scripts.php:109
/var/www/tests/phpunit/tests/dependencies/scripts.php:1505
/var/www/tests/phpunit/includes/utils.php:436
/var/www/tests/phpunit/tests/dependencies/scripts.php:1507
/var/www/vendor/bin/phpunit:123
}}}
Follow-up to [44169], [44607], [51968].
Props jrf, ocean90, Chouby, swissspidy, lovor, iviweb, meysamnorouzi, DarkoG, oneearth27, SergeyBiryukov.
Fixes #55967. See #55656.
Built from https://develop.svn.wordpress.org/trunk@54349
git-svn-id: http://core.svn.wordpress.org/trunk@53908 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-28 19:33:18 +02:00
function load_script_textdomain ( $handle , $domain = 'default' , $path = '' ) {
2018-12-19 04:33:07 +01:00
$wp_scripts = wp_scripts ();
I18N: Add JavaScript translation support.
Adds the `wp_set_script_translations()` function which registers translations for a JavaScript file. This function takes a handle, domain and optionally a path and ensures JavaScript translation files are loaded if they exist.
Merges [43825,43828,43859,43898] from the 5.0 branch to trunk.
Props herregroen, atimmer, omarreiss, nerrad, swissspidy, ocean90, georgestephanis.
Fixes #45103, #45256.
Built from https://develop.svn.wordpress.org/trunk@44169
git-svn-id: http://core.svn.wordpress.org/trunk@43999 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-12-14 06:52:52 +01:00
2018-12-19 04:28:14 +01:00
if ( ! isset ( $wp_scripts -> registered [ $handle ] ) ) {
return false ;
}
2022-09-29 00:19:10 +02:00
$path = untrailingslashit ( $path );
2018-12-17 20:36:52 +01:00
$locale = determine_locale ();
I18N: Add JavaScript translation support.
Adds the `wp_set_script_translations()` function which registers translations for a JavaScript file. This function takes a handle, domain and optionally a path and ensures JavaScript translation files are loaded if they exist.
Merges [43825,43828,43859,43898] from the 5.0 branch to trunk.
Props herregroen, atimmer, omarreiss, nerrad, swissspidy, ocean90, georgestephanis.
Fixes #45103, #45256.
Built from https://develop.svn.wordpress.org/trunk@44169
git-svn-id: http://core.svn.wordpress.org/trunk@43999 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-12-14 06:52:52 +01:00
// If a path was given and the handle file exists simply return it.
2020-02-09 17:55:09 +01:00
$file_base = 'default' === $domain ? $locale : $domain . '-' . $locale ;
I18N: Add JavaScript translation support.
Adds the `wp_set_script_translations()` function which registers translations for a JavaScript file. This function takes a handle, domain and optionally a path and ensures JavaScript translation files are loaded if they exist.
Merges [43825,43828,43859,43898] from the 5.0 branch to trunk.
Props herregroen, atimmer, omarreiss, nerrad, swissspidy, ocean90, georgestephanis.
Fixes #45103, #45256.
Built from https://develop.svn.wordpress.org/trunk@44169
git-svn-id: http://core.svn.wordpress.org/trunk@43999 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-12-14 06:52:52 +01:00
$handle_filename = $file_base . '-' . $handle . '.json' ;
2019-01-07 14:59:51 +01:00
if ( $path ) {
$translations = load_script_translations ( $path . '/' . $handle_filename , $handle , $domain );
if ( $translations ) {
return $translations ;
}
I18N: Add JavaScript translation support.
Adds the `wp_set_script_translations()` function which registers translations for a JavaScript file. This function takes a handle, domain and optionally a path and ensures JavaScript translation files are loaded if they exist.
Merges [43825,43828,43859,43898] from the 5.0 branch to trunk.
Props herregroen, atimmer, omarreiss, nerrad, swissspidy, ocean90, georgestephanis.
Fixes #45103, #45256.
Built from https://develop.svn.wordpress.org/trunk@44169
git-svn-id: http://core.svn.wordpress.org/trunk@43999 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-12-14 06:52:52 +01:00
}
2018-12-19 04:34:44 +01:00
$src = $wp_scripts -> registered [ $handle ] -> src ;
I18N: Add JavaScript translation support.
Adds the `wp_set_script_translations()` function which registers translations for a JavaScript file. This function takes a handle, domain and optionally a path and ensures JavaScript translation files are loaded if they exist.
Merges [43825,43828,43859,43898] from the 5.0 branch to trunk.
Props herregroen, atimmer, omarreiss, nerrad, swissspidy, ocean90, georgestephanis.
Fixes #45103, #45256.
Built from https://develop.svn.wordpress.org/trunk@44169
git-svn-id: http://core.svn.wordpress.org/trunk@43999 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-12-14 06:52:52 +01:00
Code Modernization: Replace usage of `strpos()` with `str_starts_with()`.
`str_starts_with()` was introduced in PHP 8.0 to perform a case-sensitive check indicating if the string to search in (haystack) begins with the given substring (needle).
WordPress core includes a polyfill for `str_starts_with()` on PHP < 8.0 as of WordPress 5.9.
This commit replaces `0 === strpos( ... )` with `str_starts_with()` in core files, making the code more readable and consistent, as well as improving performance.
While `strpos()` is slightly faster than the polyfill on PHP < 8.0, `str_starts_with()` is noticeably faster on PHP 8.0+, as it is optimized to avoid unnecessarily searching along the whole haystack if it does not find the needle.
Follow-up to [52039], [52040], [52326].
Props spacedmonkey, costdev, sabernhardt, mukesh27, desrosj, jorbin, TobiasBg, ayeshrajans, lgadzhev, SergeyBiryukov.
Fixes #58012.
Built from https://develop.svn.wordpress.org/trunk@55703
git-svn-id: http://core.svn.wordpress.org/trunk@55215 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-05-02 17:45:22 +02:00
if ( ! preg_match ( '|^(https?:)?//|' , $src ) && ! ( $wp_scripts -> content_url && str_starts_with ( $src , $wp_scripts -> content_url ) ) ) {
2018-12-19 04:28:14 +01:00
$src = $wp_scripts -> base_url . $src ;
}
I18N: Add JavaScript translation support.
Adds the `wp_set_script_translations()` function which registers translations for a JavaScript file. This function takes a handle, domain and optionally a path and ensures JavaScript translation files are loaded if they exist.
Merges [43825,43828,43859,43898] from the 5.0 branch to trunk.
Props herregroen, atimmer, omarreiss, nerrad, swissspidy, ocean90, georgestephanis.
Fixes #45103, #45256.
Built from https://develop.svn.wordpress.org/trunk@44169
git-svn-id: http://core.svn.wordpress.org/trunk@43999 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-12-14 06:52:52 +01:00
$relative = false ;
$languages_path = WP_LANG_DIR ;
$src_url = wp_parse_url ( $src );
$content_url = wp_parse_url ( content_url () );
2019-07-27 14:44:56 +02:00
$plugins_url = wp_parse_url ( plugins_url () );
I18N: Add JavaScript translation support.
Adds the `wp_set_script_translations()` function which registers translations for a JavaScript file. This function takes a handle, domain and optionally a path and ensures JavaScript translation files are loaded if they exist.
Merges [43825,43828,43859,43898] from the 5.0 branch to trunk.
Props herregroen, atimmer, omarreiss, nerrad, swissspidy, ocean90, georgestephanis.
Fixes #45103, #45256.
Built from https://develop.svn.wordpress.org/trunk@44169
git-svn-id: http://core.svn.wordpress.org/trunk@43999 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-12-14 06:52:52 +01:00
$site_url = wp_parse_url ( site_url () );
2024-10-21 16:13:14 +02:00
$theme_root = get_theme_root ();
I18N: Add JavaScript translation support.
Adds the `wp_set_script_translations()` function which registers translations for a JavaScript file. This function takes a handle, domain and optionally a path and ensures JavaScript translation files are loaded if they exist.
Merges [43825,43828,43859,43898] from the 5.0 branch to trunk.
Props herregroen, atimmer, omarreiss, nerrad, swissspidy, ocean90, georgestephanis.
Fixes #45103, #45256.
Built from https://develop.svn.wordpress.org/trunk@44169
git-svn-id: http://core.svn.wordpress.org/trunk@43999 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-12-14 06:52:52 +01:00
// If the host is the same or it's a relative URL.
if (
Code Modernization: Replace usage of `strpos()` with `str_starts_with()`.
`str_starts_with()` was introduced in PHP 8.0 to perform a case-sensitive check indicating if the string to search in (haystack) begins with the given substring (needle).
WordPress core includes a polyfill for `str_starts_with()` on PHP < 8.0 as of WordPress 5.9.
This commit replaces `0 === strpos( ... )` with `str_starts_with()` in core files, making the code more readable and consistent, as well as improving performance.
While `strpos()` is slightly faster than the polyfill on PHP < 8.0, `str_starts_with()` is noticeably faster on PHP 8.0+, as it is optimized to avoid unnecessarily searching along the whole haystack if it does not find the needle.
Follow-up to [52039], [52040], [52326].
Props spacedmonkey, costdev, sabernhardt, mukesh27, desrosj, jorbin, TobiasBg, ayeshrajans, lgadzhev, SergeyBiryukov.
Fixes #58012.
Built from https://develop.svn.wordpress.org/trunk@55703
git-svn-id: http://core.svn.wordpress.org/trunk@55215 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-05-02 17:45:22 +02:00
( ! isset ( $content_url [ 'path' ] ) || str_starts_with ( $src_url [ 'path' ], $content_url [ 'path' ] ) ) &&
2020-11-17 21:52:09 +01:00
( ! isset ( $src_url [ 'host' ] ) || ! isset ( $content_url [ 'host' ] ) || $src_url [ 'host' ] === $content_url [ 'host' ] )
I18N: Add JavaScript translation support.
Adds the `wp_set_script_translations()` function which registers translations for a JavaScript file. This function takes a handle, domain and optionally a path and ensures JavaScript translation files are loaded if they exist.
Merges [43825,43828,43859,43898] from the 5.0 branch to trunk.
Props herregroen, atimmer, omarreiss, nerrad, swissspidy, ocean90, georgestephanis.
Fixes #45103, #45256.
Built from https://develop.svn.wordpress.org/trunk@44169
git-svn-id: http://core.svn.wordpress.org/trunk@43999 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-12-14 06:52:52 +01:00
) {
// Make the src relative the specific plugin or theme.
2019-07-27 14:44:56 +02:00
if ( isset ( $content_url [ 'path' ] ) ) {
$relative = substr ( $src_url [ 'path' ], strlen ( $content_url [ 'path' ] ) );
} else {
$relative = $src_url [ 'path' ];
}
$relative = trim ( $relative , '/' );
I18N: Add JavaScript translation support.
Adds the `wp_set_script_translations()` function which registers translations for a JavaScript file. This function takes a handle, domain and optionally a path and ensures JavaScript translation files are loaded if they exist.
Merges [43825,43828,43859,43898] from the 5.0 branch to trunk.
Props herregroen, atimmer, omarreiss, nerrad, swissspidy, ocean90, georgestephanis.
Fixes #45103, #45256.
Built from https://develop.svn.wordpress.org/trunk@44169
git-svn-id: http://core.svn.wordpress.org/trunk@43999 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-12-14 06:52:52 +01:00
$relative = explode ( '/' , $relative );
2024-09-30 16:59:18 +02:00
/*
2024-10-21 16:13:14 +02:00
* Ensure correct languages path when using a custom `WP_PLUGIN_DIR` / `WP_PLUGIN_URL` configuration ,
* a custom theme root , and / or using Multisite with subdirectories .
2024-09-30 16:59:18 +02:00
* See https :// core . trac . wordpress . org / ticket / 60891 and https :// core . trac . wordpress . org / ticket / 62016.
*/
2024-10-21 16:13:14 +02:00
$theme_dir = array_slice ( explode ( '/' , $theme_root ), - 1 );
$dirname = $theme_dir [ 0 ] === $relative [ 0 ] ? 'themes' : 'plugins' ;
2024-09-30 16:59:18 +02:00
$languages_path = WP_LANG_DIR . '/' . $dirname ;
I18N: Add JavaScript translation support.
Adds the `wp_set_script_translations()` function which registers translations for a JavaScript file. This function takes a handle, domain and optionally a path and ensures JavaScript translation files are loaded if they exist.
Merges [43825,43828,43859,43898] from the 5.0 branch to trunk.
Props herregroen, atimmer, omarreiss, nerrad, swissspidy, ocean90, georgestephanis.
Fixes #45103, #45256.
Built from https://develop.svn.wordpress.org/trunk@44169
git-svn-id: http://core.svn.wordpress.org/trunk@43999 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-12-14 06:52:52 +01:00
2019-07-27 14:44:56 +02:00
$relative = array_slice ( $relative , 2 ); // Remove plugins/<plugin name> or themes/<theme name>.
$relative = implode ( '/' , $relative );
} elseif (
Code Modernization: Replace usage of `strpos()` with `str_starts_with()`.
`str_starts_with()` was introduced in PHP 8.0 to perform a case-sensitive check indicating if the string to search in (haystack) begins with the given substring (needle).
WordPress core includes a polyfill for `str_starts_with()` on PHP < 8.0 as of WordPress 5.9.
This commit replaces `0 === strpos( ... )` with `str_starts_with()` in core files, making the code more readable and consistent, as well as improving performance.
While `strpos()` is slightly faster than the polyfill on PHP < 8.0, `str_starts_with()` is noticeably faster on PHP 8.0+, as it is optimized to avoid unnecessarily searching along the whole haystack if it does not find the needle.
Follow-up to [52039], [52040], [52326].
Props spacedmonkey, costdev, sabernhardt, mukesh27, desrosj, jorbin, TobiasBg, ayeshrajans, lgadzhev, SergeyBiryukov.
Fixes #58012.
Built from https://develop.svn.wordpress.org/trunk@55703
git-svn-id: http://core.svn.wordpress.org/trunk@55215 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-05-02 17:45:22 +02:00
( ! isset ( $plugins_url [ 'path' ] ) || str_starts_with ( $src_url [ 'path' ], $plugins_url [ 'path' ] ) ) &&
2020-11-17 21:52:09 +01:00
( ! isset ( $src_url [ 'host' ] ) || ! isset ( $plugins_url [ 'host' ] ) || $src_url [ 'host' ] === $plugins_url [ 'host' ] )
2019-07-27 14:44:56 +02:00
) {
// 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>.
I18N: Add JavaScript translation support.
Adds the `wp_set_script_translations()` function which registers translations for a JavaScript file. This function takes a handle, domain and optionally a path and ensures JavaScript translation files are loaded if they exist.
Merges [43825,43828,43859,43898] from the 5.0 branch to trunk.
Props herregroen, atimmer, omarreiss, nerrad, swissspidy, ocean90, georgestephanis.
Fixes #45103, #45256.
Built from https://develop.svn.wordpress.org/trunk@44169
git-svn-id: http://core.svn.wordpress.org/trunk@43999 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-12-14 06:52:52 +01:00
$relative = implode ( '/' , $relative );
2020-11-17 21:52:09 +01:00
} elseif ( ! isset ( $src_url [ 'host' ] ) || ! isset ( $site_url [ 'host' ] ) || $src_url [ 'host' ] === $site_url [ 'host' ] ) {
I18N: Add JavaScript translation support.
Adds the `wp_set_script_translations()` function which registers translations for a JavaScript file. This function takes a handle, domain and optionally a path and ensures JavaScript translation files are loaded if they exist.
Merges [43825,43828,43859,43898] from the 5.0 branch to trunk.
Props herregroen, atimmer, omarreiss, nerrad, swissspidy, ocean90, georgestephanis.
Fixes #45103, #45256.
Built from https://develop.svn.wordpress.org/trunk@44169
git-svn-id: http://core.svn.wordpress.org/trunk@43999 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-12-14 06:52:52 +01:00
if ( ! isset ( $site_url [ 'path' ] ) ) {
$relative = trim ( $src_url [ 'path' ], '/' );
Code Modernization: Replace usage of `strpos()` with `str_starts_with()`.
`str_starts_with()` was introduced in PHP 8.0 to perform a case-sensitive check indicating if the string to search in (haystack) begins with the given substring (needle).
WordPress core includes a polyfill for `str_starts_with()` on PHP < 8.0 as of WordPress 5.9.
This commit replaces `0 === strpos( ... )` with `str_starts_with()` in core files, making the code more readable and consistent, as well as improving performance.
While `strpos()` is slightly faster than the polyfill on PHP < 8.0, `str_starts_with()` is noticeably faster on PHP 8.0+, as it is optimized to avoid unnecessarily searching along the whole haystack if it does not find the needle.
Follow-up to [52039], [52040], [52326].
Props spacedmonkey, costdev, sabernhardt, mukesh27, desrosj, jorbin, TobiasBg, ayeshrajans, lgadzhev, SergeyBiryukov.
Fixes #58012.
Built from https://develop.svn.wordpress.org/trunk@55703
git-svn-id: http://core.svn.wordpress.org/trunk@55215 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-05-02 17:45:22 +02:00
} elseif ( str_starts_with ( $src_url [ 'path' ], trailingslashit ( $site_url [ 'path' ] ) ) ) {
I18N: Add JavaScript translation support.
Adds the `wp_set_script_translations()` function which registers translations for a JavaScript file. This function takes a handle, domain and optionally a path and ensures JavaScript translation files are loaded if they exist.
Merges [43825,43828,43859,43898] from the 5.0 branch to trunk.
Props herregroen, atimmer, omarreiss, nerrad, swissspidy, ocean90, georgestephanis.
Fixes #45103, #45256.
Built from https://develop.svn.wordpress.org/trunk@44169
git-svn-id: http://core.svn.wordpress.org/trunk@43999 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-12-14 06:52:52 +01:00
// Make the src relative to the WP root.
2018-12-19 04:28:14 +01:00
$relative = substr ( $src_url [ 'path' ], strlen ( $site_url [ 'path' ] ) );
I18N: Add JavaScript translation support.
Adds the `wp_set_script_translations()` function which registers translations for a JavaScript file. This function takes a handle, domain and optionally a path and ensures JavaScript translation files are loaded if they exist.
Merges [43825,43828,43859,43898] from the 5.0 branch to trunk.
Props herregroen, atimmer, omarreiss, nerrad, swissspidy, ocean90, georgestephanis.
Fixes #45103, #45256.
Built from https://develop.svn.wordpress.org/trunk@44169
git-svn-id: http://core.svn.wordpress.org/trunk@43999 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-12-14 06:52:52 +01:00
$relative = trim ( $relative , '/' );
}
}
2018-12-19 04:28:14 +01:00
/**
* Filters the relative path of scripts used for finding translation files .
*
* @ since 5.0 . 2
*
2020-01-08 01:27:04 +01:00
* @ param string | false $relative The relative path of the script . False if it could not be determined .
* @ param string $src The full source URL of the script .
2018-12-19 04:28:14 +01:00
*/
$relative = apply_filters ( 'load_script_textdomain_relative_path' , $relative , $src );
I18N: Add JavaScript translation support.
Adds the `wp_set_script_translations()` function which registers translations for a JavaScript file. This function takes a handle, domain and optionally a path and ensures JavaScript translation files are loaded if they exist.
Merges [43825,43828,43859,43898] from the 5.0 branch to trunk.
Props herregroen, atimmer, omarreiss, nerrad, swissspidy, ocean90, georgestephanis.
Fixes #45103, #45256.
Built from https://develop.svn.wordpress.org/trunk@44169
git-svn-id: http://core.svn.wordpress.org/trunk@43999 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-12-14 06:52:52 +01:00
// If the source is not from WP.
if ( false === $relative ) {
2018-12-19 04:33:07 +01:00
return load_script_translations ( false , $handle , $domain );
I18N: Add JavaScript translation support.
Adds the `wp_set_script_translations()` function which registers translations for a JavaScript file. This function takes a handle, domain and optionally a path and ensures JavaScript translation files are loaded if they exist.
Merges [43825,43828,43859,43898] from the 5.0 branch to trunk.
Props herregroen, atimmer, omarreiss, nerrad, swissspidy, ocean90, georgestephanis.
Fixes #45103, #45256.
Built from https://develop.svn.wordpress.org/trunk@44169
git-svn-id: http://core.svn.wordpress.org/trunk@43999 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-12-14 06:52:52 +01:00
}
// Translations are always based on the unminified filename.
Code Modernization: Replace usage of `substr()` with `str_starts_with()` and `str_ends_with()`.
`str_starts_with()` and `str_ends_with()` were introduced in PHP 8.0 to perform a case-sensitive check indicating if the string to search in (haystack) begins or ends with the given substring (needle).
WordPress core includes a polyfill for these functions on PHP < 8.0 as of WordPress 5.9.
This commit uses `str_starts_with()` and `str_ends_with()` in core files where appropriate:
* `$needle === substr( $string, 0, $length )`, where `$length` is the length of `$needle`, is replaced with `str_starts_with( $haystack, $needle )`.
* `$needle === substr( $string, $offset )`, where `$offset` is negative and the absolute value of `$offset` is the length of `$needle`, is replaced with `str_ends_with( $haystack, $needle )`.
This aims to make the code more readable and consistent, as well as better aligned with modern development practices.
Follow-up to [52039], [52040], [52326], [55703], [55710], [55987], [55988].
Props Soean, spacedmonkey, Clorith, ocean90, azaozz, sabernhardt, SergeyBiryukov.
Fixes #58220.
Built from https://develop.svn.wordpress.org/trunk@55990
git-svn-id: http://core.svn.wordpress.org/trunk@55502 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-06-22 16:57:24 +02:00
if ( str_ends_with ( $relative , '.min.js' ) ) {
I18N: Add JavaScript translation support.
Adds the `wp_set_script_translations()` function which registers translations for a JavaScript file. This function takes a handle, domain and optionally a path and ensures JavaScript translation files are loaded if they exist.
Merges [43825,43828,43859,43898] from the 5.0 branch to trunk.
Props herregroen, atimmer, omarreiss, nerrad, swissspidy, ocean90, georgestephanis.
Fixes #45103, #45256.
Built from https://develop.svn.wordpress.org/trunk@44169
git-svn-id: http://core.svn.wordpress.org/trunk@43999 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-12-14 06:52:52 +01:00
$relative = substr ( $relative , 0 , - 7 ) . '.js' ;
}
$md5_filename = $file_base . '-' . md5 ( $relative ) . '.json' ;
2019-01-07 14:59:51 +01:00
if ( $path ) {
$translations = load_script_translations ( $path . '/' . $md5_filename , $handle , $domain );
if ( $translations ) {
return $translations ;
}
I18N: Add JavaScript translation support.
Adds the `wp_set_script_translations()` function which registers translations for a JavaScript file. This function takes a handle, domain and optionally a path and ensures JavaScript translation files are loaded if they exist.
Merges [43825,43828,43859,43898] from the 5.0 branch to trunk.
Props herregroen, atimmer, omarreiss, nerrad, swissspidy, ocean90, georgestephanis.
Fixes #45103, #45256.
Built from https://develop.svn.wordpress.org/trunk@44169
git-svn-id: http://core.svn.wordpress.org/trunk@43999 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-12-14 06:52:52 +01:00
}
2019-01-07 14:59:51 +01:00
$translations = load_script_translations ( $languages_path . '/' . $md5_filename , $handle , $domain );
if ( $translations ) {
return $translations ;
I18N: Add JavaScript translation support.
Adds the `wp_set_script_translations()` function which registers translations for a JavaScript file. This function takes a handle, domain and optionally a path and ensures JavaScript translation files are loaded if they exist.
Merges [43825,43828,43859,43898] from the 5.0 branch to trunk.
Props herregroen, atimmer, omarreiss, nerrad, swissspidy, ocean90, georgestephanis.
Fixes #45103, #45256.
Built from https://develop.svn.wordpress.org/trunk@44169
git-svn-id: http://core.svn.wordpress.org/trunk@43999 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-12-14 06:52:52 +01:00
}
2018-12-19 04:33:07 +01:00
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 .
2021-01-15 21:08:07 +01:00
* @ return string | false The JSON - encoded translated strings for the given script handle and text domain .
* False if there are none .
2018-12-19 04:33:07 +01:00
*/
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
*
2019-09-21 19:41:57 +02:00
* @ 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 .
2018-12-19 04:33:07 +01:00
*/
$translations = apply_filters ( 'pre_load_script_translations' , null , $file , $handle , $domain );
if ( null !== $translations ) {
return $translations ;
}
/**
* Filters the file path for loading script translations 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 .
*/
$file = apply_filters ( 'load_script_translation_file' , $file , $handle , $domain );
if ( ! $file || ! is_readable ( $file ) ) {
return false ;
}
$translations = file_get_contents ( $file );
/**
* Filters script translations for the given file , script handle and text domain .
*
* @ since 5.0 . 2
*
* @ param string $translations JSON - encoded translation data .
* @ param string $file Path to the translation file that was loaded .
* @ param string $handle Name of the script to register a translation domain to .
* @ param string $domain The text domain .
*/
return apply_filters ( 'load_script_translations' , $translations , $file , $handle , $domain );
I18N: Add JavaScript translation support.
Adds the `wp_set_script_translations()` function which registers translations for a JavaScript file. This function takes a handle, domain and optionally a path and ensures JavaScript translation files are loaded if they exist.
Merges [43825,43828,43859,43898] from the 5.0 branch to trunk.
Props herregroen, atimmer, omarreiss, nerrad, swissspidy, ocean90, georgestephanis.
Fixes #45103, #45256.
Built from https://develop.svn.wordpress.org/trunk@44169
git-svn-id: http://core.svn.wordpress.org/trunk@43999 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-12-14 06:52:52 +01:00
}
2016-05-10 22:31:27 +02:00
/**
2022-08-11 14:39:12 +02:00
* Loads plugin and theme text domains just - in - time .
2016-05-10 22:31:27 +02:00
*
2016-07-20 18:57:32 +02:00
* When a textdomain is encountered for the first time , we try to load
* the translation file from `wp-content/languages` , removing the need
2022-09-19 10:44:10 +02:00
* to call load_plugin_textdomain () or load_theme_textdomain () .
2016-07-20 18:57:32 +02:00
*
2016-05-10 22:31:27 +02:00
* @ since 4.6 . 0
* @ access private
*
2022-08-11 14:39:12 +02:00
* @ 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 .
2016-05-10 22:31:27 +02:00
*
* @ 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 ) {
2022-08-11 14:39:12 +02:00
/** @var WP_Textdomain_Registry $wp_textdomain_registry */
global $l10n_unloaded , $wp_textdomain_registry ;
2016-06-26 16:26:29 +02:00
$l10n_unloaded = ( array ) $l10n_unloaded ;
2016-05-10 22:31:27 +02:00
// Short-circuit if domain is 'default' which is reserved for core.
2016-06-26 16:26:29 +02:00
if ( 'default' === $domain || isset ( $l10n_unloaded [ $domain ] ) ) {
2016-05-10 22:31:27 +02:00
return false ;
}
2022-10-24 12:02:16 +02:00
if ( ! $wp_textdomain_registry -> has ( $domain ) ) {
2016-11-21 17:07:33 +01:00
return false ;
}
2018-12-14 02:32:39 +01:00
$locale = determine_locale ();
Coding Standards: Various alignment fixes from `composer format`.
Follow up to [53874], [54097], [54110], [54155], [54162], [54184].
See #39210, #55443, #56288, #56092, #56408, #56467, #55881.
Built from https://develop.svn.wordpress.org/trunk@54210
git-svn-id: http://core.svn.wordpress.org/trunk@53769 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-19 21:51:09 +02:00
$path = $wp_textdomain_registry -> get ( $domain , $locale );
2022-08-11 14:39:12 +02:00
if ( ! $path ) {
return false ;
2016-05-10 22:31:27 +02:00
}
2024-09-30 17:30:20 +02:00
if ( ! doing_action ( 'after_setup_theme' ) && ! did_action ( 'after_setup_theme' ) ) {
_doing_it_wrong (
__FUNCTION__ ,
sprintf (
I18N: Do not load translations directly in `load_*_textdomain`.
In [59127], `_doing_it_wrong` warnings were added if plugins or themes load translations too early, either through a manual function call or just-in-time loading.
Because many plugins and themes still manually call `load_plugin_textdomain()`, `load_theme_textdomain()` or `load_muplugin_textdomain()`, even though they don't have to anymore, that caused a lot of warnings.
With this new approach, these functions merely register the translations path in the existing `WP_Textdomain_Registry` and do not immediately try to load the translations anymore. The loading is all handled by the just-in-time functionality.
This way, warnings will only be emitted if triggering the just-in-time loading too early, greatly improving the developer experience and to a degree also performance.
Props swissspidy, sergeybiryukov, mukesh27.
See #44937.
Built from https://develop.svn.wordpress.org/trunk@59157
git-svn-id: http://core.svn.wordpress.org/trunk@58552 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-10-02 15:44:17 +02:00
/* translators: 1: The text domain. 2: 'init'. */
__ ( 'Translation loading for the %1$s domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the %2$s action or later.' ),
'<code>' . $domain . '</code>' ,
'<code>init</code>'
2024-09-30 17:30:20 +02:00
),
'6.7.0'
);
}
2022-08-11 14:39:12 +02:00
// 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 " ;
2020-11-12 15:43:09 +01:00
}
2022-08-11 14:39:12 +02:00
return load_textdomain ( $domain , $mofile , $locale );
2016-05-10 22:31:27 +02:00
}
2009-02-17 06:03:29 +01:00
/**
2022-06-07 07:18:13 +02:00
* Returns the Translations instance for a text domain .
2013-09-16 22:17:09 +02:00
*
* If there isn ' t one , returns empty Translations instance .
2009-03-18 03:43:45 +01:00
*
2014-07-01 17:29:13 +02:00
* @ since 2.8 . 0
*
2022-08-11 14:39:12 +02:00
* @ global MO [] $l10n An array of all currently loaded text domains .
2015-05-26 20:13:25 +02:00
*
2013-09-24 00:28:10 +02:00
* @ param string $domain Text domain . Unique identifier for retrieving translated strings .
2016-08-05 00:13:29 +02:00
* @ return Translations | NOOP_Translations A Translations instance .
2009-02-17 06:03:29 +01:00
*/
2012-09-10 19:00:11 +02:00
function get_translations_for_domain ( $domain ) {
2009-02-17 06:03:29 +01:00
global $l10n ;
2016-05-16 17:10:29 +02:00
if ( isset ( $l10n [ $domain ] ) || ( _load_textdomain_just_in_time ( $domain ) && isset ( $l10n [ $domain ] ) ) ) {
2016-02-16 22:15:29 +01:00
return $l10n [ $domain ];
2009-10-21 09:09:00 +02:00
}
2016-02-16 22:15:29 +01:00
static $noop_translations = null ;
if ( null === $noop_translations ) {
2022-11-29 16:51:14 +01:00
$noop_translations = new NOOP_Translations ();
2016-02-16 22:15:29 +01:00
}
2023-05-29 10:36:23 +02:00
$l10n [ $domain ] = & $noop_translations ;
2016-02-16 22:15:29 +01:00
return $noop_translations ;
2009-02-17 06:03:29 +01:00
}
2010-02-22 20:10:03 +01:00
/**
2022-06-07 07:18:13 +02:00
* Determines whether there are translations for the text domain .
2010-02-22 20:10:03 +01:00
*
* @ since 3.0 . 0
2014-07-01 17:29:13 +02:00
*
2022-08-11 14:39:12 +02:00
* @ global MO [] $l10n An array of all currently loaded text domains .
2015-05-26 20:13:25 +02:00
*
2013-09-24 00:28:10 +02:00
* @ param string $domain Text domain . Unique identifier for retrieving translated strings .
2013-09-16 22:17:09 +02:00
* @ return bool Whether there are translations .
2010-02-22 20:10:03 +01:00
*/
function is_textdomain_loaded ( $domain ) {
global $l10n ;
2023-05-29 10:36:23 +02:00
return isset ( $l10n [ $domain ] ) && ! $l10n [ $domain ] instanceof NOOP_Translations ;
2010-02-22 20:10:03 +01:00
}
2009-03-11 00:02:29 +01:00
/**
2013-09-16 22:17:09 +02:00
* Translates role name .
*
* Since the role names are in the database and not in the source there
* are dummy gettext calls to get them into the POT file and this function
* properly translates them back .
2009-03-18 03:43:45 +01:00
*
2017-08-22 13:52:48 +02:00
* The before_last_bar () call is needed , because older installations keep the roles
2009-03-11 00:02:29 +01:00
* using the old context format : 'Role name|User role' and just skipping the
2017-08-22 13:52:48 +02:00
* content after the last bar is easier than fixing them in the DB . New installations
2009-03-11 00:02:29 +01:00
* won ' t suffer from that problem .
2013-09-16 22:17:09 +02:00
*
* @ since 2.8 . 0
2019-03-11 21:32:52 +01:00
* @ since 5.2 . 0 Added the `$domain` parameter .
2013-09-16 22:17:09 +02:00
*
2019-03-11 21:32:52 +01:00
* @ param string $name The role name .
* @ param string $domain Optional . Text domain . Unique identifier for retrieving translated strings .
* Default 'default' .
2013-09-16 22:17:09 +02:00
* @ return string Translated role name on success , original name on failure .
2009-03-11 00:02:29 +01:00
*/
2019-03-11 21:32:52 +01:00
function translate_user_role ( $name , $domain = 'default' ) {
return translate_with_gettext_context ( before_last_bar ( $name ), 'User role' , $domain );
2009-03-11 00:02:29 +01:00
}
2010-02-04 19:46:25 +01:00
/**
2024-02-16 11:28:12 +01:00
* Gets all available languages based on the presence of *. mo and *. l10n . php files in a given directory .
2013-09-16 22:17:09 +02:00
*
* The default directory is WP_LANG_DIR .
2010-02-04 19:46:25 +01:00
*
* @ since 3.0 . 0
2016-11-15 10:52:32 +01:00
* @ since 4.7 . 0 The results are now filterable with the { @ see 'get_available_languages' } filter .
2024-02-16 11:28:12 +01:00
* @ since 6.5 . 0 The initial file list is now cached and also takes into account *. l10n . php files .
2010-02-04 19:46:25 +01:00
*
2024-01-15 20:05:14 +01:00
* @ global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry .
*
2013-09-16 22:17:09 +02:00
* @ param string $dir A directory to search for language files .
* Default WP_LANG_DIR .
Docs: Update various DocBlocks and inline comments per the documentation standards.
Includes minor formatting edits for consistency.
Follow-up to [53/tests], [12179], [12946], [35288], [37884], [38810], [38928], [46596], [48131], [52955], [53548], [53813], [53873], [54118], [54316], [54420], [54421], [54803].
See #56792.
Built from https://develop.svn.wordpress.org/trunk@54855
git-svn-id: http://core.svn.wordpress.org/trunk@54407 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-11-17 19:15:19 +01:00
* @ return string [] An array of language codes or an empty array if no languages are present .
2024-02-16 11:28:12 +01:00
* Language codes are formed by stripping the file extension from the language file names .
2010-02-04 19:46:25 +01:00
*/
function get_available_languages ( $dir = null ) {
2024-01-15 20:05:14 +01:00
global $wp_textdomain_registry ;
2010-05-26 04:42:15 +02:00
$languages = array ();
2024-01-15 20:05:14 +01:00
$path = is_null ( $dir ) ? WP_LANG_DIR : $dir ;
$lang_files = $wp_textdomain_registry -> get_language_files_from_path ( $path );
2015-07-27 20:05:24 +02:00
if ( $lang_files ) {
2015-08-25 22:28:22 +02:00
foreach ( $lang_files as $lang_file ) {
2015-07-27 20:05:24 +02:00
$lang_file = basename ( $lang_file , '.mo' );
2024-02-16 11:28:12 +01:00
$lang_file = basename ( $lang_file , '.l10n.php' );
Code Modernization: Replace usage of `strpos()` with `str_starts_with()`.
`str_starts_with()` was introduced in PHP 8.0 to perform a case-sensitive check indicating if the string to search in (haystack) begins with the given substring (needle).
WordPress core includes a polyfill for `str_starts_with()` on PHP < 8.0 as of WordPress 5.9.
This commit replaces `0 === strpos( ... )` with `str_starts_with()` in core files, making the code more readable and consistent, as well as improving performance.
While `strpos()` is slightly faster than the polyfill on PHP < 8.0, `str_starts_with()` is noticeably faster on PHP 8.0+, as it is optimized to avoid unnecessarily searching along the whole haystack if it does not find the needle.
Follow-up to [52039], [52040], [52326].
Props spacedmonkey, costdev, sabernhardt, mukesh27, desrosj, jorbin, TobiasBg, ayeshrajans, lgadzhev, SergeyBiryukov.
Fixes #58012.
Built from https://develop.svn.wordpress.org/trunk@55703
git-svn-id: http://core.svn.wordpress.org/trunk@55215 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-05-02 17:45:22 +02:00
if ( ! str_starts_with ( $lang_file , 'continents-cities' ) && ! str_starts_with ( $lang_file , 'ms-' ) &&
! str_starts_with ( $lang_file , 'admin-' ) ) {
2015-07-27 20:05:24 +02:00
$languages [] = $lang_file ;
}
}
2010-02-04 19:46:25 +01:00
}
2010-05-26 04:42:15 +02:00
2016-11-15 02:19:31 +01:00
/**
2016-11-15 10:52:32 +01:00
* Filters the list of available language codes .
2016-11-15 02:19:31 +01:00
*
* @ since 4.7 . 0
*
2019-11-05 22:23:02 +01:00
* @ param string [] $languages An array of available language codes .
* @ param string $dir The directory where the language files were found .
2016-11-15 02:19:31 +01:00
*/
2024-02-16 11:28:12 +01:00
return apply_filters ( 'get_available_languages' , array_unique ( $languages ), $dir );
2013-09-16 22:17:09 +02:00
}
2013-09-20 21:13:09 +02:00
/**
2022-06-07 07:18:13 +02:00
* Gets installed translations .
2013-09-20 21:13:09 +02:00
*
* Looks in the wp - content / languages directory for translations of
* plugins or themes .
*
* @ since 3.7 . 0
*
2024-04-30 10:39:07 +02:00
* @ global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry .
*
2013-09-30 21:47:12 +02:00
* @ param string $type What to search for . Accepts 'plugins' , 'themes' , 'core' .
2013-09-20 21:13:09 +02:00
* @ return array Array of language data .
*/
2013-09-21 06:08:10 +02:00
function wp_get_installed_translations ( $type ) {
2024-04-30 10:39:07 +02:00
global $wp_textdomain_registry ;
2020-02-09 17:55:09 +01:00
if ( 'themes' !== $type && 'plugins' !== $type && 'core' !== $type ) {
2013-09-20 21:13:09 +02:00
return array ();
2017-12-01 00:11:00 +01:00
}
2013-09-20 21:13:09 +02:00
2024-04-30 10:39:07 +02:00
$dir = 'core' === $type ? WP_LANG_DIR : WP_LANG_DIR . " / $type " ;
2013-09-30 21:47:12 +02:00
2024-04-30 10:39:07 +02:00
if ( ! is_dir ( $dir ) ) {
2013-09-30 21:47:12 +02:00
return array ();
2017-12-01 00:11:00 +01:00
}
2013-09-30 21:47:12 +02:00
2024-04-30 10:39:07 +02:00
$files = $wp_textdomain_registry -> get_language_files_from_path ( $dir );
2017-12-01 00:11:00 +01:00
if ( ! $files ) {
2013-09-20 21:13:09 +02:00
return array ();
2017-12-01 00:11:00 +01:00
}
2013-09-20 21:13:09 +02:00
$language_data = array ();
foreach ( $files as $file ) {
2024-04-30 10:39:07 +02:00
if ( ! preg_match ( '/(?:(.+)-)?([a-z]{2,3}(?:_[A-Z]{2})?(?:_[a-z0-9]+)?)\.(?:mo|l10n\.php)/' , basename ( $file ), $match ) ) {
2013-09-20 21:13:09 +02:00
continue ;
2014-10-08 21:05:20 +02:00
}
2013-09-20 21:13:09 +02:00
list ( , $textdomain , $language ) = $match ;
2014-10-08 21:05:20 +02:00
if ( '' === $textdomain ) {
2013-09-30 21:47:12 +02:00
$textdomain = 'default' ;
2014-10-08 21:05:20 +02:00
}
2024-04-30 10:39:07 +02:00
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 );
}
2013-09-20 21:13:09 +02:00
}
return $language_data ;
}
/**
2022-06-07 07:18:13 +02:00
* Extracts headers from a PO file .
2013-09-20 21:13:09 +02:00
*
* @ since 3.7 . 0
*
* @ param string $po_file Path to PO file .
2019-11-05 22:23:02 +01:00
* @ return string [] Array of PO file header values keyed by header name .
2013-09-20 21:13:09 +02:00
*/
function wp_get_pomo_file_data ( $po_file ) {
2017-12-01 00:11:00 +01:00
$headers = get_file_data (
2018-08-17 03:51:36 +02:00
$po_file ,
array (
2017-12-01 00:11:00 +01:00
'POT-Creation-Date' => '"POT-Creation-Date' ,
'PO-Revision-Date' => '"PO-Revision-Date' ,
'Project-Id-Version' => '"Project-Id-Version' ,
'X-Generator' => '"X-Generator' ,
)
);
2013-10-30 15:39:10 +01:00
foreach ( $headers as $header => $value ) {
2013-09-20 21:13:09 +02:00
// Remove possible contextual '\n' and closing double quote.
2013-10-30 15:39:10 +01:00
$headers [ $header ] = preg_replace ( '~(\\\n)?"$~' , '' , $value );
2013-09-20 21:13:09 +02:00
}
return $headers ;
}
2014-07-05 08:19:16 +02:00
2024-04-30 10:39:07 +02:00
/**
* 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 ;
}
2014-07-05 08:19:16 +02:00
/**
2022-06-07 07:18:13 +02:00
* Displays or returns a Language selector .
2014-07-05 08:19:16 +02:00
*
* @ since 4.0 . 0
2015-05-19 02:33:26 +02:00
* @ since 4.3 . 0 Introduced the `echo` argument .
2016-11-21 17:14:30 +01:00
* @ since 4.7 . 0 Introduced the `show_option_site_default` argument .
2019-01-09 12:46:50 +01:00
* @ since 5.1 . 0 Introduced the `show_option_en_us` argument .
2021-11-08 23:37:59 +01:00
* @ since 5.9 . 0 Introduced the `explicit_option_en_us` argument .
2014-07-14 03:02:15 +02:00
*
* @ see get_available_languages ()
2014-08-26 21:59:16 +02:00
* @ see wp_get_available_translations ()
2014-07-14 03:02:15 +02:00
*
2014-11-18 19:47:22 +01:00
* @ param string | array $args {
* Optional . Array or string of arguments for outputting the language selector .
2014-11-13 18:01:24 +01:00
*
2017-10-04 17:23:46 +02:00
* @ type string $id ID attribute of the select element . Default 'locale' .
* @ type string $name Name attribute of the select element . Default 'locale' .
2023-08-18 19:29:20 +02:00
* @ type string [] $languages List of installed languages , contain only the locales .
2015-05-19 02:33:26 +02:00
* Default empty array .
* @ type array $translations List of available translations . Default result of
2015-07-13 23:42:24 +02:00
* wp_get_available_translations () .
2015-05-19 02:33:26 +02:00
* @ type string $selected Language which should be selected . Default empty .
2016-01-13 21:33:25 +01:00
* @ type bool | int $echo Whether to echo the generated markup . Accepts 0 , 1 , or their
* boolean equivalents . Default 1.
2015-05-19 02:33:26 +02:00
* @ type bool $show_available_translations Whether to show available translations . Default true .
2016-11-21 17:14:30 +01:00
* @ type bool $show_option_site_default Whether to show an option to fall back to the site ' s locale . Default false .
2019-01-09 12:46:50 +01:00
* @ type bool $show_option_en_us Whether to show an option for English ( United States ) . Default true .
2021-12-20 23:26:02 +01:00
* @ type bool $explicit_option_en_us Whether the English ( United States ) option uses an explicit value of en_US
2021-11-08 23:37:59 +01:00
* instead of an empty value . Default false .
2014-11-13 18:01:24 +01:00
* }
2020-01-11 19:32:05 +01:00
* @ return string HTML dropdown list of languages .
2014-07-05 08:19:16 +02:00
*/
function wp_dropdown_languages ( $args = array () ) {
2014-08-26 21:59:16 +02:00
2017-12-01 00:11:00 +01:00
$parsed_args = wp_parse_args (
2018-08-17 03:51:36 +02:00
$args ,
array (
2017-12-01 00:11:00 +01:00
'id' => 'locale' ,
'name' => 'locale' ,
'languages' => array (),
'translations' => array (),
'selected' => '' ,
'echo' => 1 ,
'show_available_translations' => true ,
'show_option_site_default' => false ,
2019-01-09 12:46:50 +01:00
'show_option_en_us' => true ,
2021-11-08 23:37:59 +01:00
'explicit_option_en_us' => false ,
2017-12-01 00:11:00 +01:00
)
);
2014-08-26 21:59:16 +02:00
2017-10-04 17:23:46 +02:00
// Bail if no ID or no name.
if ( ! $parsed_args [ 'id' ] || ! $parsed_args [ 'name' ] ) {
return ;
}
2016-11-09 00:01:31 +01:00
// English (United States) uses an empty string for the value attribute.
2021-11-08 23:37:59 +01:00
if ( 'en_US' === $parsed_args [ 'selected' ] && ! $parsed_args [ 'explicit_option_en_us' ] ) {
2017-10-04 17:08:46 +02:00
$parsed_args [ 'selected' ] = '' ;
2016-11-09 00:01:31 +01:00
}
2017-10-04 17:08:46 +02:00
$translations = $parsed_args [ 'translations' ];
2014-11-13 18:01:24 +01:00
if ( empty ( $translations ) ) {
2020-02-06 07:33:11 +01:00
require_once ABSPATH . 'wp-admin/includes/translation-install.php' ;
2014-11-13 18:01:24 +01:00
$translations = wp_get_available_translations ();
2014-08-26 21:59:16 +02:00
}
/*
2017-10-04 17:08:46 +02:00
* $parsed_args [ 'languages' ] should only contain the locales . Find the locale in
2014-08-26 21:59:16 +02:00
* $translations to get the native name . Fall back to locale .
*/
$languages = array ();
2017-10-04 17:08:46 +02:00
foreach ( $parsed_args [ 'languages' ] as $locale ) {
2014-08-26 21:59:16 +02:00
if ( isset ( $translations [ $locale ] ) ) {
$translation = $translations [ $locale ];
$languages [] = array (
'language' => $translation [ 'language' ],
'native_name' => $translation [ 'native_name' ],
2015-02-13 12:23:26 +01:00
'lang' => current ( $translation [ 'iso' ] ),
2014-08-26 21:59:16 +02:00
);
2014-11-13 18:01:24 +01:00
// Remove installed language from available translations.
unset ( $translations [ $locale ] );
2014-08-26 21:59:16 +02:00
} else {
$languages [] = array (
'language' => $locale ,
'native_name' => $locale ,
'lang' => '' ,
);
}
2014-07-05 08:19:16 +02:00
}
2017-10-04 17:08:46 +02:00
$translations_available = ( ! empty ( $translations ) && $parsed_args [ 'show_available_translations' ] );
2014-11-21 04:20:22 +01:00
2014-11-13 18:01:24 +01:00
// Holds the HTML markup.
$structure = array ();
2014-08-26 21:59:16 +02:00
// List installed languages.
2014-11-21 04:20:22 +01:00
if ( $translations_available ) {
2014-11-14 04:24:23 +01:00
$structure [] = '<optgroup label="' . esc_attr_x ( 'Installed' , 'translations' ) . '">' ;
}
2016-11-09 00:01:31 +01:00
2017-10-04 17:23:46 +02:00
// Site default.
2017-10-04 17:08:46 +02:00
if ( $parsed_args [ 'show_option_site_default' ] ) {
2016-11-09 00:01:31 +01:00
$structure [] = sprintf (
'<option value="site-default" data-installed="1"%s>%s</option>' ,
2017-10-04 17:08:46 +02:00
selected ( 'site-default' , $parsed_args [ 'selected' ], false ),
2016-11-09 00:01:31 +01:00
_x ( 'Site Default' , 'default site language' )
);
}
2019-01-09 12:46:50 +01:00
if ( $parsed_args [ 'show_option_en_us' ] ) {
2021-11-09 19:51:02 +01:00
$value = ( $parsed_args [ 'explicit_option_en_us' ] ) ? 'en_US' : '' ;
2019-01-09 12:46:50 +01:00
$structure [] = sprintf (
2021-11-08 23:37:59 +01:00
'<option value="%s" lang="en" data-installed="1"%s>English (United States)</option>' ,
esc_attr ( $value ),
2019-01-09 12:46:50 +01:00
selected ( '' , $parsed_args [ 'selected' ], false )
);
}
2016-11-09 00:01:31 +01:00
2017-12-01 00:11:00 +01:00
// List installed languages.
2014-07-05 08:19:16 +02:00
foreach ( $languages as $language ) {
2014-11-13 18:01:24 +01:00
$structure [] = sprintf (
'<option value="%s" lang="%s"%s data-installed="1">%s</option>' ,
2014-08-26 21:59:16 +02:00
esc_attr ( $language [ 'language' ] ),
esc_attr ( $language [ 'lang' ] ),
2017-10-04 17:08:46 +02:00
selected ( $language [ 'language' ], $parsed_args [ 'selected' ], false ),
2014-08-26 21:59:16 +02:00
esc_html ( $language [ 'native_name' ] )
);
2014-07-05 08:19:16 +02:00
}
2014-11-21 04:20:22 +01:00
if ( $translations_available ) {
2014-11-14 04:24:23 +01:00
$structure [] = '</optgroup>' ;
}
2014-11-13 18:01:24 +01:00
// List available translations.
2014-11-21 04:20:22 +01:00
if ( $translations_available ) {
2014-11-13 18:01:24 +01:00
$structure [] = '<optgroup label="' . esc_attr_x ( 'Available' , 'translations' ) . '">' ;
foreach ( $translations as $translation ) {
$structure [] = sprintf (
'<option value="%s" lang="%s"%s>%s</option>' ,
esc_attr ( $translation [ 'language' ] ),
2015-02-13 12:23:26 +01:00
esc_attr ( current ( $translation [ 'iso' ] ) ),
2017-10-04 17:08:46 +02:00
selected ( $translation [ 'language' ], $parsed_args [ 'selected' ], false ),
2014-11-13 18:01:24 +01:00
esc_html ( $translation [ 'native_name' ] )
);
}
$structure [] = '</optgroup>' ;
}
2017-10-04 17:23:46 +02:00
// Combine the output string.
$output = sprintf ( '<select name="%s" id="%s">' , esc_attr ( $parsed_args [ 'name' ] ), esc_attr ( $parsed_args [ 'id' ] ) );
2020-10-18 19:27:06 +02:00
$output .= implode ( " \n " , $structure );
2015-05-19 02:33:26 +02:00
$output .= '</select>' ;
2017-10-04 17:08:46 +02:00
if ( $parsed_args [ 'echo' ] ) {
2015-05-19 02:33:26 +02:00
echo $output ;
}
return $output ;
2014-07-05 08:19:16 +02:00
}
2016-08-26 12:20:29 +02:00
/**
2018-02-13 17:54:31 +01:00
* Determines whether the current locale is right - to - left ( RTL ) .
2018-03-18 15:23:33 +01:00
*
2018-02-13 17:54:31 +01:00
* For more information on this and similar theme functions , check out
2018-03-18 15:23:33 +01:00
* the { @ link https :// developer . wordpress . org / themes / basics / conditional - tags /
2018-02-13 17:54:31 +01:00
* Conditional Tags } article in the Theme Developer Handbook .
2016-08-26 12:20:29 +02:00
*
* @ since 3.0 . 0
*
2019-08-04 03:46:55 +02:00
* @ global WP_Locale $wp_locale WordPress date and time locale object .
2016-08-26 12:20:29 +02:00
*
* @ return bool Whether locale is RTL .
*/
function is_rtl () {
global $wp_locale ;
2016-08-30 07:57:41 +02:00
if ( ! ( $wp_locale instanceof WP_Locale ) ) {
return false ;
}
2016-08-26 12:20:29 +02:00
return $wp_locale -> is_rtl ();
2016-10-03 09:04:29 +02:00
}
2016-10-26 17:36:31 +02:00
/**
* Switches the translations according to the given locale .
*
* @ since 4.7 . 0
*
2019-08-04 03:47:55 +02:00
* @ global WP_Locale_Switcher $wp_locale_switcher WordPress locale switcher object .
2016-10-26 17:36:31 +02:00
*
* @ param string $locale The locale .
* @ return bool True on success , false on failure .
*/
function switch_to_locale ( $locale ) {
/* @var WP_Locale_Switcher $wp_locale_switcher */
global $wp_locale_switcher ;
2023-01-30 11:27:16 +01:00
if ( ! $wp_locale_switcher ) {
return false ;
}
2016-10-26 17:36:31 +02:00
return $wp_locale_switcher -> switch_to_locale ( $locale );
}
2023-01-30 11:27:16 +01:00
/**
* 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 );
}
2016-10-26 17:36:31 +02:00
/**
* Restores the translations according to the previous locale .
*
* @ since 4.7 . 0
*
2019-08-04 03:47:55 +02:00
* @ global WP_Locale_Switcher $wp_locale_switcher WordPress locale switcher object .
2016-10-26 17:36:31 +02:00
*
* @ return string | false Locale on success , false on error .
*/
function restore_previous_locale () {
/* @var WP_Locale_Switcher $wp_locale_switcher */
global $wp_locale_switcher ;
2023-01-30 11:27:16 +01:00
if ( ! $wp_locale_switcher ) {
return false ;
}
2016-10-26 17:36:31 +02:00
return $wp_locale_switcher -> restore_previous_locale ();
}
/**
* Restores the translations according to the original locale .
*
* @ since 4.7 . 0
*
2019-08-04 03:47:55 +02:00
* @ global WP_Locale_Switcher $wp_locale_switcher WordPress locale switcher object .
2016-10-26 17:36:31 +02:00
*
* @ return string | false Locale on success , false on error .
*/
function restore_current_locale () {
/* @var WP_Locale_Switcher $wp_locale_switcher */
global $wp_locale_switcher ;
2023-01-30 11:27:16 +01:00
if ( ! $wp_locale_switcher ) {
return false ;
}
2016-10-26 17:36:31 +02:00
return $wp_locale_switcher -> restore_current_locale ();
}
/**
2022-06-07 07:18:13 +02:00
* Determines whether switch_to_locale () is in effect .
2016-10-26 17:36:31 +02:00
*
* @ since 4.7 . 0
*
2019-08-04 03:47:55 +02:00
* @ global WP_Locale_Switcher $wp_locale_switcher WordPress locale switcher object .
2016-10-26 17:36:31 +02:00
*
* @ return bool True if the locale has been switched , false otherwise .
*/
function is_locale_switched () {
/* @var WP_Locale_Switcher $wp_locale_switcher */
global $wp_locale_switcher ;
return $wp_locale_switcher -> is_switched ();
}
2021-08-11 11:08:01 +02:00
/**
* Translates the provided settings value using its i18n schema .
*
* @ since 5.9 . 0
* @ access private
*
* @ param string | string [] | array [] | object $i18n_schema I18n schema for the setting .
* @ param string | string [] | array [] $settings Value for the settings .
* @ param string $textdomain Textdomain to use with translations .
*
* @ return string | string [] | array [] Translated settings .
*/
function translate_settings_using_i18n_schema ( $i18n_schema , $settings , $textdomain ) {
if ( empty ( $i18n_schema ) || empty ( $settings ) || empty ( $textdomain ) ) {
return $settings ;
}
if ( is_string ( $i18n_schema ) && is_string ( $settings ) ) {
return translate_with_gettext_context ( $settings , $i18n_schema , $textdomain );
}
if ( is_array ( $i18n_schema ) && is_array ( $settings ) ) {
$translated_settings = array ();
foreach ( $settings as $value ) {
$translated_settings [] = translate_settings_using_i18n_schema ( $i18n_schema [ 0 ], $value , $textdomain );
}
return $translated_settings ;
}
if ( is_object ( $i18n_schema ) && is_array ( $settings ) ) {
$group_key = '*' ;
$translated_settings = array ();
foreach ( $settings as $key => $value ) {
if ( isset ( $i18n_schema -> $key ) ) {
$translated_settings [ $key ] = translate_settings_using_i18n_schema ( $i18n_schema -> $key , $value , $textdomain );
} elseif ( isset ( $i18n_schema -> $group_key ) ) {
$translated_settings [ $key ] = translate_settings_using_i18n_schema ( $i18n_schema -> $group_key , $value , $textdomain );
} else {
$translated_settings [ $key ] = $value ;
}
}
return $translated_settings ;
}
return $settings ;
}
2022-03-14 19:01:02 +01:00
/**
* Retrieves the list item separator based on the locale .
*
* @ since 6.0 . 0
*
* @ global WP_Locale $wp_locale WordPress date and time locale object .
*
* @ return string Locale - specific list item separator .
*/
function wp_get_list_item_separator () {
global $wp_locale ;
2023-02-16 00:37:17 +01:00
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 __ ( ', ' );
}
2022-03-14 19:01:02 +01:00
return $wp_locale -> get_list_item_separator ();
}
2023-02-07 18:28:18 +01:00
/**
* Retrieves the word count type based on the locale .
*
* @ since 6.2 . 0
*
* @ global WP_Locale $wp_locale WordPress date and time locale object .
*
2023-02-08 11:17:23 +01:00
* @ return string Locale - specific word count type . Possible values are `characters_excluding_spaces` ,
* `characters_including_spaces` , or `words` . Defaults to `words` .
2023-02-07 18:28:18 +01:00
*/
function wp_get_word_count_type () {
global $wp_locale ;
2023-02-16 00:37:17 +01:00
if ( ! ( $wp_locale instanceof WP_Locale ) ) {
// Default value of WP_Locale::get_word_count_type().
return 'words' ;
}
2023-02-07 18:28:18 +01:00
return $wp_locale -> get_word_count_type ();
}
2024-09-17 22:58:15 +02:00
/**
* Returns a boolean to indicate whether a translation exists for a given string with optional text domain and locale .
*
* @ since 6.7 . 0
*
* @ param string $singular Singular translation to check .
* @ param string $textdomain Optional . Text domain . Default 'default' .
* @ param ? string $locale Optional . Locale . Default current locale .
* @ return bool True if the translation exists , false otherwise .
*/
function has_translation ( string $singular , string $textdomain = 'default' , ? string $locale = null ) : bool {
return WP_Translation_Controller :: get_instance () -> has_translation ( $singular , $textdomain , $locale );
}