函数文档

load_textdomain()

💡 云策文档标注

概述

load_textdomain() 函数用于将 .mo 文件加载到指定的文本域 $domain 中,支持翻译文件的合并与覆盖机制。成功加载后,翻译数据将存储在全局变量 $l10n 中,并返回布尔值表示操作结果。

关键要点

  • 函数加载 .mo 文件到文本域,若文本域已存在则合并翻译,相同字符串优先使用原始值。
  • 参数包括必需的 $domain(文本域标识符)和 $mofile(.mo 文件路径),以及可选的 $locale(区域设置,默认为当前区域)。
  • 返回布尔值:成功为 true,失败为 false。
  • 支持多个过滤器(如 pre_load_textdomain、override_load_textdomain)和动作(如 load_textdomain)以自定义加载行为。
  • 从 WordPress 6.5.0 开始,支持通过 translation_file_format 过滤器选择优先的翻译文件格式(如 'php' 或 'mo')。
  • 内部使用 WP_Translation_Controller 和 WP_Textdomain_Registry 来管理翻译文件和区域设置。

代码示例

if ( is_textdomain_loaded( $plugin ) ) {
    unload_textdomain( $plugin );
}
$mofile = sprintf( '%s-%s.mo', $plugin, $locale );
$domain_path = path_join( WP_LANG_DIR, 'plugins' );
$loaded = load_textdomain( $plugin, path_join( $domain_path, $mofile ) );
if ( ! $loaded ) {
    $domain_path = path_join( WP_PLUGIN_DIR, "{$plugin}/languages" );
    load_textdomain( $plugin, path_join( $domain_path, $mofile ) );
}

注意事项

  • 在多语言网站中,可能需要先卸载已加载的文本域,再加载特定区域的翻译文件。
  • 函数依赖于全局变量 $l10n 和 $l10n_unloaded,以及 WP_Textdomain_Registry 实例。
  • 从版本 6.1.0 开始添加了 $locale 参数,允许指定区域设置。

📄 原文内容

Loads a .mo file into the text domain $domain.

Description

If the text domain already exists, the translations will be merged. If both sets have the same string, the translation from the original value will be taken.

On success, the .mo file will be placed in the $l10n global by $domain and will be a MO object.

Parameters

$domainstringrequired
Text domain. Unique identifier for retrieving translated strings.
$mofilestringrequired
Path to the .mo file.
$localestringoptional
Locale. Default is the current locale.

Default:null

Return

bool True on success, false on failure.

Source

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;

	if ( ! is_string( $domain ) ) {
		return false;
	}

	/**
	 * Filters whether to short-circuit loading .mo file.
	 *
	 * Returning a non-null value from the filter will effectively short-circuit
	 * the loading, returning the passed value instead.
	 *
	 * @since 6.3.0
	 *
	 * @param bool|null   $loaded The result of loading a .mo file. Default null.
	 * @param string      $domain Text domain. Unique identifier for retrieving translated strings.
	 * @param string      $mofile Path to the MO file.
	 * @param string|null $locale Locale.
	 */
	$loaded = apply_filters( 'pre_load_textdomain', null, $domain, $mofile, $locale );
	if ( null !== $loaded ) {
		if ( true === $loaded ) {
			unset( $l10n_unloaded[ $domain ] );
		}

		return $loaded;
	}

	/**
	 * Filters whether to override the .mo file loading.
	 *
	 * @since 2.9.0
	 * @since 6.2.0 Added the `$locale` parameter.
	 *
	 * @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.
	 * @param string|null $locale   Locale.
	 */
	$plugin_override = apply_filters( 'override_load_textdomain', false, $domain, $mofile, $locale );

	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 ( ! $locale ) {
		$locale = determine_locale();
	}

	$i18n_controller = WP_Translation_Controller::get_instance();

	// Ensures the correct locale is set as the current one, in case it was filtered.
	$i18n_controller->set_locale( $locale );

	/**
	 * Filters the preferred file format for translation files.
	 *
	 * Can be used to disable the use of PHP files for translations.
	 *
	 * @since 6.5.0
	 *
	 * @param string $preferred_format Preferred file format. Possible values: 'php', 'mo'. Default: 'php'.
	 * @param string $domain           The text domain.
	 */
	$preferred_format = apply_filters( 'translation_file_format', 'php', $domain );
	if ( ! in_array( $preferred_format, array( 'php', 'mo' ), true ) ) {
		$preferred_format = 'php';
	}

	$translation_files = array();

	if ( 'mo' !== $preferred_format ) {
		$translation_files[] = substr_replace( $mofile, ".l10n.$preferred_format", - strlen( '.mo' ) );
	}

	$translation_files[] = $mofile;

	foreach ( $translation_files as $file ) {
		/**
		 * Filters the file path for loading translations for the given text domain.
		 *
		 * Similar to the 'load_textdomain_mofile' filter with the difference that
		 * the file path could be for an MO or PHP file.
		 *
		 * @since 6.5.0
		 * @since 6.6.0 Added the `$locale` parameter.
		 *
		 * @param string $file   Path to the translation file to load.
		 * @param string $domain The text domain.
		 * @param string $locale The locale.
		 */
		$file = (string) apply_filters( 'load_translation_file', $file, $domain, $locale );

		$success = $i18n_controller->load_file( $file, $domain, $locale );

		if ( $success ) {
			if ( isset( $l10n[ $domain ] ) && $l10n[ $domain ] instanceof MO ) {
				$i18n_controller->load_file( $l10n[ $domain ]->get_filename(), $domain, $locale );
			}

			// Unset NOOP_Translations reference in get_translations_for_domain().
			unset( $l10n[ $domain ] );

			$l10n[ $domain ] = new WP_Translations( $i18n_controller, $domain );

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

			return true;
		}
	}

	return false;
}

Hooks

do_action( ‘load_textdomain’, string $domain, string $mofile )

Fires before the MO translation file is loaded.

apply_filters( ‘load_textdomain_mofile’, string $mofile, string $domain )

Filters MO file path for loading translations for a specific text domain.

apply_filters( ‘load_translation_file’, string $file, string $domain, string $locale )

Filters the file path for loading translations for the given text domain.

apply_filters( ‘override_load_textdomain’, bool $override, string $domain, string $mofile, string|null $locale )

Filters whether to override the .mo file loading.

apply_filters( ‘pre_load_textdomain’, bool|null $loaded, string $domain, string $mofile, string|null $locale )

Filters whether to short-circuit loading .mo file.

apply_filters( ‘translation_file_format’, string $preferred_format, string $domain )

Filters the preferred file format for translation files.

Changelog

Version Description
6.1.0 Added the $locale parameter.
1.5.0 Introduced.

User Contributed Notes

  1. Skip to note 3 content

    There are situations where one requires a plugin translation locale to be loaded which is different from the current user locale.

    For example, in multilingual websites, creating a translation of a post/widget may require some translations to be loaded for a given plugin text domain. The user locale (dashboard locale) is loaded by default, so it is important to unload that if it has been loaded already and seek the translation file to load for the text domain for the requested locale,

    if ( is_textdomain_loaded( $plugin ) ) {
    	unload_textdomain( $plugin );
    }
    $mofile = sprintf( '%s-%s.mo', $plugin, $locale );
    //check the installation language path first.
    $domain_path = path_join( WP_LANG_DIR, 'plugins' );
    $loaded = load_textdomain( $plugin, path_join( $domain_path, $mofile ) );
    
    if ( ! $loaded ) { //else, check the plugin language folder.
    	$domain_path = path_join( WP_PLUGIN_DIR, "{$plugin}/languages" );
    	load_textdomain( $plugin, path_join( $domain_path, $mofile ) );
    }