I made a php script that allows me to switch the language of my website and for that, I used a cookie called lang to store the user's language prefer but I don't Know why this cookie can't be set as I can't find it in the chrome developer tap .. Although I made this script also with session variable in case cookie isn't set, it doesn't work either, as it shifts the language whenever I change the page
this is my PHP script
<?php
session_start();
if(isset($_GET['lang'])) {
$lan = $_GET['lang'];
$_SESSION['lang'] = $lan;
setcookie("lang", $lan, time() + (3600 * 24 * 30), '/');
} else if (isset($_SESSION['lang'])) {
$lan = $_SESSION['lang'];
} else if (isset($_COOKIE['lang'])) {
$lan = $_COOKIE['lang'];
} else {
$lan = 'en';
}
include "languages/" . $lan . ".php";
?>
I have two files in that language folder en.php and ar.php and each one contains an array with translation like below
<?php
$lang = array(
"Give" => "Give",
"Blood" => "Blood",
"Register as a donator" => "Register as a donator",
"Search for a donator" => "Search for a donator"
I include my script in all my site's pages
here is my chrome developer tap screen shot
any thoughts?
Thanks for helping in advance :)
Related
My website by default is in English
But I can change the language, when a visitor visits our website with a url of a news item in Spanish:
example.com/es/news/718/url-title-article/
The url given as an example is a news in Spanish since the first folder or directory is named "es" example.com/es/news/718/url-title-article/ -> es
And, that's where the trick comes, since I have a function that allows me to obtain the name of the first folder or directory and if it matches with the given parameters, it should set a cookie with the language of that url, in this case Spanish -> "es".
if($FOLDER_LANG === "es" || $SUBDOMAIN_LANG === "es") {
setcookie ('language', 'es', time()+60*60*24*365, '/', 'example.com');
}
if(isset($_COOKIE['language'])){
if($_COOKIE['language']==='en'){
$WEBSITE_TITLE = " | WebSIte EN";
$LANG = 'en';
$language = "en";
}elseif($_COOKIE['language']==='es'){
$WEBSITE_TITLE = " | WebSite ES";
$LANG = 'es';
$language = "es";
}
} else {
$WEBSITE_TITLE = " | WebSite DEFAULT EN";
$LANG = 'en';
$language = "en";
}
But the problem, is that I have to reload the page to see the changes of the new language added to the site.
So how can you solve it without having to reload the site.
You need to write the logic which determines the language so it doesn't depend on the cookie.
So, before, where you might have had something like:
$language_data = get_i18n_data( $_COOKIES['language'] );
You would instead have something like:
function get_language() {
if($FOLDER_LANG === "es" || $SUBDOMAIN_LANG === "es") {
setcookie ('language', 'es', time()+60*60*24*365, '/', 'example.com');
return 'es'
}
return $_COOKIES['language'];
}
$language_data = get_i18n_data( get_language() );
The reason for refresh being needed is that $LANG and $language is set by an existing cookie and not the new one.
Analysing your code:
if($FOLDER_LANG === "es" || $SUBDOMAIN_LANG === "es") {
setcookie ('language', 'es', time()+60*60*24*365, '/', 'example.com');
}
Here, it's setting the cookie only in the header. It is not accessible until the page is reloaded.
One option is to add the following in the if condition:
$LANG = 'es';
$language = "es";
The rest of your code then reads existing cookies so this is what will load on the same page even if the cookie is set just before it:
A better way in my opinion is to keep it simple and expandable by using variable $language to drive the data:
$language = 'en'; //this is default
if(isset($_COOKIE['language'])){
$language = $_COOKIE['language'];
}
//now check if current $language is different to folder
if($FOLDER_LANG != $language || $SUBDOMAIN_LANG != $language) {
//this is condition depends on your variables. You might need && if both conditions should be met or modify as required
//set the language for this page (so no need for refresh and also set the cookie
$language = $FOLDER_LANG ? $FOLDER_LANG : $SUBDOMAIN_LANG; //sets to folder or sudomain language code if $FOLDER_LANG is empty. You can switch the two folders to priotorise the latter.
//now set the cookie
setcookie ('language', $language, time()+60*60*24*365, '/', 'example.com');
}
//now set the the title and the extra variable $LANG (you probably should choose one variable to use)
$WEBSITE_TITLE = " | WebSite . strtoupper($language)";
$LANG = $language;
Now you can support more languages and also won't need to refresh
Help please organize bi-lingual website.
So first there are two files eng.php, es.php and they will be stored in translation site.
example:
$lang['hi'] = 'Hi';
How can I organize further language choice on the site and record information about the language in cookies?
You can have two files like this.
Source of en.php:
$lang = array(
'hi' => 'Hi'
);
Source of es.php:
$lang = array(
'hi' => 'Hello'
);
And for the main content file, the source should be this way:
<?php
session_start(); // Make sure you initialize cookies / session
$allowedLangs = array('en', 'es'); // Array with allowed values
if(isset($_SESSION['lang'])) { // If already user had stored language in session
include $_SESSION['lang'] . ".php";
} elseif(isset($_GET['lang']) && in_array($_GET['lang'], allowedLangs)) { // If user had requested like index.php?lang=en
include $_GET['lang'] . ".php";
$_SESSION['lang'] = $_GET['lang']; // Update the session with the language
} else { // If user is visiting for the first time, then...
include "en.php";
}
echo $lang['hi'];
?>
<?php
if(!isset($_COOKIE['lang'])){
?>
Choose Language...
ESENG
<?php
} else {
if($_COOKIE['lang']=='es'){
header("location:es.php");
}
elseif($_COOKIE['lang']=='eng'){
header("location:eng.php");
}
}
?>
es.php // eng.php
<!--Your Content-->
<?php
setcookie("lang","es/eng",time()+SECONDS_YOU_WANT)
?>
I made a multi-language website .
It contains only two languages : English and French.
The default language is English and the second language is French
English works perfectly in the website but French doesn't because while changing the language to French using the URL (?lang=fr) I get random letters (in this case I got 'F') and if I switch to (?lang=de) I got 'D') it means that I get to first letter of the varibale $_GET["lang"]
This is the code of my lang.php file
<?php
session_start();
if(isset($_GET['lang']))
{
$lang = $_GET['lang'];
$_SESSION['lang'] = $lang;
setcookie('lang', $lang, time() + (3600 * 24 * 30));
}
else if(isset($_SESSION['lang'])) {$lang = $_SESSION['lang'];}
else if(isset($_COOKIE['lang'])) {$lang = $_COOKIE['lang'];}
else {$lang = 'fr';}
if($lang=='fr'){$lang_dir = 'fr.php';}
else if ($lang=='en') {$lang_dir = 'en.php';}
else {$lang_dir = 'fr.php';}
include 'administration/includes/lang/'.$lang_dir;
?>
When people visit my site, I need them to click one of two states (NC or VA). Depending on which state is clicked, it will redirect them to the appropriate page on my site. After the cookie is set, I want them to visit the site and instead of asking the question again, it already knows to send them to the page (the state) they selected. I know very little about php...just enough to be dangerous and any direction you can give me would be appreciated.
You can set the cookie as:
setcookie('state', $state, time() + (60 * 60 *24));
Assuming $state is either 'nc' or 'va', this will work:
if(isset($_COOKIE['state']))
{
if($_COOKIE['state'] == 'va')
header('Location: va/index.php');
else if($_COOKIE['state'] == 'nc')
header('Location: vnc/index.php');
}
else
{
// Make them choose again here.
}
You should take a look at this php function setcookie.
setcookie( "state", "VA", time()+3600 );
Then redirect using the location header.
header( "Location: /" );
On the index page
<?php
if(isset($_COOKIE['state']))
{
switch($_COOKIE['state'])
{
case "NC":
header('location: www.url.com/site1/');
break;
case "VA":
header('location: www.url.com/site2/');
break;
}
}
else
{
//Display site options
}
?>
And on the individual sites (EG www.url.com/site1/):
<?php
if(!isset($_COOKIE['state']))
{
setcookie('state', "NC" ,time() + (86400 * 7)); //valid for 7 days
}
?>
I'm testing this code in Localhost and on Server "Aruba".
In local environment it works perfectly while on Server I dont have the expected session value
When I echo out the $_SESSION['lang'] it outputs :
-the correct country code (Ex.'en') in localhost
-On the Aruba server $_SESSION['lang'] outputs the array named $lang (that you can find on lang.en.php)instead of the needed country code!!
Where am I wrong?
thanks
Luca
my home.php
require_once('/web/htdocs/www.mywebsite.com/home/includes/langSwitcher.inc');
echo $_SESSION['lang'];
[..]
my langSwitcher.inc
session_start();
header('Cache-control: private'); // IE 6 FIX
if(isset($_GET['lang']))
{
$lang = $_GET['lang'];
// register the session and set the cookie
$_SESSION['lang'] = $lang;
setcookie('lang', $lang, time() + (3600 * 24 * 30));
}
else if(isset($_SESSION['lang']))
{
$lang = $_SESSION['lang'];
}
else if(isset($_COOKIE['lang']))
{
$lang = $_COOKIE['lang'];
$_SESSION['lang']=$lang;
}
else
{
$lang = 'en';
$_SESSION['lang']=$lang;
}
switch ($lang)
{
case 'en':
$lang_file = 'lang.en.php';
break;
case 'it':
$lang_file = 'lang.it.php';
break;
}
include_once $lang_file;
my lang.en.php
/*
-----------------
Language: Italian
-----------------
*/
$langcode='en';
$lang = array();
$lang['PAGE_TITLE'] = 'pagetitle';
$lang['HEADER_TITLE'] = 'title header ';
$lang['SITE_NAME'] = 'name site';
$lang['HEADING'] = 'title';
It sounds like register_globals may be enabled (although that feature is deprecated). You can find out by running a phpinfo() and looking for the register_globals entry.
Assuming it is enabled, the only solution is to fix it in php.ini (you cannot override register_globals with an ini_set() call).
Well you are using $lang to keep the langcode, but also to store the array information. Perhaps in the langSwitcher.inc you should be using $langcode to store the session?
Because you are also settings the $lang var in the session their. On your server it seems to be using a reference to the $lang file, and therefor outputting the latest content set to $lang (which is the array) and on local it is storing the actual content of $lang.
Anyway, it can be solved by not using the same variable name to store two different items.