using .xml file for language change

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pradeepjain
    Contributor
    • Jul 2007
    • 563

    using .xml file for language change

    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
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    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.

    Comment

    • pradeepjain
      Contributor
      • Jul 2007
      • 563

      #3
      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 
      }
      is the form

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        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);
        	}
        config.language .ini
        Code:
        ; register languages here like
        ; language_abbr = translation_file.ini
        
        en = English.ini
        de = German.ini
        ; etc.

        Comment

        • pradeepjain
          Contributor
          • Jul 2007
          • 563

          #5
          okie i understood the logic. how do i replace the labels in the form ? i could not get that!

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            Code:
            echo "<label>", $label['label_key'], "</label>";

            Comment

            • pradeepjain
              Contributor
              • Jul 2007
              • 563

              #7
              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>
              say this is the form .How is the ini parser better than xml parser ? just for the knowledge sake.

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                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

                • pradeepjain
                  Contributor
                  • Jul 2007
                  • 563

                  #9
                  can u just show me an example for xml .i did not get proper example to see it so asked !

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    post an XML to work with.

                    Comment

                    • pradeepjain
                      Contributor
                      • Jul 2007
                      • 563

                      #11
                      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

                      • Dormilich
                        Recognized Expert Expert
                        • Aug 2008
                        • 8694

                        #12
                        you are aware that XForms is something completely different?

                        Comment

                        • pradeepjain
                          Contributor
                          • Jul 2007
                          • 563

                          #13
                          i am not much aware Xml thats the problem!so finding it difficult

                          Comment

                          • Dormilich
                            Recognized Expert Expert
                            • Aug 2008
                            • 8694

                            #14
                            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

                            Working...