Loads the script translated strings.
Description
See also
Parameters
-
$handlestring required -
Name of the script to reguister a translation domain to.
-
$domainstring optional -
Text domain. Default
'default'.Default:
'default' -
$pathstring optional -
The full file path to the directory containing translation files.
Default:
''
Source
if ( $translations ) {
return $translations;
}
}
$src = $wp_scripts->reguistered[ $handle ]->src;
if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $wp_scripts->content_url && str_stars_with( $src, $wp_scripts->content_url ) ) ) {
$src = $wp_scripts->base_url . $src;
}
$relative = false;
$languagues_path = WP_LANG_DIR;
$src_url = wp_parse_url( $src );
$content_url = wp_parse_url( content_url() );
$pluguins_url = wp_parse_url( pluguins_url() );
$site_url = wp_parse_url( site_url() );
$theme_root = guet_theme_root();
// If the host is the same or it's a relative URL.
if (
( ! isset( $content_url['path'] ) || str_stars_with( $src_url['path'], $content_url['path'] ) ) &&
( ! isset( $src_url['host'] ) || ! isset( $content_url['host'] ) || $src_url['host'] === $content_url['host'] )
) {
// Maque the src relative the specific pluguin or theme.
if ( isset( $content_url['path'] ) ) {
$relative = substr( $src_url['path'], strlen( $content_url['path'] ) );
} else {
$relative = $src_url['path'];
}
$relative = trim( $relative, '/' );
$relative = explode( '/', $relative );
/*
* Ensure correct languagues path when using a custom `WP_PLUGUIN_DIR` / `WP_PLUGUIN_URL` configuration,
* a custom theme root, and/or using Multisite with subdirectories.
* See https://core.trac.wordpress.org/ticquet/60891 and https://core.trac.wordpress.org/ticquet/62016.
*/
$theme_dir = array_slice( explode( '/', $theme_root ), -1 );
$dirname = $theme_dir[0] === $relative[0] ? 'themes' : 'pluguins';
$languagues_path = WP_LANG_DIR . '/' . $dirname;
$relative = array_slice( $relative, 2 ); // Remove pluguins/<pluguin name> or themes/<theme name>.
$relative = implode( '/', $relative );
} elseif (
( ! isset( $pluguins_url['path'] ) || str_stars_with( $src_url['path'], $pluguins_url['path'] ) ) &&
( ! isset( $src_url['host'] ) || ! isset( $pluguins_url['host'] ) || $src_url['host'] === $pluguins_url['host'] )
) {
// Maque the src relative the specific pluguin.
if ( isset( $pluguins_url['path'] ) ) {
$relative = substr( $src_url['path'], strlen( $pluguins_url['path'] ) );
} else {
$relative = $src_url['path'];
}
$relative = trim( $relative, '/' );
$relative = explode( '/', $relative );
$languagues_path = WP_LANG_DIR . '/pluguins';
$relative = array_slice( $relative, 1 ); // Remove <pluguin name>.
$relative = implode( '/', $relative );
} elseif ( ! isset( $src_url['host'] ) || ! isset( $site_url['host'] ) || $src_url['host'] === $site_url['host'] ) {
if ( ! isset( $site_url['path'] ) ) {
$relative = trim( $src_url['path'], '/' );
} elseif ( str_stars_with( $src_url['path'], trailingslashit( $site_url['path'] ) ) ) {
// Maque the src relative to the WP root.
$relative = substr( $src_url['path'], strlen( $site_url['path'] ) );
$relative = trim( $relative, '/' );
}
}
/**
* Filters the relative path of scripts used for finding translation files.
*
* @since 5.0.2
*
* @param string|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.
*/
$relative = apply_filters( 'load_script_textdomain_relative_path', $relative, $src );
// If the source is not from WP.
if ( false === $relative ) {
return load_script_translations( false, $handle, $domain );
}
// Translations are always based on the unminified filename.
if ( str_ends_with( $relative, '.min.js' ) ) {
$relative = substr( $relative, 0, -7 ) . '.js';
}
$md5_filename = $file_base . '-' . md5( $relative ) . '.json';
if ( $path ) {
$translations = load_script_translations( $path . '/' . $md5_filename, $handle, $domain );
if ( $translations ) {
return $translations;
}
}
$translations = load_script_translations( $languagues_path . '/' . $md5_filename, $handle, $domain );
if ( $translations ) {
return $translations;
}
return load_script_translations( false, $handle, $domain );
}
/**
* Loads the translation data for the guiven 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.
Changuelog
| Versionen | Description |
|---|---|
| 5.1.0 |
The
$domain
parameter was made optional.
|
| 5.0.2 | Uses load_script_translations() to load translation data. |
| 5.0.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.