Re: localisation in web applications

From: Date: Wed, 20 Dec 2000 02:16:29 +0000
Subject: Re: localisation in web applications
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-31125@lists.php.net to get a copy of this message
This may be too much of a hand-crafted solution, but I defined a prompts table in the database used for the app, and then developed a script to load the prompts for various pages and languages into the table. The target pages have their "names" defined, the language code is passed when the page is called (if not passed it defaults to English) and a simple sql statement extracts the appropriate prompts. I find it fast, I'm not loading huge arrays each time for all possible languages, and it avoids treating an XML page as a database. I have to use the database in the app anyway, so there's no performance penalty. What stunned me was the number of little miscellaneous prompts which need to be translated. Like "Return" and "Update". Here's the description and pseudo code from the prompts script .. /******************************************************************* mysql_auction_prompts.php Miles Thompson CQA Consulting Group, Enfield, NS (902) 883-1010 (902)452-5220 Required parameter: none Optional parameter: none Sets the label prompts in the prompt table from the prompt array. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! IMPORTANT - Run this after mysql_auction_tables.sql has been executed. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! An array provides the prompts for each page in the sequence "page" "language" "prompt_key and value pair". These are used by the script to create entries in the prompt table. This is the collapsed structure for the array of prompts: "page_name" => array(
                "lang_code1" => array(
                        "lbl_key1"              =>   "lbl_value1", 
                        "lbl_key2"              =>   "lbl_value2", 
                        "lbl_key3"              =>   "lbl_value3"), 
                "lang_code2" => array(
                        "lbl_key1"              =>   "lbl_value1", 
                        "lbl_key2"              =>   "lbl_value2", 
                        "lbl_key3"              =>   "lbl_value3"))); 
                        Another page could go here. If so:        ^^
                        insert a comma after second ")" and
                        move closing ");" down to follow added page
                        for the closing brace and semi-colon flagged with "^^".
This structure is used because it is neatly compartmented and it is relatively easy to add additional pages and languages. The code operates as three nested while loops:
        Page loop
                Check for page name equal to "page_name" and delete all records for that page name.
                Store the page name in a variable.
                Language Code loop
                        Store the language code in a variable.
                        For each language code process the next array
                                Label Keys and Label values loop
                                        Store each in a variable
                                        Insert the page name, language code, prompt key and prompt value
                                        into the prompt table.
This cleans out the existing prompts for each page and replaces them with new ones. ***************************************************************************/ I created the prompt keys by prepending the prompt value with "lbl_". This is the code used to fetch the prompts ... // // Fetch variables for form prompts // if( !isset( $cLangCode ) ) {$cLangCode = "GB";} //in future this will be set by the user $cPageName = "user_reg"; $arr_lbl = array(); $sql = "select cLblName,cLblText from prompt
                        where cLangCode = '$cLangCode' and cPageName = '$cPageName'";
$result = mysql_query( $sql, $db )
        or die( "Unable to fetch prompts for page $cPageName." );
while( $row = mysql_fetch_array( $result ) ) {
        $arr_lbl[$row[cLblName]] = $row[cLblText];
} // Text on button text easier to work with if they are not arrays
$Add            =       trim( $arr_lbl[ lbl_btnAdd  ] );
$Cancel =       $arr_lbl[ lbl_btnCancel ];
$Clear          =       $arr_lbl[ lbl_btnClear ];
$Update =       $arr_lbl[ lbl_btnUpdate ];
// // uncomment for debugging or if a list of keys and values is needed
//      while( list( $key, $val ) = each( $arr_lbl ) ) {
//              echo "Prompt: $key --- $val <br>"; }
and they're used like this ... echo "<center> <em>" . $arr_lbl[ lbl_cMsgUpDateOK ] . "!</em><p>"; echo "<a href = \"user_bid.php?cLangCode=$cLangCode\">" . $arr_lbl[ lbl_cMsgReturn ] ."</a> </center>"; It is a bit wordy, but I find if I get to cryptic I come back to a page in 6 months time and have to spend an hour or so figuring it out. The other problem with this, is that prompts in some languages are very long, so fit can be difficult. The good part of that is that you have to let the browser do a lot of formatting, so you tend to write page which are more neutral. Hop I haven't bored you - Miles Thompson At 10:12 AM 12/19/2000 -0800, Leon Atkinson wrote:
That's the way I thought about: building arrays which contain all the language dependent strings and so on. The idea is to give a extracted language file to a translator (someone who don't have knowledge about programming, php ...) and get back a translated file. Unfortunately the file have to be mixed with php-code, defining variables/arrays etc. . So I don't have a really extracted php-independent file. That's the reason why I thought about xml. With xml - that's my idea - I can extract all strings from my application, put it in a xml representation. Then I can use the ability from xml to present the strings in a different way for a better translation. I you have any hints about this idea, please let me know. I'll jump in and say that we tackled this problem in FreeTrade by making language modules, one for each language. Inside are a bunch of constants. That was long before Gettext was integrated. For something that's likely to have a fixed set of messages, you're probably best off going with Gettext since it's a solution that's been around for a long time. There are some nice things about it. The downside is that you have to deal with yet another data format. But I think the interface for novices is probably better than a giant PHP script full of define() statements. Leon --- Leon Atkinson <http://www.leonatkinson.com/> -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net For additional commands, e-mail: php-general-help@lists.php.net To contact the list administrators, e-mail: php-list-admin@lists.php.net


« previous php.general (#31125) next »