load_child_theme_textdomain()WP 2.9.0

Loads the translation file of the child theme (.mo) into memory for further processing.

This is a wrapper for load_theme_textdomain(). It is created for the convenient inclusion of the translation file for the child theme.

Creating a translation for the child theme will positively affect the optimization and structuring of files. For example, if the twentyfifteen theme is used, translation files are already included via load_theme_textdomain(), and there is already a translation in the languages folder. We created a child theme with many new strings for translation. We can add these strings to the parent theme files, but we will lose the changes upon updating. To prevent this, another translation file needs to be created.

.mo must be named exactly as the locale, for example, ru_RU.mo

Should be called during the after_setup_theme hook.

No Hooks.

Returns

true|false. True when the .mo file is loaded, false otherwise.

Usage

load_child_theme_textdomain( $domain, $path );
$domain(string) (required)
Identifier that will be used later in translation functions: __() _e() can refer specifically to this translation string. It can be the same as the parent theme's.
$path(string)
Path to the folder containing the .mo file. By default, it specifies the path to the child theme directory: get_stylesheet_directory().
Default: false

Examples

1

#1 Call it from within the after_setup_theme action

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.

add_action( 'after_setup_theme', 'wpdocs_child_theme_setup' );

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

Notes:

  • .mo and .po files are not uploaded to the child-theme root, but to a folder named languages inside the child-folder.

  • my_parent_theme = main theme textdomain. It's normaly the same as folder 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.
0

#2 Connect the translation file of the child theme

add_action( 'after_setup_theme', 'my_child_theme_setup' );

function my_child_theme_setup(){
	load_child_theme_textdomain( 'my_child_theme', get_stylesheet_directory() . '/languages' );
}

The .mo file must be in the languages folder in the subtopic and have the name of the language locale, for example: languages/de_DE.mo.

Changelog

Since 2.9.0 Introduced.

load_child_theme_textdomain() code WP 6.9.1

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