View Full Version : php language packs?

01-18-2005, 07:12 PM
Just curious in how hard it would be to create and implement a language pack for my scripts. How would I go about doing this?

01-18-2005, 08:49 PM
Its not really difficult at all, but it depends completely on what you have. Instead of doing any word hardcoded, you would create say a phrase index. So you would take your database, add a phrase table that has say, a variable name, a language set (this I would do just an id for, and link it to another table for the languages), and a replacement set. Actually, it depends on how much replacement you want to do, you may want to add a pageset or something for it, like so:

// Pretend this is generated by a query, we just want to see what it would look like.
$lang['id'] = 1; // This is an auto incrementing number, not nessessary, but nice to have.
$lang['language'] = 2; // Lets say 2 is equal to english.
$lang['variable'] = 'replace_my_words_with_phrase';
$lang['replacement'] = 'I\'m being replaced by an English phrase.';
$lang['group'] = 'global';

Ok, so we have a table, inside is phrases that we would like to use. I have added the group option to help balance the load a little bit. To use a group option you would need to state it at the beginning of a script:

$langroups = array('global', 'news'... etc.);

function language()
global $langroups, $userprefs, $defprefs, $langroups;
// We are assuming that $defprefs and $userprefs are already defined and usable.
if (isset($userprefs['languageset']) AND $userprefs['languageset'] != -1)
$langset = $userprefs['language'];
$langset = $defprefs['language'];
/* We have a language set, so default is english, but a user has stated spanish.
Good to go. If user has stated nothing, or set = -1, revert to default.

$languagebase = mysql_query("
SELECT lang.id, lang.charset, lang.title, words.id AS wordid, words.language, words.variable, words.replacement
FROM languages AS lang
LEFT JOIN words AS words
ON lang.id = words.language
WHERE lang.id='" . $langset . " AND words.group IN ('" . implode("', '", $langroups) . "')

// If you have a lot of words to replace, you may want to use two queries, as I find a join takes longer.
while ($conver = mysql_fetch_array($languagebase))
$langwords["$conver[variable]"] = $conver['replacement'];

return $langwords;

$lang = language();
// To use:

echo $lang['welcome_to_my_site'];
// This should display the words, Welcome To My Site, in which ever language is now set.

Please note that I have not tested this code ONE BIT. It should work, but its different than the one that I have used. The most important thing to remember IMO is to add the groups section to it, so that you can maximize the number of words used and not overkill your database. In either case, it would always be extremely extensive, and expect a higher load time. You could attempt to cache the information if you would like, though I'm not certain how that will really effect your load times in the long run.