<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

$language = "en_US";
$uid = "0";

if (isset($_SESSION['uid'])) {
    $uid = $_SESSION['uid'];
}
$offering1 = DBConnection();
$query = "SELECT l.lang_cd_dt FROM pref_language as l,user_account as ua WHERE l.lang_id=ua.pref_lang_id AND ua.user_id=" . $uid . "";
$stmt = $offering1->prepare($query);
$stmt->execute();
if ($row_lan = $stmt->fetch(PDO::FETCH_ASSOC)) {
    extract($row_lan);
    $language = $row_lan['lang_cd_dt'];
    if (empty($language) || $language === "1") {
        $lang_browser = isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2) : "en";
//        $lang_browser = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
        $query1 = "SELECT lang_cd_dt,lang_cd_mobile FROM pref_language WHERE lang_cd_mobile='" . $lang_browser . "'";
        $stmt1 = $offering1->prepare($query1);
        $stmt1->execute();
        if ($row_lan_dt = $stmt1->fetch(PDO::FETCH_ASSOC)) {
            $language = $row_lan_dt['lang_cd_dt'];
        } else {
            $language = "en_US";
        }
    }
}else{
  $lang_browser = isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2) : "en";
//  $lang_browser = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
        $query1 = "SELECT lang_cd_dt,lang_cd_mobile FROM pref_language WHERE lang_cd_mobile='" . $lang_browser . "'";
        $stmt1 = $offering1->prepare($query1);
        $stmt1->execute();
        if ($row_lan_dt = $stmt1->fetch(PDO::FETCH_ASSOC)) {
            $language = $row_lan_dt['lang_cd_dt'];
        } else {
            $language = "en_US";
        }

}
putenv("LANG=" . $language);
setlocale(LC_ALL, $language);

$domain = "messages";
bindtextdomain($domain, __DIR__ . "/../Locale");
bind_textdomain_codeset($domain, 'UTF-8');

textdomain($domain);
?>