load_theme_textdomain( string $domain, string|false $path = false ): bool

Loads the theme’s translated strings.

Description

If the current locale exists as a .mo file in the 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 textdomain is successfully loaded, false otherwise.

Source

function load_theme_textdomain( $domain, $path = false ) {
	/** @var WP_Textdomain_Registry $wp_textdomain_registry */
	global $wp_textdomain_registry;

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

	/**
	 * Filters a theme's locale.
	 *
	 * @since 3.0.0
	 *
	 * @param string $locale The theme's current locale.
	 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
	 */
	$locale = apply_filters( 'theme_locale', determine_locale(), $domain );

	$mofile = $domain . '-' . $locale . '.mo';

	// Try to load from the languages directory first.
	if ( load_textdomain( $domain, WP_LANG_DIR . '/themes/' . $mofile, $locale ) ) {
		return true;
	}

	if ( ! $path ) {
		$path = get_template_directory();
	}

	$wp_textdomain_registry->set_custom_path( $domain, $path );

	return load_textdomain( $domain, $path . '/' . $locale . '.mo', $locale );
}

Hooks

apply_filters( ‘theme_locale’, string $locale, string $domain )

Filters a theme’s locale.

Changelog

VersionDescription
4.6.0The function now tries to load the .mo file from the languages directory first.
1.5.0Introduced.

User Contributed Notes

  1. Skip to note 3 content

    1st example
    The load_theme_textdomain() function should generally be called from within the after_setup_theme action hook.

    add_action('after_setup_theme', 'wpdocs_theme_setup');
    
    /**
     * Load translations for wpdocs_theme
     */
    function wpdocs_theme_setup(){
        load_theme_textdomain('wpdocs_theme', get_template_directory() . '/languages');
    }

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

    Unlike plugin language files, a name like my_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. Language files for themes should include the language shortcut ONLY.

  2. Skip to note 4 content

    2nd example
    you can use this example if you wish to switch theme language using a variable passed within the URL, for example to load the Tamazikht language, your URL would look like; www.example.com/?l=tz_MA, this will search for a .mo file with name tz_MA.mo in the language directory inside your theme.

    // CHANGE LOCAL LANGUAGE
    // must be called before load_theme_textdomain()
    
    add_filter( 'locale', 'wpdocs_theme_localized' );
    
    /**
     * Switch to locale given as query parameter l, if present
     */
    function wpdocs_theme_localized( $locale )
    {
    	if ( isset( $_GET['l'] ) )
    	{
    		return sanitize_key( $_GET['l'] );
    	}
    
    	return $locale;
    }
    
    // SET THEME LANGUAGES DIRECTORY
    // Theme translations can be filed in the my_theme/languages/ directory
    // WordPress translations can be filed in the wp-content/languages/ directory
    load_theme_textdomain( 'wpdocs_theme_textdomain', get_template_directory().'/languages' );

You must log in before being able to contribute a note or feedback.