_load_textdomain_just_in_time

函式
_load_textdomain_just_in_time ( $domain )
Access
Private
引數
  • (string) $domain Text domain. Unique identifier for retrieving translated strings.
    Required:
返回值
  • (bool) True when the textdomain is successfully loaded, false otherwise.
定義位置
相關方法
load_textdomainunload_textdomainload_theme_textdomainload_script_textdomainload_plugin_textdomain
引入
4.6.0
棄用
-

_load_textdomain_just_in_time: 這個函式用於及時載入特定域的翻譯檔案,這可以幫助減少WordPress的記憶體佔用。

及時載入外掛和主題文字域。

當第一次遇到文字域時,我們嘗試從`wp-content/languages`中載入翻譯檔案,不需要再呼叫load_plugin_textdomain()或load_theme_textdomain()。

function _load_textdomain_just_in_time( $domain ) {
	/** @var WP_Textdomain_Registry $wp_textdomain_registry */
	global $l10n_unloaded, $wp_textdomain_registry;

	$l10n_unloaded = (array) $l10n_unloaded;

	// Short-circuit if domain is 'default' which is reserved for core.
	if ( 'default' === $domain || isset( $l10n_unloaded[ $domain ] ) ) {
		return false;
	}

	if ( ! $wp_textdomain_registry->has( $domain ) ) {
		return false;
	}

	$locale = determine_locale();
	$path   = $wp_textdomain_registry->get( $domain, $locale );
	if ( ! $path ) {
		return false;
	}
	// Themes with their language directory outside of WP_LANG_DIR have a different file name.
	$template_directory   = trailingslashit( get_template_directory() );
	$stylesheet_directory = trailingslashit( get_stylesheet_directory() );
	if ( str_starts_with( $path, $template_directory ) || str_starts_with( $path, $stylesheet_directory ) ) {
		$mofile = "{$path}{$locale}.mo";
	} else {
		$mofile = "{$path}{$domain}-{$locale}.mo";
	}

	return load_textdomain( $domain, $mofile, $locale );
}

常見問題

FAQs
檢視更多 >