PHP changing language version

后端 未结 4 332
走了就别回头了
走了就别回头了 2021-01-16 13:51

I have a website which i want to create other language version. I don\'t want to create folder for each language. I was wondering it it\'s possible to add a combobox on each

相关标签:
4条回答
  • 2021-01-16 14:17

    if you wanted to use cookies... in the lang files you would include an array of words or content to use.

     <?php
    
        if($_GET['language']){
            $lang = (string)$_GET['language'];
            setcookie("lang", $lang, time()+3600);
            header('Location: '.$_SERVER['PHP_SELF']);
            die();
        }elseif(!isset($_COOKIE['lang'])){
            $lang='en';
        }else{$lang=$_COOKIE['lang'];}
    
        switch($lang){
            case "en":
                include('./lang/en.php');
                break;
            case "fr":
                include('./lang/fr.php');
                break;
            case "pol":
                include('./lang/pol.php');
                break;
            default:
                include('./lang/en.php');
                break;
        }
        ?>
    
    0 讨论(0)
  • 2021-01-16 14:24

    you mean something along the lines of

    if ($_GET['language']) {
      include $_GET['language'] . ".php";
    }
    

    and then save the languages in a php-file with there name, or a function depending on what you want to do with it

    0 讨论(0)
  • 2021-01-16 14:28

    hey for language version. have languages in combobox. maintain your current language in session. When u change language call an ajax call Update the changed language into session and reload the page.

    display page view with respect to session stored language.

    thats it........

    0 讨论(0)
  • 2021-01-16 14:37

    If you have a combobox, when the user submits it, store the language in the session (session_start(); has to be called) with $_SESSION['lang'] = $_POST['lang'];. I'd advise you to whitelist languages as such:

    session_start();
    
    // define language whitelist
    $allowedLangs = array('en', 'de');
    
    // only store the new user language if it's an allowed one
    if (isset($_POST['lang']) && in_array($_POST['lang'], $allowedLangs)) {
        $_SESSION['lang'] = $_POST['lang'];
    }
    
    // define the user language based on session data or use 'en' as default if not available
    $userLang = isset($_SESSION['lang']) ? $_SESSION['lang'] : 'en';
    
    // parse some language file according to the language
    $translations = // TODO load some file with $userLang here
    

    Of course you should adjust this to your own project and environment. For translation files, you can use a plain PHP file that returns an array like such:

    <?php 
    // en.php
    return array(
        'some.key' => 'Translation',
    );
    

    Then if you include that file, the return value of the include will be the array, so in the above code you could do:

    $translations = include 'translations/'.$userLang.'.php';
    

    You then have to output all your text through this $translations variable, like echo $translations['some.key'].

    0 讨论(0)
提交回复
热议问题