Create a webpage with Multilanguage in PHP

后端 未结 10 2128
醉话见心
醉话见心 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 09:57

    You could use sessions to store what language the user has set.

    $_SESSION["lang"] = "en";
    $_SESSION["lang"] = $_GET["lang"]; 
    // or this way, then you can do example.com/?lang=en
    

    Then create a file with an array of the translation.

    $lang["en"]["welcome"] = "Welcome to the site";
    

    And then another file could have the German translation

    $lang["de"]["welcome"] = "Willkommen auf der Webseite";
    

    And then you would just call the $lang array with the session

    echo $lang[$_SESSION["lang"]]["welcome"];
    

    This is a simplified version of this, you could also use objects to store the translation. But what i do not recommend is to use a database to store the translation, since this involves either a very large SELECT or very very many.

提交回复
热议问题