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來命名。

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
檢視更多 >