Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Carrot River, Saskatchewan
    Thanked 10 Times in 10 Posts

    php language packs?

    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?
    Dawson Irvine
    CEO - DNI Web Design

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Saskatoon, Saskatchewan
    Thanked 2,662 Times in 2,631 Posts
    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:
    PHP Code:
    // 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:
    PHP Code:
    $langroups = array('global''news'... etc.);

    // 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'];

    $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.


    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts