Connect with Expertise | Find Experts, Get Answers, Share Insights

using .xml file for language change

pradeepjain's Avatar
C
 
Join Date: Jul 2007
Location: India
Posts: 562
#1: Feb 10 '10
I have a form .i need this form field labels to be displayed in 3-4 languages . i have the corresponding labels in different languages in a .xml file . i dono how to do the conversions. form is in html rite now. any help on how to do this!

Dormilich's Avatar
E
M
C
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 5,395
#2: Feb 10 '10

re: using .xml file for language change


you have to get the labels from the XML through a filter. but that depends on the structure of the file, the HTML etc. can’t say more without knowing the code.
pradeepjain's Avatar
C
 
Join Date: Jul 2007
Location: India
Posts: 562
#3: Feb 10 '10

re: using .xml file for language change


Expand|Select|Wrap|Line Numbers
  1. <?php
  2. function showForm($error=false){
  3. $errors='';
  4. if($error['surname']==TRUE){
  5. $surname_class = "valid-error";
  6. $errors.="<li>Error  in Surname</li>";
  7. }
  8. if($errors){
  9. ?>
  10. <div id="errors">
  11. <ul>
  12. <?php echo $errors; ?>
  13. The fields Marked Red have errors . Please correct them and submit again !
  14. </ul>
  15. </div>
  16. <?php } 
  17. ?>
  18. <form name="form1" id="form1" method="POST" action=<?php $_SERVER['php_self'] ?>>  
  19.  <div class="field50Pct">
  20.                     <div class="fieldItemLabel">
  21.                             <label for="Surname">Surname:<span class="star">*</span></label>
  22.                     </div>
  23.                     <div class="fieldItemValue">
  24.                 <input type="text" id="surname" name="surname" value="<?php echo $_POST['surname'] ?>" class="<?php echo $surname_class ?>"/>
  25.                     </div>
  26.             </div>
  27. <center>
  28. <input type="submit" name="submit" value="submit">
  29. <input type="reset" value="Reset" />
  30. </center>
  31. </form>
  32.     </div>
  33. <?php
  34. }
  35. if (!isset($_POST['submit'])) {
  36.  
  37. showForm();
  38. } else {
  39. $_POST    = snipExtras($_POST);
  40. $error=array();
  41. if(!validate_surname($_POST['surname'])) $error['surname']=true;
  42.  
  43. if($error){
  44. showForm($error);
  45. } else {
  46. //process the form 
  47. }
is the form
Dormilich's Avatar
E
M
C
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 5,395
#4: Feb 10 '10

re: using .xml file for language change


maybe you’re better off using an .ini file => parse_ini_file(). uses far less memory. then you should’ve got all labels in an array, where you can print from.

I’d do something like.
Expand|Select|Wrap|Line Numbers
  1. // found somewhere in my web project …
  2.     /**
  3.      * set the $lang property by looking for a "lang" attribute (URL).
  4.      * read the appropriate language file.
  5.      *
  6.      * @return (void)
  7.      */
  8.     private function getLanguage()
  9.     {
  10.         # read language parameter from $_GET
  11.         if (isset($_GET['lang']) and strlen($_GET['lang']) == 2)
  12.         {
  13.             $this->lang = $_GET['lang'];
  14.         }
  15.  
  16.         # read language configuration
  17.         $lang_conf = parse_ini_file(MYPHP_GB_DIR . 'lang/' . 'config.language.ini');
  18.  
  19.         $lang_file = MYPHP_GB_DIR . 'lang/';
  20.  
  21.         # select system language
  22.         if (array_key_exists($this->lang, $lang_conf))
  23.         {
  24.             $lang_file .= $lang_conf[$this->lang];
  25.         }
  26.         else
  27.         {
  28.             $lang_file .= 'German.ini';
  29.         }
  30.  
  31.         # read translation table
  32.         $this->translation = parse_ini_file($lang_file);
  33.     }
config.language.ini
Expand|Select|Wrap|Line Numbers
  1. ; register languages here like
  2. ; language_abbr = translation_file.ini
  3.  
  4. en = English.ini
  5. de = German.ini
  6. ; etc.
pradeepjain's Avatar
C
 
Join Date: Jul 2007
Location: India
Posts: 562
#5: Feb 11 '10

re: using .xml file for language change


okie i understood the logic. how do i replace the labels in the form ? i could not get that!
Dormilich's Avatar
E
M
C
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 5,395
#6: Feb 11 '10

re: using .xml file for language change


Expand|Select|Wrap|Line Numbers
  1. echo "<label>", $label['label_key'], "</label>";
pradeepjain's Avatar
C
 
Join Date: Jul 2007
Location: India
Posts: 562
#7: Feb 11 '10

re: using .xml file for language change


I got this working .Thanks a lot .
Can i know how to do the same with .xml file

Expand|Select|Wrap|Line Numbers
  1. <form name ='test' >
  2. <?php
  3. echo "<label>", $label['label_key'], "</label>";
  4. ?>
  5. <input type='text' name='namevalue'>
  6. <input type='submit' value='submit'>
  7. </form>
say this is the form .How is the ini parser better than xml parser ? just for the knowledge sake.
Dormilich's Avatar
E
M
C
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 5,395
#8: Feb 11 '10

re: using .xml file for language change


reading XML files usually takes quite an amount of memory. you could use an XMLReader (nodewise XML processing), but then you’d need loops to generate the translation array.

you read .ini files with one function and then you have an array, ready to use.

IMO XML files are less efficient, when it comes to such data structures like a simple serialized array.
pradeepjain's Avatar
C
 
Join Date: Jul 2007
Location: India
Posts: 562
#9: Feb 11 '10

re: using .xml file for language change


can u just show me an example for xml .i did not get proper example to see it so asked !
Dormilich's Avatar
E
M
C
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 5,395
#10: Feb 11 '10

re: using .xml file for language change


post an XML to work with.
pradeepjain's Avatar
C
 
Join Date: Jul 2007
Location: India
Posts: 562
#11: Feb 11 '10

re: using .xml file for language change


Cant we use the html form and just pick up the labels from the .xml file ?

for example i just took this from a tutorial .
Expand|Select|Wrap|Line Numbers
  1. <xforms>
  2. <model>
  3. <instance>
  4. <person>
  5. <fname/>
  6. <lname/>
  7. </person>
  8. </instance>
  9. <submission id="form1" method="get"
  10. action="submit.asp"/>
  11. </model>
  12.  
  13. <input ref="fname">
  14. <label>First Name</label></input><br />
  15.  
  16. <input ref="lname">
  17. <label>Last Name</label></input><br /><br />
  18.  
  19. <submit submission="form1">
  20. <label>Submit</label></submit>
  21.  
  22. </xforms>
Dormilich's Avatar
E
M
C
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 5,395
#12: Feb 11 '10

re: using .xml file for language change


you are aware that XForms is something completely different?
pradeepjain's Avatar
C
 
Join Date: Jul 2007
Location: India
Posts: 562
#13: Feb 11 '10

re: using .xml file for language change


i am not much aware Xml thats the problem!so finding it difficult
Dormilich's Avatar
E
M
C
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 5,395
#14: Feb 11 '10

re: using .xml file for language change


basicly, XForms is a standard of forms. You can think of it as a replacement for HTML forms (actually, XForms require an XML document to "work" in).

XML as such is more of a data storage facility, with a lot of flexibility.

when you want your form labels in different human languages, that’s simply a servers task, to fill in the appropriate text somewhere. where it gets the required data (DB, JSON, CSV, .ini, XML, etc.), does not matter.
Reply