• Resolved dejudicibus

    (@dejudicibus)


    It looks like the __() and _e() functions do not honor the lang attribute of HTML. For example

    <html lang=”en”>

    <body>
    <?php _e(‘Red’,’textdomain’); ?>
    <div lang=”it”>
    <?php _e(‘Red’,’textdomain’); ?>
    </div>

    should return

    Red
    Rosso

    but it does not. It returns

    Red
    Red

    Any idea why?

Viewing 3 replies - 1 through 3 (of 3 total)
  • They’re not supposed to, that’s not how they work. Those functions use the language defined in WordPress’ settings and get their translations from a translation file in the theme or plugin. See the Localization page of the Theme Developer Handbook for more.

    If you’re using a static HTML attribute then you probably shouldn’t be using the translation functions at all, as the use of the attribute with a hard-coded value implies that the language won’t/can’t change.

    • This reply was modified 6 years, 11 months ago by Jacob Peattie.
    Thread Starter dejudicibus

    (@dejudicibus)

    I know how to use i18n in WordPress. The problem is not the file, but the fact that in a site of a specific language, I have pages in other languages. So, in those pages, the same plug-in should use another language file. The problem is that I do not know how to FORCE the language in a specific page.

    • This reply was modified 6 years, 11 months ago by dejudicibus.
    Thread Starter dejudicibus

    (@dejudicibus)

    SOLVED! I added this function to functions.php

    if( ! function_exists('load_specific_textdomain') ){	
    	function load_specific_textdomain( $textdomain = 'default', $locale = null ) {
    		if ( null === $locale ) {
    			$locale = is_admin() ? get_user_locale() : get_locale();
    		}
    	 
    		// Unload previously loaded strings so we can switch translations.
    		unload_textdomain( $textdomain );
    	 
    		$return = load_textdomain( $textdomain, WP_LANG_DIR . "/$locale.mo" );
    	 
    		if ( ( is_multisite() || ( defined( 'WP_INSTALLING_NETWORK' ) && WP_INSTALLING_NETWORK ) ) && ! file_exists(  WP_LANG_DIR . "/admin-$locale.mo" ) ) {
    			load_textdomain( $textdomain, WP_LANG_DIR . "/ms-$locale.mo" );
    			return $return;
    		}
    	 
    		if ( is_admin() || wp_installing() || ( defined( 'WP_REPAIRING' ) && WP_REPAIRING ) ) {
    			load_textdomain( $textdomain, WP_LANG_DIR . "/admin-$locale.mo" );
    		}
    	 
    		if ( is_network_admin() || ( defined( 'WP_INSTALLING_NETWORK' ) && WP_INSTALLING_NETWORK ) )
    			load_textdomain( $textdomain, WP_LANG_DIR . "/admin-network-$locale.mo" );
    	 
    		return $return;
    	}
    }

    then I used the following block of code:

    // $lang is the language you wish to temporarily load
    // $domain is the text domain 
    //       if $lang is null, use the current locale
    
    if ($lang != null) {
    	$clang = is_admin() ? get_user_locale() : get_locale();
    	load_specific_textdomain($domain, $lang);
    }
    
    // Place here the code containing __($string_to_be_translated, $domain)
    
    if ($lang != null) {
    	load_specific_textdomain($domain, $clang);
    }
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘__() & _e() do not support LANG attribute’ is closed to new replies.