0

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!

1
  • 1
    What's wrong with -- array('desc' => $variablehere ) ? Commented Nov 1, 2013 at 15:15

3 Answers 3

1

Like this?

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

or

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

In your case:

$lang = array('HOME' => $homeVariable);
Sign up to request clarification or add additional context in comments.

1 Comment

+1 and deleted mine :) Didn't realise you got there just before me.
0

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'

Comments

0

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

}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.