Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

community.

I've been looking for an answer, but not close enough to this scenario

I have a code like this (actually working fine):

array('desc'=>'Home')

It is working defining it as text (Home) but I would rather to use it as a PHP variable due multilanguage site. Using the language package I have a variable such:

$lang['HOME'] = 'Home';

So depending on the language selected, the value changes

In other words, I need the array to have the variable value as the element

array('desc'=>'variablehere')

Can anyone plese let me know what am I missing? I have tried to set it as variable, as echo and so other ways.

Thanks a lot!

share|improve this question
1  
What's wrong with -- array('desc' => $variablehere ) ? –  Amal Murali Nov 1 '13 at 15:15

3 Answers 3

Use a translate function instead:

// It can be key-based, i.e., t('home_string'), or literal like below
t('Home');


function t($text, $language = NULL)
{
    if (!$language) {
        // determine visitor's language
    }

    // look up translation in db/file
    // if none found just return $text as is

}
share|improve this answer

Use a multi-dimensional array. e.g., given something like

$lang = 'en';
$key = 'desc';

The exact array structure depends on your needs, but it could either be primarily by language, or by key-to-translate:

language-based:

$translations = array(
   'en' => array('desc' => 'foo'),
   'fr' => array('desc' => 'bar')
);

$text_to_display = $translations['en']['desc']; // produces 'foo'

or

$translations = array(
    'desc' => array(
        'en' => array('desc' => 'foo'),
        'fr' => array('desc' => 'bar')
    )
)

$text_to_display = $translations['desc']['fr']; // produces 'bar'
share|improve this answer
    
Worked wonderful. Thanks for the help –  Belaam Adad Nov 5 '13 at 4:50

Like this?

$myArray = array('desc' => $variable);

or

$myArray = array(
    'desc' => $desc,
    'name' => $name
);

In your case:

$lang = array('HOME' => $homeVariable);
share|improve this answer
    
+1 and deleted mine :) Didn't realise you got there just before me. –  BeatAlex Nov 1 '13 at 15:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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