0

I'm creating a plugin that supports internationalization.

Everything is working fine, except for global variables.

Strings in global variables will not get the appropriate translation.

Explanation:

_e('Hello World','text-domain'); // Gets translated

$var = __('Hello Global World','text-domain');
function fun() {
    global $var;
    echo $var; // Doesn't get translated
}

Any clue on how to fix this behaviour?

Update: There is a similar issue, without answer, in this question Wordpress Localization and Templating

0

Translation works with global variables, you just need to wait until the proper hook to call the translation functions. I haven't looked at the code or documentation, but just by experimenting, the translations are loaded sometime before the after_setup_theme hook, but after the other hooks that fire before that.

That means that if you try to translate something on the plugins_loaded hook, you're not going to get a translated result back.

As an example, in the following plugin, the first time $dash is "translated" in the main plugin file, it won't be. It will always print "Dashboard" no matter what. Same thing on the plugins_loaded hook.

The fun part is in the after_setup_theme hook where the global $dash prints in English, but the newly translated text prints (in my case) "Escritorio".

On the init hook, the global $dash prints the translated text. I added a callback to a closure to make sure that it was displaying the global $dash variable, and it was.

/**
 * Plugin Name: StackExchange Sample
 */

namespace StackExchange\WordPress;

$dash = __( 'Dashboard ' );
printf( '<p>Main plugin file: %1$s</p>', $dash );  // Dashboard

class plugin {
  public function plugins_loaded() {
    \add_action( 'after_setup_theme', [ $this, 'after_setup_theme' ] );
    \add_action( 'init', [ $this, 'init' ] );

    echo '<h2>plugins_loaded</h2>';
    global $dash;
    printf( '<p>Global: %1$s</p>', $dash );  // Dashboard
    $dash = __( 'Dashboard' );
    printf( '<p>Local: %1$s</p>', $dash );  // Dashboard
  }
  public function after_setup_theme() {
    echo '<h2>after_setup_theme</h2>';
    global $dash;
    printf( '<p>Global: %1$s</p>', $dash );  // Dashboard
    $dash = __( 'Dashboard' );
    printf( '<p>Local: %1$s</p>', $dash );  // Escritorio
  }
  public function init() {
    echo '<h2>init</h2>';
    global $dash;
    printf( '<p>Global: %1$s</p>', $dash );  // Escritorio
    $dash = __( 'Dashboard' );
    printf( '<p>Local: %1$s</p>', $dash );  // Escritorio
  }
}
\add_action( 'plugins_loaded', [ new plugin(), 'plugins_loaded' ] );
\add_action( 'init', function() {
  global $dash;
  printf( '<p>Global: %1$s</p>', $dash );  // Escritorio
}, 20 );

In summation: global variables with translated text do work, make sure you wait until after the after_setup_theme hook to translate your text, and don't use global variables because it'll just cause problems.

1

In general you should not have global variables.

Specifically, if your file is not loaded at the global scope $var is not going o be global unless you explicitly declare it as such. Your problem is unlikely to be related to the actual translation.

  • I don't have issues in using the global variable, the issue is only in translating it. If I make the variable local, the translation works – Michele Fortunato Feb 20 '18 at 14:40
  • 2
    which is exactly what I said. first never use globals ever in any place. second you assumption that $var is global is probably wrong and this is the only explanation unless there are parts of the code you do not show. – Mark Kaplun Feb 20 '18 at 17:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.