Jengas
Jengas

Reputation: 204

PHP: Detect user language and able to change language

I have a website with ability to choose language. And I wanted to make that when user enters first time to the website, php gets his system language and writes to cookie (So user by default every time when he enters time will have same language). But when user want to change website language, he will press a button with chosen language (For example Russian), then website language will be set for russian, and when he will enter website again, he will have russian language.

So far I have this code, but it's really confusing and it doesnt work properly.

HTML:

<a href="index.php?language=en">
<a href="index.php?language=ru">

PHP:

<?php
ini_set('display_errors',1);
error_reporting(E_ALL);

$language = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);

if (empty($_COOKIE['language'])){
setcookie('language', $language);
}

if ( !empty($_GET['language']) ) {
    $_COOKIE['language'] = $_GET['language'] === 'en' ? 'en' : 'ru';
} else {
  switch ($language){
  case "ru":
      $language = 'ru';
      break;
  case "en":
      $language = 'en';
      break;
  default:
      $language = 'en';
      break;
}
}

if ( $_COOKIE['language'] == "en") {
   $language = 'en';
} else {
   $language = 'ru';
}

$xml = simplexml_load_file("language.xml") or die("Equestria forgot languages");

$s_nav_main = $xml->s_nav_main->$language;
$s_nav_more = $xml->s_nav_more->$language;
$s_nav_bot = $xml->s_nav_bot->$language;
$s_nav_partners = $xml->s_nav_partners->$language;
$s_nav_developer = $xml->s_nav_developer->$language;
$s_aboutus = $xml->s_aboutus->$language;
$s_title = $xml->s_title->$language;
$s_head_title = $xml->s_head_title->$language;
$s_head_info = $xml->s_head_info->$language;
$s_statistics_people = $xml->s_statistics_people->$language;
$s_statistics_online = $xml->s_statistics_online->$language;
$s_statistics_messages = $xml->s_statistics_messages->$language;
$s_why_we_best = $xml->s_why_we_best->$language;
$s_why_we_best_content_title = $xml->s_why_we_best_content_title->$language;
$s_why_we_best_content_info = $xml->s_why_we_best_content_info->$language;
$s_why_we_best_adm_title = $xml->s_why_we_best_adm_title->$language;
$s_why_we_best_adm_info = $xml->s_why_we_best_adm_info->$language;
$s_why_we_best_comfort_title = $xml->s_why_we_best_comfort_title->$language;
$s_why_we_best_comfort_info = $xml->s_why_we_best_comfort_info->$language;
$s_why_we_best_wtf_title = $xml->s_why_we_best_wtf_title->$language;
$s_why_we_best_wtf_info = $xml->s_why_we_best_wtf_info->$language;
$s_trusted_title = $xml->s_trusted_title->$language;
$s_trusted_info = $xml->s_trusted_info->$language;
$s_people_celestia = $xml->s_people_celestia->$language;
$s_people_celestia_comment = $xml->s_people_celestia_comment->$language;
$s_people_luna = $xml->s_people_luna->$language;
$s_people_luna_comment = $xml->s_people_luna_comment->$language;
$s_people_twilight = $xml->s_people_twilight->$language;
$s_people_twilight_comment = $xml->s_people_twilight_comment->$language;
$s_botinfo_info = $xml->s_botinfo_info->$language;
$s_botinfo_more = $xml->s_botinfo_more->$language;
?>

Upvotes: 2

Views: 3153

Answers (3)

symcbean
symcbean

Reputation: 48357

The first place you should look for the users preferred language is the Accept-Language header. Geo-IP lookups are a dangerous and expensive waste of time (at least for determining language). Beyond that, you can set a cookie to override the choices presented by the browser, but there are legal implications around this for websites in Europe.

 $avail_lang=array(
            'en'=>1,
            'fr'=>1,
            'de'=>1,
            'ru'=>1
            );
 define("DEFAULT_LANG", 'en');
 ...
 if ($_COOKIE['language'] && isset($avail_lang[$_COOKIE['language']]) {
    $use_lang=$_COOKIE['language'];
 }
 // override with GET if provided
 if ($_GET['language'] && isset($avail_lang[$_GET['language']]) {
    $use_lang=$_GET['language'];
 }
 // no language? check browser
 if (!$use_lang) {
    $request_lang=explode(",", $_SERVER['HTTP_ACCEPT_LANGUAGE']);
    foreach($request_lang as $i) {
            list($lang, $pref)=explode("=", trim($i));
            $pref=$pref ? 0.0+$pref : 1.0;
            list($lang, $country)=explode("-", $lang);
            $pref_lang[$lang]=$pref;
    }
    rsort($pref_lang);
    $use_lang=array_shift(array_intersect_key($pref_lang, $avail_lang));
    if (!$use_lang) $use_lang=DEFAULT_LANGUAGE;
}
if (user_accepts_cookies() && $use_lang!=$_COOKIE['language']) {
    set_lang_cookie($use_lang);
}

Upvotes: 2

Jengas
Jengas

Reputation: 204

Found the way how to do this:

$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);

if ( !empty($_GET['language']) ) {
    $_COOKIE['language'] = $_GET['language'] === 'en' ? 'en' : 'ru';
} elseif (empty($_COOKIE['language'])) {
    $_COOKIE['language'] = $lang;
}
setcookie('language', $_COOKIE['language']);

if ( $_COOKIE['language'] == "en") {
   $language = 'en';
} else {
   $language = 'ru';
}

Upvotes: 0

Paritosh Pandey
Paritosh Pandey

Reputation: 115

a simple logic can be adopted here -

when a user lands at your website you should track his/her IP address, we can easily get their country using that IP. Then you can easily serve language to them.

Upvotes: 1

Related Questions