Can we load “load_theme_textdomain” multiple times with different domains?

Note that if you put your mo-files inside your theme’s directory, the same mo-file will be loaded for every theme textdomain. That is because a theme mo-file is based solely on the locale. For example:

load_theme_textdomain( 'basic', get_template_directory() . '/languages' );
load_theme_textdomain( 'advance', get_template_directory() . '/languages' );
load_theme_textdomain( 'whatever', get_template_directory() . '/languages' );

Given that “en_US” is your WPLANG locale, the code above will load wp-content/themes/your-theme/languages/en_US.mo three times! Bad because having the same mo-file imported multiple times only causes superfluous work. In that case you’re better off using a single theme domain.

However, if the wp-content/themes/your-theme/languages/en_US.mo does not exist, WordPress will have a look in the subdirectory “themes” of the default language directory and in that case it does take the domain into account as part of the filename. You can learn all that from a quick look at the load_theme_textdomain() function:

function load_theme_textdomain( $domain, $path = false ) {
    $locale = apply_filters( 'theme_locale', get_locale(), $domain );

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

    // Load the textdomain from the Theme provided location, or theme directory first
    $mofile = "{$path}/{$locale}.mo";
    if ( $loaded = load_textdomain($domain, $mofile) )
        return $loaded;

    // Else, load textdomain from the Language directory
    $mofile = WP_LANG_DIR . "/themes/{$domain}-{$locale}.mo";
    return load_textdomain($domain, $mofile);
}

Leave a Comment

tech