函数文档

wp_load_translations_early()

💡 云策文档标注

概述

wp_load_translations_early() 函数用于在 WordPress 初始化早期尝试加载翻译文件,主要针对初始加载过程中遇到的错误场景,如本地化检测未完成时。它适用于非标准加载序列(如 setup-config.php)或脚本即将因错误终止的情况,以避免文件重复包含的风险。

关键要点

  • 函数旨在处理早期错误,在 locale 未正确检测和加载前使用。
  • 适用于特殊加载流程(例如安装配置阶段)或错误终止场景,防止重复包含文件。
  • 通过静态变量 $loaded 确保只执行一次,避免重复加载。
  • 检查 did_action('init') 以防止在 init 钩子后执行。
  • 加载必要的翻译和本地化类库,如 WP_Textdomain_Registry、WP_Locale 等。
  • 根据 WPLANG、$wp_local_package 等定义确定 locales,并搜索多个语言目录加载 .mo 文件。
  • 最终初始化 WP_Locale 对象以支持本地化功能。

代码示例

function wp_load_translations_early() {
    global $wp_textdomain_registry, $wp_locale;
    static $loaded = false;

    if ( $loaded ) {
        return;
    }

    $loaded = true;

    if ( function_exists( 'did_action' ) && did_action( 'init' ) ) {
        return;
    }

    // We need $wp_local_package.
    require ABSPATH . WPINC . '/version.php';

    // Translation and localization.
    require_once ABSPATH . WPINC . '/pomo/mo.php';
    require_once ABSPATH . WPINC . '/l10n/class-wp-translation-controller.php';
    require_once ABSPATH . WPINC . '/l10n/class-wp-translations.php';
    require_once ABSPATH . WPINC . '/l10n/class-wp-translation-file.php';
    require_once ABSPATH . WPINC . '/l10n/class-wp-translation-file-mo.php';
    require_once ABSPATH . WPINC . '/l10n/class-wp-translation-file-php.php';
    require_once ABSPATH . WPINC . '/l10n.php';
    require_once ABSPATH . WPINC . '/class-wp-textdomain-registry.php';
    require_once ABSPATH . WPINC . '/class-wp-locale.php';
    require_once ABSPATH . WPINC . '/class-wp-locale-switcher.php';

    // General libraries.
    require_once ABSPATH . WPINC . '/plugin.php';

    $locales   = array();
    $locations = array();

    if ( ! $wp_textdomain_registry instanceof WP_Textdomain_Registry ) {
        $wp_textdomain_registry = new WP_Textdomain_Registry();
    }

    while ( true ) {
        if ( defined( 'WPLANG' ) ) {
            if ( '' === WPLANG ) {
                break;
            }
            $locales[] = WPLANG;
        }

        if ( isset( $wp_local_package ) ) {
            $locales[] = $wp_local_package;
        }

        if ( ! $locales ) {
            break;
        }

        if ( defined( 'WP_LANG_DIR' ) && @is_dir( WP_LANG_DIR ) ) {
            $locations[] = WP_LANG_DIR;
        }

        if ( defined( 'WP_CONTENT_DIR' ) && @is_dir( WP_CONTENT_DIR . '/languages' ) ) {
            $locations[] = WP_CONTENT_DIR . '/languages';
        }

        if ( @is_dir( ABSPATH . 'wp-content/languages' ) ) {
            $locations[] = ABSPATH . 'wp-content/languages';
        }

        if ( @is_dir( ABSPATH . WPINC . '/languages' ) ) {
            $locations[] = ABSPATH . WPINC . '/languages';
        }

        if ( ! $locations ) {
            break;
        }

        $locations = array_unique( $locations );

        foreach ( $locales as $locale ) {
            foreach ( $locations as $location ) {
                if ( file_exists( $location . '/' . $locale . '.mo' ) ) {
                    load_textdomain( 'default', $location . '/' . $locale . '.mo', $locale );

                    if ( defined( 'WP_SETUP_CONFIG' ) && file_exists( $location . '/admin-' . $locale . '.mo' ) ) {
                        load_textdomain( 'default', $location . '/admin-' . $locale . '.mo', $locale );
                    }

                    break 2;
                }
            }
        }

        break;
    }

    $wp_locale = new WP_Locale();
}

📄 原文内容

Attempts an early load of translations.

Description

Used for errors encountered during the initial loading process, before the locale has been properly detected and loaded.

Designed for unusual load sequences (like setup-config.php) or for when the script will then terminate with an error, otherwise there is a risk that a file can be double-included.

Source

function wp_load_translations_early() {
	global $wp_textdomain_registry, $wp_locale;
	static $loaded = false;

	if ( $loaded ) {
		return;
	}

	$loaded = true;

	if ( function_exists( 'did_action' ) && did_action( 'init' ) ) {
		return;
	}

	// We need $wp_local_package.
	require ABSPATH . WPINC . '/version.php';

	// Translation and localization.
	require_once ABSPATH . WPINC . '/pomo/mo.php';
	require_once ABSPATH . WPINC . '/l10n/class-wp-translation-controller.php';
	require_once ABSPATH . WPINC . '/l10n/class-wp-translations.php';
	require_once ABSPATH . WPINC . '/l10n/class-wp-translation-file.php';
	require_once ABSPATH . WPINC . '/l10n/class-wp-translation-file-mo.php';
	require_once ABSPATH . WPINC . '/l10n/class-wp-translation-file-php.php';
	require_once ABSPATH . WPINC . '/l10n.php';
	require_once ABSPATH . WPINC . '/class-wp-textdomain-registry.php';
	require_once ABSPATH . WPINC . '/class-wp-locale.php';
	require_once ABSPATH . WPINC . '/class-wp-locale-switcher.php';

	// General libraries.
	require_once ABSPATH . WPINC . '/plugin.php';

	$locales   = array();
	$locations = array();

	if ( ! $wp_textdomain_registry instanceof WP_Textdomain_Registry ) {
		$wp_textdomain_registry = new WP_Textdomain_Registry();
	}

	while ( true ) {
		if ( defined( 'WPLANG' ) ) {
			if ( '' === WPLANG ) {
				break;
			}
			$locales[] = WPLANG;
		}

		if ( isset( $wp_local_package ) ) {
			$locales[] = $wp_local_package;
		}

		if ( ! $locales ) {
			break;
		}

		if ( defined( 'WP_LANG_DIR' ) && @is_dir( WP_LANG_DIR ) ) {
			$locations[] = WP_LANG_DIR;
		}

		if ( defined( 'WP_CONTENT_DIR' ) && @is_dir( WP_CONTENT_DIR . '/languages' ) ) {
			$locations[] = WP_CONTENT_DIR . '/languages';
		}

		if ( @is_dir( ABSPATH . 'wp-content/languages' ) ) {
			$locations[] = ABSPATH . 'wp-content/languages';
		}

		if ( @is_dir( ABSPATH . WPINC . '/languages' ) ) {
			$locations[] = ABSPATH . WPINC . '/languages';
		}

		if ( ! $locations ) {
			break;
		}

		$locations = array_unique( $locations );

		foreach ( $locales as $locale ) {
			foreach ( $locations as $location ) {
				if ( file_exists( $location . '/' . $locale . '.mo' ) ) {
					load_textdomain( 'default', $location . '/' . $locale . '.mo', $locale );

					if ( defined( 'WP_SETUP_CONFIG' ) && file_exists( $location . '/admin-' . $locale . '.mo' ) ) {
						load_textdomain( 'default', $location . '/admin-' . $locale . '.mo', $locale );
					}

					break 2;
				}
			}
		}

		break;
	}

	$wp_locale = new WP_Locale();
}

Changelog

Version Description
3.4.0 Introduced.