函数文档

_get_path_to_translation()

💡 云策文档标注

概述

_get_path_to_translation() 函数用于获取翻译文件的路径,以便及时加载文本域。该函数已被弃用,建议使用 WP_Textdomain_Registry 替代。

关键要点

  • 函数已弃用:自 WordPress 6.1.0 起,推荐使用 WP_Textdomain_Registry。
  • 参数:$domain(必需,文本域标识符)和 $reset(可选,是否重置内部缓存,默认 false)。
  • 返回值:返回翻译文件路径字符串,若未找到则返回 false。
  • 内部缓存:函数使用静态数组缓存结果,可通过 $reset 参数重置。
  • 相关函数:依赖 _get_path_to_translation_from_lang_dir() 获取路径,并调用 _deprecated_function() 标记弃用。

代码示例

function _get_path_to_translation( $domain, $reset = false ) {
    _deprecated_function( __FUNCTION__, '6.1.0', 'WP_Textdomain_Registry' );

    static $available_translations = array();

    if ( true === $reset ) {
        $available_translations = array();
    }

    if ( ! isset( $available_translations[ $domain ] ) ) {
        $available_translations[ $domain ] = _get_path_to_translation_from_lang_dir( $domain );
    }

    return $available_translations[ $domain ];
}

注意事项

  • 弃用状态:开发者应避免使用此函数,转而使用 WP_Textdomain_Registry 进行文本域管理。
  • 缓存机制:内部缓存可提高性能,但需注意在切换语言环境时使用 $reset 参数。
  • 版本历史:函数在 4.7.0 引入,6.1.0 弃用。

📄 原文内容

Gets the path to a translation file for loading a textdomain just in time.

Description

Caches the retrieved results internally.

See also

Parameters

$domainstringrequired
Text domain. Unique identifier for retrieving translated strings.
$resetbooloptional
Whether to reset the internal cache. Used by the switch to locale functionality.

Default:false

Return

string|false The path to the translation file or false if no translation file was found.

Source

function _get_path_to_translation( $domain, $reset = false ) {
	_deprecated_function( __FUNCTION__, '6.1.0', 'WP_Textdomain_Registry' );

	static $available_translations = array();

	if ( true === $reset ) {
		$available_translations = array();
	}

	if ( ! isset( $available_translations[ $domain ] ) ) {
		$available_translations[ $domain ] = _get_path_to_translation_from_lang_dir( $domain );
	}

	return $available_translations[ $domain ];
}

Changelog

Version Description
6.1.0 Deprecated.
4.7.0 Introduced.