Change language conditions - php

I try to make a 'change languge system' for my script. I already created my class and everything works fine. My problem is next: if I have URL index.php?lang=fr , my website language is changed as long I have ?lang=fr. If I remove ?lang=fr from URL, language is changed too back to english. What is wrong in my conditions ?
P.S: $available_langs is an array with en, fr and ro.
if(isset($_GET['lang']) && $_GET['lang'] != ''){
if(in_array($_GET['lang'], $available_langs)){
$_SESSION['lang'] = $_GET['lang'];
$lang = new Language("languages/");
$lang->setLang($_SESSION['lang']);
} else {
$_SESSION['lang'] = "en";
$lang = new Language("languages/");
$lang->setLang($_SESSION['lang']);
}
} else {
if(isset($_SESSION['lang'])){
$lang = new Language("languages/");
$lang->setLang($_SESSION['lang']);
} else {
$_SESSION['lang'] = "en";
$lang = new Language("languages/");
$lang->setLang($_SESSION['lang']);
}
}

Related

How to add a third language in PHP?

How to add a third language with this specific code format? I have successfully managed 2 languages. I would like to add more than 3 languages. Thank you in advance.
<?php
session_start();
if (!isset($_SESSION['lang']))
$_SESSION['lang'] = "en";
else if (!isset($_GET['lang']) && $_SESSION['lang'] != $_GET['lang'] && !empty($_GET['lang'])) {
if ($_GET['lang'] == "en")
$_SESSION['lang'] = "en";
else if ($_GET['lang'] == "sp")
$_SESSION['lang'] = "sp";}

PHP - Changing session value to change the language

I am trying to change the session to a value of "en" or "no" by clicking on a link so that the correct language is being displayed on a website.
I have 2 variables, $langNO and $langEN. These are variables containing keys and values of each language.
I am using this:
if (!isset($_SESSION['lang'])) {$_SESSION['lang'] = "en";}
if ($_SESSION['lang'] === "no") {$lang = $langNO;}
if ($_SESSION['lang'] === "en") {$lang = $langEN;}
$lang is related to the strings in the text.
Example:
<h1><?php echo $lang['heading']; ?></h1>
To change the session value, I use this in changelang.php:
session_start();
$_SESSION['lang'] = $_GET['lang'];
header("Location:index.php");
When I click on NO to change the language, it just stays on the default language.
I would love to get some feedback for how this can be solved.
Thank you!
I just changed the index code a little bit
index.php
<?php
session_start();
if (!isset($_SESSION['lang'])) {
$lang = "en";
} else {
$lang = $_SESSION['lang'];
}
switch ($_SESSION['lang']) {
case "en":
$lang = $langEN;
break;
case "no":
$lang = $langNO;
break;
}
?>
NO
changelang.php
<?php
session_start();
$_SESSION['lang'] = $_GET['lang'];
header("Location:index.php");
?>
changelang.php
Set the header() in a function
function redirect(url) {
header('Location: '. url);
}
session_start()
$_SESSION['lang'] = $_GET['lang'];
redirect('index.php');

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;
?>
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);
}
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.
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';
}

Checking cookie and session value

I am working on a multilingual website, and I have a problem now. When the $_GET['setlang'] is empty, language is set to en_US, even if I had ?setlang=pl opened before (which is working fine - language is changing)
empty($_GET['setlang']) ? $_GET['setlang'] = '' : $_GET['setlang'];
switch ($_GET['setlang']) {
case 'en':
$lang = 'en_US';
putenv('LC_ALL='.$lang);
setlocale(LC_ALL, $lang.'.UTF8');
setcookie('lang', $lang, time()+60*60*24*365);
$_SESSION['lang'] = $lang;
$_config['lang'] = $lang;
break;
case 'pl':
$lang = 'pl_PL';
putenv('LC_ALL='.$lang);
setlocale(LC_ALL, $lang.'.UTF8');
setcookie('lang', $lang, time()+60*60*24*365);
$_SESSION['lang'] = $lang;
$_config['lang'] = $lang;
break;
default:
if((isset($_SESSION['lang']) && $_SESSION['lang'] ='en_US') || (isset($_COOKIE['lang']) && $_COOKIE['lang'] = 'en_US')){
$lang = 'en_US';
putenv('LC_ALL='.$lang);
setlocale(LC_ALL, $lang.'.UTF8');
}
else{
$lang = 'pl_PL';
putenv('LC_ALL='.$lang);
setlocale(LC_ALL, $lang.'.UTF8');
}
$_config['lang'] = $lang;
break;
}
$my_name = 'default';
bindtextdomain($my_name,'./locale');
textdomain($my_name);
I can't get this script to change the language to pl_PL after I run ?setlang=pl action. When this get is empty, cookie value is changing to en_US ;/
How should I check it and avoid this problem?
Also I am working with MVC. Does this code seem to be right or should I put some code into model instead of controller?
if((isset($_SESSION['lang']) && $_SESSION['lang'] =='en_US')
|| (isset($_COOKIE['lang']) && $_COOKIE['lang'] == 'en_US'))
make comparisons with == not = this way you assign 'en_US' whatever the $_SESSION["lang"] or $_COOKIE["lang"] is.
Have you started the session? You cannot set or query session variables if you have not.
Use session_start();
$lang = "";
// Check if lang cookie is set
if(isset($_COOKIE['lang']))
{
$lang = $_COOKIE['lang'];
}
// Cookie is not set
else
{
// Get language GET variable
$set_lang = (empty($_GET['setlang'])) ? '' : $_GET['setlang'];
switch($set_lang)
{
case 'en':
$lang = "en_US";
break;
case 'pl':
$lang = "pl_PL";
break;
default:
$lang = "en_US";
break;
}
// Set language cookie
setcookie('lang', $lang, time()+60*60*24*365);
}
// Set locale
putenv('LC_ALL=' . $lang);
setlocale(LC_ALL, $lang . ".UTF8");

$_get and session. Can't get it to work

Can anyone tell me why this doesn't work?
<?php
$lang = $_get["lang"];
if (($lang == "fr"))
{
session_destroy();
session_start();
$_SESSION['lang'] == "fr";
}
if (($lang == "en"))
{
session_destroy();
session_start();
$_SESSION['lang'] == "en";
}
if (isset($_SESSION['lang']))
{
$lang = $_SESSION['lang'];
}
else
{
$lang = "fr";
}
?>
I just can't seem to get it to work and I tried a lot of different things. Just need a direction to the mistake.
It's running on PHP5 on an Apache server if that's any help.
Even without the session I can't even get the $_get to work. With normally is never the case.
$_SESSION['lang'] == "fr";
two equal signs mean comparision operator.
assignment is = (one equal)
One obvious thing is
$_get["lang"];
Variables are case sensitive in PHP. It must be
$_GET["lang"];
if that doesn't solve your problem, you need to describe in detail what exactly doesn't work.
I have re-factored your code to make it easier to work with in the future. As Pekka says and I asked perhaps you need/want $_GET.
<?php
session_start();
$language = $_GET['lang'];
$allowable_languages = array(
'en',
'fr',
);
if(in_array($language, $allowable_languages)) {
$_SESSION['lang'] = $language;
} else {
$_SESSION['lang'] = 'fr';
}
?>
Updated: In answer to your comment:
<?php
session_start();
if(isset($_GET['lang'])) {
$language = $_GET['lang'];
$allowable_languages = array(
'en',
'fr',
);
if(in_array($language, $allowable_languages)) {
$_SESSION['lang'] = $language;
}
}
if(!isset($_SESSION['lang'])) {
$_SESSION['lang'] = 'fr';
}
?>
U just made a single mistake i.e, u are using wrong syntax it should be $_GET['lang'];
Your code doesn't work because session_start() can't be used directly after session_destroy(). Btw, you don't need to destroy session. Just redefine your variable:
$lang = 'fr';
if(isset($_GET['lang']) && in_array($_GET['lang'], array('fr', 'en'))) $lang = $_GET['lang'];
$_SESSION['lang'] = $lang;
For those who thinks the problem is lower case variable - PHP variables are case insensitive.
You are both right. There was a lot of different error. Both the "=" and "==" problem plus the $_GET case sensitive problem.
Here the code i god to work both the $_GET plus the session.
Thanks a lot. I've spend all night trying to find those error plus think i got errorblind.
<?php
$lang = $_GET["lang"];
if (($lang == "fr"))
{
session_destroy();
session_start();
$_SESSION['lang'] = "fr";
}
if (($lang == "en"))
{
session_destroy();
session_start();
$_SESSION['lang'] = "en";
}
if (isset($_SESSION['lang']))
{
$lang = $_SESSION['lang'];
}
else {
$lang = "fr";
}
?>

Categories