I'm using a lookup table to define a few things for the qTranslate plugin


$trans_home = array('en' => 'Home','it' => 'Home','de' => 'Home','zh' => '首页','es' => 'Página de inicio','fr' => 'Page d\'accueil');
$trans_company = array('en' => 'Company','it' => 'L\'azienda','de' => 'Firma','zh' => '公司','es' => 'Empresa','fr' => 'Société');

Code calling up lookup table

echo $trans_home[qtrans_getLanguage()];
echo $trans_company[qtrans_getLanguage()];

I thought that if I put the lookup table in the header file I would be able to call on the table throughout the template, but it seems I need to define the lookup table in each part of the template I need it in, so for example I would have to define the table in both in the header and footer for it to work.

So the question is how do I define a lookup table that will work across all PHP elements of the template?


Loading it in the header should work, or on the init hook. You will need to declare it global as well. So...

// define
global $trans_home;
$trans_home = array('en' => 'Home','it' => 'Home','de' => 'Home','zh' => '首页','es' => 'Página de inicio','fr' => 'Page d\'accueil');

// and use in your templates
global $trans_home;
echo $trans_home[qtrans_getLanguage()];
| improve this answer | |
  • Thanks, knew it would be something simple, delared it global and works perfectly. – Vince Pettit Nov 8 '12 at 14:54

I would use some sort of function or object to wrap things up, rather than a global variable. WordPress has plenty of those already, no need to add more.


function wpse71942_home($l)
    // only set it once
    static $langs = null;

    // Using filters makes your theme more extensible later
        $langs = apply_filters('wpse71942_home_trans', array('en' => 'Home','it' => 'Home','de' => 'Home','zh' => '首页','es' => 'Página de inicio','fr' => 'Page d\'accueil'));

    return isset($langs[$l]) ? $langs[$l] : '';


echo wpse71942_home(qtrans_getLanguage());

Note that the current language is passed in -- it's a bit more "loosely coupled" with qTranslate.

| improve this answer | |

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.