load_theme_textdomain

函数
load_theme_textdomain ( $domain, $path = false )
参数
  • (string) $domain Text domain. Unique identifier for retrieving translated strings.
    Required:
  • (string|false) $path Optional. Path to the directory containing the .mo file. Default false.
    Required:
    Default: false
返回值
  • (bool) True when textdomain is successfully loaded, false otherwise.
定义位置
相关方法
load_child_theme_textdomainload_textdomainunload_textdomainload_script_textdomainload_default_textdomain
引入
1.5.0
弃用
-

load_theme_textdomain: 这是WordPress中的一个函数,用于加载一个主题的翻译文件。你可以使用这个函数来翻译你的主题的模板文件和其他PHP代码中的字符串。

加载主题的翻译字符串。

如果当前的locale在主题的根目录下以.mo文件的形式存在,它将被$domain包含在翻译后的字符串中。

.mo文件必须完全根据locale来命名。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function load_theme_textdomain( $domain, $path = false ) {
/** @var WP_Textdomain_Registry $wp_textdomain_registry */
global $wp_textdomain_registry;
/**
* Filters a theme's locale.
*
* @since 3.0.0
*
* @param string $locale The theme's current locale.
* @param string $domain Text domain. Unique identifier for retrieving translated strings.
*/
$locale = apply_filters( 'theme_locale', determine_locale(), $domain );
$mofile = $domain . '-' . $locale . '.mo';
// Try to load from the languages directory first.
if ( load_textdomain( $domain, WP_LANG_DIR . '/themes/' . $mofile, $locale ) ) {
return true;
}
if ( ! $path ) {
$path = get_template_directory();
}
$wp_textdomain_registry->set_custom_path( $domain, $path );
return load_textdomain( $domain, $path . '/' . $locale . '.mo', $locale );
}
function load_theme_textdomain( $domain, $path = false ) { /** @var WP_Textdomain_Registry $wp_textdomain_registry */ global $wp_textdomain_registry; /** * Filters a theme's locale. * * @since 3.0.0 * * @param string $locale The theme's current locale. * @param string $domain Text domain. Unique identifier for retrieving translated strings. */ $locale = apply_filters( 'theme_locale', determine_locale(), $domain ); $mofile = $domain . '-' . $locale . '.mo'; // Try to load from the languages directory first. if ( load_textdomain( $domain, WP_LANG_DIR . '/themes/' . $mofile, $locale ) ) { return true; } if ( ! $path ) { $path = get_template_directory(); } $wp_textdomain_registry->set_custom_path( $domain, $path ); return load_textdomain( $domain, $path . '/' . $locale . '.mo', $locale ); }
function load_theme_textdomain( $domain, $path = false ) {
	/** @var WP_Textdomain_Registry $wp_textdomain_registry */
	global $wp_textdomain_registry;

	/**
	 * Filters a theme's locale.
	 *
	 * @since 3.0.0
	 *
	 * @param string $locale The theme's current locale.
	 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
	 */
	$locale = apply_filters( 'theme_locale', determine_locale(), $domain );

	$mofile = $domain . '-' . $locale . '.mo';

	// Try to load from the languages directory first.
	if ( load_textdomain( $domain, WP_LANG_DIR . '/themes/' . $mofile, $locale ) ) {
		return true;
	}

	if ( ! $path ) {
		$path = get_template_directory();
	}

	$wp_textdomain_registry->set_custom_path( $domain, $path );

	return load_textdomain( $domain, $path . '/' . $locale . '.mo', $locale );
}

常见问题

FAQs
查看更多 >