Interactive

Web Development Services

Manage user language and language files in php

Manage user language and language files in php

You can also get language settings from database as well. If the user has set it in the database, then that language should have priority over cookie settings since user could have changed it from a different computer. Best order of priority:database, $_GET(changed by user) , $_SESSION, $_COOKIE, browser.

Copy PHP

//php
$lang_file = "profile";

//check if language settings were saved
if (!isset($_COOKIE["language"])) {
    $lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
} else {
    $lang = $_COOKIE["language"];
}

//check if user changed the language
if (isset($_GET["language"]) && (($_GET["language"] == "French")OR ( $_GET["language"] == "french")OR ( $_GET["language"] == "fr"))) {
    $lang = "fr";
} else if (isset($_GET["language"]) && (($_GET["language"] == "English")OR ( $_GET["language"] == "english")OR ( $_GET["language"] == "en"))) {
    $lang = "en";
}

//update language
setcookie("language", $lang);

switch ($lang) {
    case "fr":
        $lang_id = "2";
        require("/folder/languages/french/main.php"); 
        require("/folder/languages/french/".$lang_file.".php"); 
        break;
    case "en":
        $lang_id = "1";
        require("/folder/languages/english/main.php"); 
        require("/folder/languages/english/".$lang_file.".php"); 
        break;
    default:
        $lang_id = "1";
        require("/folder/languages/english/main.php"); 
        require("/folder/languages/english/".$lang_file.".php"); 
        break;
}

comments

no comments...

Leave A Comment

Don’t wait! Your customers are surfing the Web right now. Get your website up today.

get in touch