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

Supposing that I have two domains in my theme named “basic” and “advance”.

Is it possible to load two different domains in my theme?

load_theme_textdomain( 'basic', TEMPLATEPATH.'/languages' );
load_theme_textdomain( 'advance', TEMPLATEPATH.'/languages' );

Related posts

4 comments

  1. 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);
    }
    
  2. If you’re creating this theme or plugin for inclusion in the official plugin or theme directories on WordPress.org, then you need to use the plugin or theme’s slug as the text-domain. You cannot have multiple text domains, and the text domain cannot be anything other than the slug of the plugin or theme.

    This is due to the new language pack support coming in 3.7. The language pack system will use the slug for the text domain, and won’t work with anything else there.

  3. You certainly can.. however, you cannot use two separate language translations on a single text string.

    For example, let’s consider we have this text string somewhere on our page:

    Hello World!
    

    Now, if we want to set that up for language localization, here is what we do:

    <?php _e('Hello World!', 'my_language_localization'); ?>
    

    In your case, it could be either:

    <?php _e('Hello World!', 'basic'); ?>
    

    Or,

    <?php _e('Hello World!', 'advance'); ?>
    

    But, it is not possible to translate the single string into two localizations.

    Also, when using a localization name; make it something unique so that it does not clash with other plugins/themes.

  4. Since WordPress 4, I suggest you create a simple plugin and “only” load the needed text domain. All strings in your Theme folders or templates or everywhere will be translated. Example:

    <?php
    
    /*
    Plugin Name: UA Translation UA Tiny select
    Description: Translation for Theme templates outside the main text domain
    Version: 1.0
    Author: Jonas Lundman
    Author URI: http://lundman.info
    Text Domain: ua-tiny-select
    Domain Path: /languages/
    */
    
    // Updated 2015 04 22 (21:00) 
    
    function ua_load_plugin_textdomain_tiny_select() {
        load_plugin_textdomain( 'ua-tiny-select', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
    }
    add_action( 'plugins_loaded', 'ua_load_plugin_textdomain_tiny_select');
    
    ?>
    

    Works great for custom changes in woocommerce template files or Buddypress template files in your main domain Theme!

    Remember to give the mo and po files the slug, in my case above:

    languages/ua-tiny-select-sv_SE.mo
    

    And the folder Languages goes in your plugin folder

Comments are closed.