加载脚本翻译的字符串。

原型

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?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/