加载脚本翻译的字符串。
原型
load_script_textdomain( string $handle, string $domain, string $path = null )
参数
$handle
(string)
(Required)
注册翻译域的脚本的名称。
$domain
(string)
(Required)
textdomain。
$path
(string)
(Optional)
包含转换文件的目录的完整文件路径。
返回值
(false|string)
如果无法加载脚本textdomain,则返回false,否则将转换为JSON编码的转换字符串。
源文件
路径:wp-includes/l10n.php
<?php
...
function load_script_textdomain( $handle, $domain, $path = null ) {
global $wp_scripts;
$path = untrailingslashit( $path );
$locale = determine_locale();
// If a path was given and the handle file exists simply return it.
$file_base = $domain === 'default' ? $locale : $domain . '-' . $locale;
$handle_filename = $file_base . '-' . $handle . '.json';
if ( $path && file_exists( $path . '/' . $handle_filename ) ) {
return file_get_contents( $path . '/' . $handle_filename );
}
$obj = $wp_scripts->registered[ $handle ];
/** This filter is documented in wp-includes/class.wp-scripts.php */
$src = esc_url( apply_filters( 'script_loader_src', $obj->src, $handle ) );
$relative = false;
$languages_path = WP_LANG_DIR;
$src_url = wp_parse_url( $src );
$content_url = wp_parse_url( content_url() );
$site_url = wp_parse_url( site_url() );
// If the host is the same or it's a relative URL.
if (
strpos( $src_url['path'], $content_url['path'] ) === 0 &&
( ! isset( $src_url['host'] ) || $src_url['host'] !== $content_url['host'] )
) {
// Make the src relative the specific plugin or theme.
$relative = trim( substr( $src, strlen( $content_url['path'] ) ), '/' );
$relative = explode( '/', $relative );
$languages_path = WP_LANG_DIR . '/' . $relative[0];
$relative = array_slice( $relative, 2 );
$relative = implode( '/', $relative );
} elseif ( ! isset( $src_url['host'] ) || $src_url['host'] !== $site_url['host'] ) {
if ( ! isset( $site_url['path'] ) ) {
$relative = trim( $src_url['path'], '/' );
} elseif ( ( strpos( $src_url['path'], $site_url['path'] ) === 0 ) ) {
// Make the src relative to the WP root.
$relative = substr( $src, strlen( $site_url['path'] ) );
$relative = trim( $relative, '/' );
}
}
// If the source is not from WP.
if ( false === $relative ) {
return false;
}
// Translations are always based on the unminified filename.
if ( substr( $relative, -7 ) === '.min.js' ) {
$relative = substr( $relative, 0, -7 ) . '.js';
}
$md5_filename = $file_base . '-' . md5( $relative ) . '.json';
if ( $path && file_exists( $path . '/' . $md5_filename ) ) {
return file_get_contents( $path . '/' . $md5_filename );
}
if ( file_exists( $languages_path . '/' . $md5_filename ) ) {
return file_get_contents( $languages_path . '/' . $md5_filename );
}
return false;
}
...
?>
其他
英文文档:https://developer.wordpress.org/reference/functions/load_script_textdomain/