Page 1 of 1

Drop-down menu look-ups for multiple languages

Posted: Mon Mar 05, 2012 2:28 pm
by mecha_godzilla
Hi,

I'm currently developing a web application using PHP/MySQL that allows users to schedule different types of events - as an example, when they go to the "schedule event" page they just have to choose the date and select an option from the "event type" list relating to the event they're holding, and the selection options for this list are populated from the database.

At the moment I'm only supporting English but need to translate the drop-down menu options into Spanish; which language is used will be determined by a setting in the user's account options (which is held as a $_SESSION value for quick access) but the IDs for these options will be exactly the same and on a 1-to-1 basis, so that there will be the same number of options irrespective of language and they all mean the same thing.

I'd like to implement as much of this in the database as possible, but my design theory is getting a bit fuzzy at this point - is it sensible to have all of the values held in the field the "event" relates to, so the schema might look like

TABLE: EventOptions
id
desc_EN
desc_SP
sort_order

and the query in the PHP script then just needs to select the correct field (or all of them if we're being lazy) and displays the correct one based on the user's language.

One limitation of this approach seems to be that the database schema needs to be updated each time a new language is added (as do the queries in the scripts) so would having something like a look-up table for the look-up tables be more appropriate?

Thanks in advance

Mecha Godzilla

Re: Drop-down menu look-ups for multiple languages

Posted: Fri Mar 23, 2012 12:18 am
by JoeCommodore
This is how I did it in an array on one program I wrote... (don't try to correct the spanish in there, that section never got used.)

Code: Select all

<?php
$factypelist = array(
    'en' => array(
        "lcent" => "Licensed Child Care Center",
        "excent" => "Licensed Exempt Child Care Center",
        "sfcch" => "Small Family Child Care Home",
        "lfcch" => "Large Family Child Care Home",
        "exhome" => "License Exempt Child Care Home",
        "other" => "Other"
        )
    ,'es' => array(
        "lcent" => "Instalaciones de cuidado infantil con licencia",
        "excent" => "Licencia exentas servicio del cuidado de ni�os",
        "sfcch" => "Peque�os con licencia de cuidado de ni�os de origen",
        "lfcch" => "Gran casa con licencia de cuidado de ni�os",
        "exhome" => "Exentos de licencia de cuidado de ni�os de origen",
        "other" => "Other"
        )
    );
?>
I use this function to make my select list....

Code: Select all

<?php
function selectList($inputname, $itemlist, $tool = 'na'){
    global $wform;
    $selected = $wform;
    $parts =  explode(','
        ,str_replace(']','',str_replace('[',',',$inputname)));
    foreach($parts as $element) {
        $selected = ( isset($selected[$element])
            ? $selected[$element]
            : '' );
    }
    $string = '<SELECT name="'.$inputname.'">';
    if( $tool == 'echo' ) {
        $string = $itemlist[$selected].
        '<INPUT type="hidden" name="'.$inputname.'" value="'.$selected.'">';
    } else {
        foreach( $itemlist as $key => $desc ) {
            $string .= ' <OPTION value="'.$key.'"';
            if( $key == $selected ) {
                $string .= ' SELECTED';
            }
            $string .= ">$desc</OPTION>\n";
        }
    }
    $string .= "</SELECT>\n";
    return $string;
}
and produce the select code it with a line like this: <?php echo selectList('facility_type',$factypelist[$lang]); ?>

note: my field values for this function are in an array "wform", so this example would reference $wform['facility_type']

So for doing this in a table I would suggest:

language_id
element_id
element_description

Where the element_id always stays the same but the description will be in the language defined by language_id.

That also makes it a cinch to add additional languages as well as when looking at the data, you can read the the values without having to translate...

Re: Drop-down menu look-ups for multiple languages

Posted: Fri Mar 23, 2012 5:48 am
by whiterainbow
this is the ideal place to use a definition list on the html side. I mean something like this. Then you can use jquery to hide or show the elements based on the language if you want.

Code: Select all

<dl>
<dt lang="en-US">color</dt>
<dt lang="en-GB">colour</dt>
</dl>

Re: Drop-down menu look-ups for multiple languages

Posted: Fri Mar 23, 2012 3:43 pm
by mecha_godzilla
Thank you both for your suggestions.

I haven't implemented the multi-language drop-down menus yet (which will have to be populated from the database rather than an array) but what I've done for the text in my templates is defined an array of error messages for each language, and then put each array into their own included file. As an example, my application has English as the default language:

Code: Select all

class MyApplication {
    public static $lang = 'en';
    //....continues
}
then further down in the same class there's this code:

Code: Select all

    function set_lang() {
        if ($_SESSION['lang'] != NULL) {
            require_once('languages/lang_' . $_SESSION['lang'] . '.php');
            Application::$lang = $_SESSION['lang'];
        } else {
            require_once('languages/lang_' . Application::$lang . '.php');
        }
    }
In my lang_en.php script I then have an array that looks like this:

Code: Select all

define('ICON_LANG', '<img src="images/en.png" width="16" height="11" border="0" align="" alt="en" />');

$GLOBALS['messages'] = array(
    'view' => array(
        'unauthorised' => 'Sorry, you are not authorised to access this page!')
);
Whenever I call $GLOBALS['message']['view']['unauthorised'] in my template it then displays the correct version of that error message depending on which language has been loaded, but I could probably have included all the different variations in one file and saved myself some work :D

Thanks,

M_G