Hello and welcome to our community! Is this your first visit?
Register
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
    Location
    Carrot River, Saskatchewan
    Posts
    842
    Thanks
    15
    Thanked 9 Times in 9 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
    http://www.dniwebdesign.com

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 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.);

    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'];
         }
         else
         {
              
    $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'];
         }
         
         
    mysql_free_result($languagebase);
         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.


  •  

    Posting Permissions

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