0

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

Table

$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?

2 Answers 2

1

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()];
1
  • Thanks, knew it would be something simple, delared it global and works perfectly.
    – Vince P
    Commented Nov 8, 2012 at 14:54
1

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.

Eg.

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

    // Using filters makes your theme more extensible later
    if(is_null($langs))
        $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] : '';
}

Usage:

<?php
echo wpse71942_home(qtrans_getLanguage());

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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