load_textdomain

函式
load_textdomain ( $domain, $mofile, $locale = null )
引數
  • (string) $domain Text domain. Unique identifier for retrieving translated strings.
    Required:
  • (string) $mofile Path to the .mo file.
    Required:
  • (string) $locale Optional. Locale. Default is the current locale.
    Required:
    Default: null
返回值
  • (bool) True on success, false on failure.
定義位置
相關方法
unload_textdomainload_theme_textdomainload_script_textdomainload_plugin_textdomainload_default_textdomain
引入
1.5.0
棄用
-

load_textdomain: 這是WordPress中的一個函式,用於載入一個特定文字域的翻譯檔案。你可以使用這個函式來翻譯你的WordPress網站或外掛中的字串。

將一個.mo檔案載入到文字域$domain中。

如果該文字域已經存在,翻譯將被合併。如果兩組都有相同的字串,將採取原始值的翻譯。

一旦成功,.mo檔案將被$domain放在$l10n全域性中,併成為一個MO物件。

function load_textdomain( $domain, $mofile, $locale = null ) {
	/** @var WP_Textdomain_Registry $wp_textdomain_registry */
	global $l10n, $l10n_unloaded, $wp_textdomain_registry;

	$l10n_unloaded = (array) $l10n_unloaded;

	/**
	 * Filters whether to override the .mo file loading.
	 *
	 * @since 2.9.0
	 *
	 * @param bool   $override Whether to override the .mo file loading. Default false.
	 * @param string $domain   Text domain. Unique identifier for retrieving translated strings.
	 * @param string $mofile   Path to the MO file.
	 */
	$plugin_override = apply_filters( 'override_load_textdomain', false, $domain, $mofile );

	if ( true === (bool) $plugin_override ) {
		unset( $l10n_unloaded[ $domain ] );

		return true;
	}

	/**
	 * Fires before the MO translation file is loaded.
	 *
	 * @since 2.9.0
	 *
	 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
	 * @param string $mofile Path to the .mo file.
	 */
	do_action( 'load_textdomain', $domain, $mofile );

	/**
	 * Filters MO file path for loading translations for a specific text domain.
	 *
	 * @since 2.9.0
	 *
	 * @param string $mofile Path to the MO file.
	 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
	 */
	$mofile = apply_filters( 'load_textdomain_mofile', $mofile, $domain );

	if ( ! is_readable( $mofile ) ) {
		return false;
	}

	if ( ! $locale ) {
		$locale = determine_locale();
	}

	$mo = new MO();
	if ( ! $mo->import_from_file( $mofile ) ) {
		$wp_textdomain_registry->set( $domain, $locale, false );

		return false;
	}

	if ( isset( $l10n[ $domain ] ) ) {
		$mo->merge_with( $l10n[ $domain ] );
	}

	unset( $l10n_unloaded[ $domain ] );

	$l10n[ $domain ] = &$mo;

	$wp_textdomain_registry->set( $domain, $locale, dirname( $mofile ) );

	return true;
}

常見問題

FAQs
檢視更多 >