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!
using .xml file for language change
Collapse
X
-
Tags: None
-
Code:<?php function showForm($error=false){ $errors=''; if($error['surname']==TRUE){ $surname_class = "valid-error"; $errors.="<li>Error in Surname</li>"; } if($errors){ ?> <div id="errors"> <ul> <?php echo $errors; ?> The fields Marked Red have errors . Please correct them and submit again ! </ul> </div> <?php } ?> <form name="form1" id="form1" method="POST" action=<?php $_SERVER['php_self'] ?>> <div class="field50Pct"> <div class="fieldItemLabel"> <label for="Surname">Surname:<span class="star">*</span></label> </div> <div class="fieldItemValue"> <input type="text" id="surname" name="surname" value="<?php echo $_POST['surname'] ?>" class="<?php echo $surname_class ?>"/> </div> </div> <center> <input type="submit" name="submit" value="submit"> <input type="reset" value="Reset" /> </center> </form> </div> <?php } if (!isset($_POST['submit'])) { showForm(); } else { $_POST = snipExtras($_POST); $error=array(); if(!validate_surname($_POST['surname'])) $error['surname']=true; if($error){ showForm($error); } else { //process the form }
Comment
-
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.
Code:// found somewhere in my web project … /** * set the $lang property by looking for a "lang" attribute (URL). * read the appropriate language file. * * @return (void) */ private function getLanguage() { # read language parameter from $_GET if (isset($_GET['lang']) and strlen($_GET['lang']) == 2) { $this->lang = $_GET['lang']; } # read language configuration $lang_conf = parse_ini_file(MYPHP_GB_DIR . 'lang/' . 'config.language.ini'); $lang_file = MYPHP_GB_DIR . 'lang/'; # select system language if (array_key_exists($this->lang, $lang_conf)) { $lang_file .= $lang_conf[$this->lang]; } else { $lang_file .= 'German.ini'; } # read translation table $this->translation = parse_ini_file($lang_file); }
Code:; register languages here like ; language_abbr = translation_file.ini en = English.ini de = German.ini ; etc.
Comment
-
Comment
-
I got this working .Thanks a lot .
Can i know how to do the same with .xml file
Code:<form name ='test' > <?php echo "<label>", $label['label_key'], "</label>"; ?> <input type='text' name='namevalue'> <input type='submit' value='submit'> </form>
Comment
-
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.Comment
-
-
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 .
Code:<xforms> <model> <instance> <person> <fname/> <lname/> </person> </instance> <submission id="form1" method="get" action="submit.asp"/> </model> <input ref="fname"> <label>First Name</label></input><br /> <input ref="lname"> <label>Last Name</label></input><br /><br /> <submit submission="form1"> <label>Submit</label></submit> </xforms>
Comment
-
-
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.Comment
Comment