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
Drop-down menu look-ups for multiple languages
Moderator: General Moderators
- mecha_godzilla
- Forum Contributor
- Posts: 375
- Joined: Wed Apr 14, 2010 4:45 pm
- Location: UK
-
JoeCommodore
- Forum Newbie
- Posts: 15
- Joined: Fri Oct 01, 2010 10:16 pm
Re: Drop-down menu look-ups for multiple languages
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.)
I use this function to make my select list....
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...
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"
)
);
?>
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;
}
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...
-
whiterainbow
- Forum Newbie
- Posts: 11
- Joined: Fri Mar 18, 2011 9:13 am
Re: Drop-down menu look-ups for multiple languages
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>- mecha_godzilla
- Forum Contributor
- Posts: 375
- Joined: Wed Apr 14, 2010 4:45 pm
- Location: UK
Re: Drop-down menu look-ups for multiple languages
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:
then further down in the same class there's this code:
In my lang_en.php script I then have an array that looks like this:
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 
Thanks,
M_G
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
}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');
}
}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!')
);
Thanks,
M_G