Create a webpage with Multilanguage in PHP

后端 未结 10 2126
醉话见心
醉话见心 2020-12-11 09:37

I would like to develop a multilanguage page in PHP, for exemple english/german/japanese.. so when i click on german the page language will change to german, then i click en

10条回答
  •  佛祖请我去吃肉
    2020-12-11 10:02

    I personally translate my complete views. Thus you would have a file structure like:

    application/
     someScript.php
    views/
     de/
      someView.php
     en/
      someView.php
    models/
     someModel.php
    

    And then as suggested by Olafur store the language in a session variable and do something like this.

     session_start(); //dont forget to call this somewhere
     $_SESSION['lang'] = 'en';
     //and then in somewehere else:
     include 'views/'.$_SESSION['lang'].'/someView.php'; //or whatever method is used to include views.
    

    EDIT
    You should seperate you output from your script. Thus you would have a script which does some scripting stuff, like getting the currect UNIX timestamp, this would be someScript.php in the example above:

      //someScript.php
      $timestamp = time();
    

    Now we could add all the output directly below this, but that would maintainability very hard, so we do the output in a different file, which here would be the someView.php files. So the someView.php in the 'en' folder would be:

    
    
    Currect UNIX timestamp!
    
    
    

    The current UNIX timestamp is: seconds

    And in the 'de' folder it would be practicly the same file but with all the text in german.

    Now all we need to do is include these output files in our someScript.php thus we add the following line to the bottom of that script:

     include '../views/'.$_SESSION['lang'].'/someView.php';
    

    You should read more on MVC here: http://www.onlamp.com/pub/a/php/2005/09/15/mvc_intro.html

    Note: That this example isn't strict MVC but this should give you the idea.

提交回复
热议问题