函数文档

load_child_theme_textdomain()

💡 云策文档标注

概述

load_child_theme_textdomain() 函数用于加载子主题的翻译字符串,通过指定文本域和路径来包含 .mo 文件。它通常与 after_setup_theme 钩子配合使用,确保国际化正确加载。

关键要点

  • 函数作用:加载子主题的翻译字符串,基于当前语言环境从子主题根目录或指定路径查找 .mo 文件。
  • 参数说明:$domain 为必需参数,是文本域的唯一标识符;$path 为可选参数,默认为 false,表示使用 get_stylesheet_directory() 获取子主题目录。
  • 返回值:成功加载返回 true,否则返回 false。
  • 使用建议:应在 after_setup_theme 钩子中调用,类似于 load_theme_textdomain()。
  • 文件命名:.mo 文件必须基于语言环境精确命名(如 de_DE.mo),不能包含文本域前缀,这与插件语言文件不同。

代码示例

/**
 * Loads the child theme textdomain.
 */
function wpdocs_child_theme_setup() {
    load_child_theme_textdomain( 'my_parent_theme', get_stylesheet_directory() . '/languages' );
}
add_action( 'after_setup_theme', 'wpdocs_child_theme_setup' );

注意事项

  • 确保 .mo 文件放置在正确路径,如子主题的 languages 文件夹,且文件名仅包含语言代码。
  • 回调函数名称可自定义,但需与 add_action 中的第二个参数匹配;也可使用匿名函数(PHP 5.3+)。
  • 文本域参数应使用父主题的目录名,而非子主题名称。

📄 原文内容

Loads the child theme’s translated strings.

Description

If the current locale exists as a .mo file in the child theme’s root directory, it will be included in the translated strings by the $domain.

The .mo files must be named based on the locale exactly.

Parameters

$domainstringrequired
Text domain. Unique identifier for retrieving translated strings.
$pathstring|falseoptional
Path to the directory containing the .mo file.

Default:false

Return

bool True when the theme textdomain is successfully loaded, false otherwise.

More Information

Internationalization and localization (other common spellings are internationalisation and localisation) are means of adapting computer software to different languages.

  • l10n is an abbreviation for localization.
  • i18n 18 stands for the number of letters between the first i and last n in internationalization.

Source

function load_child_theme_textdomain( $domain, $path = false ) {
	if ( ! $path ) {
		$path = get_stylesheet_directory();
	}
	return load_theme_textdomain( $domain, $path );
}

Changelog

Version Description
2.9.0 Introduced.

User Contributed Notes

  1. Skip to note 3 content

    The load_child_theme_textdomain() function should generally be called from within the after_setup_theme action hook, just the same as with its related load_theme_textdomain() function.

    /**
     * Loads the child theme textdomain.
     */
    function wpdocs_child_theme_setup() {
        load_child_theme_textdomain( 'my_parent_theme', get_stylesheet_directory() . '/languages' );
    }
    add_action( 'after_setup_theme', 'wpdocs_child_theme_setup' );

    ‘my_parent_theme’ = The name of the Main theme

    The .mo files must use language-only filenames, like languages/de_DE.mo in your child theme directory.

    Unlike plugin language files, a name like my_child_theme-de_DE.mo will NOT work. Although plugin language files allow you to specify the text-domain in the filename, this will NOT work with themes and child themes. Language files for themes should include the language shortcut ONLY.

  2. Skip to note 4 content

    Might be useful for beginners to also know that in the note above
    ‘wpdocs_child_theme_setup’ = The name of your child theme,
    hence that needs to be changed in line 4 and line 7 in the code snippet shown above.
    And in that same example the .mo and .po files are not uploaded to the child-theme root, but to a folder named languages inside the child-folder.