My website has two languages: English and Russian. These languages' database names are EN and RU.
I use an old php+smarty script. This script's default language selection codes like this:
if(!isset($_SESSION)){
define('LANGUAGE_ID', 'EN');
} else if ($_SESSION['language'] == '')
{
define('LANGUAGE_ID', 'EN');
}
When I want to see a page in Russian, I visit any page with language phrase (?language=RU) like this:
http://www.example.com/index.php?language=RU
But these pages doesn't load in Russian firstly. When I clicked another link or refresh the page, then page loading in Russian. After saved the cookies, then I can see the pages in Russian when first visit. But If I delete the cookies in browser, then I couldn't see in Russian when first visit.
I tried lots of combinations but I couldn't find any solution. Do you have any idea?
Thank you very much...
Edit:
I found some codes in main.class.php:
function __construct($dbh,$smartyBuild) {
$this->dbh = $dbh;
$this->sitevar = #$smartyBuild->FetchSiteVar();
$this->smartybuild = #$smartyBuild;
if($_REQUEST['language'] !='')
{
$_SESSION['language'] = $_REQUEST['language'];
}
else
{
$langaugeAlready = mysql_fetch_array(mysql_query("select value from ".TABLE_PREFIX."sitevars where array_key = 'default_language_feed'"));
if($_SESSION['language'] == '')
{
$_SESSION['language'] = $langaugeAlready['value'];
}
}
if($_SESSION['language'] !='' )
{
define('LANGUAGE_ID', $_SESSION['language']);
}
else
{
define('LANGUAGE_ID', 'EN');
$_SESSION['language'] = 'EN';
}
}
Is the problem related with these codes?
Like I say, without all the code we are guessing a bit at what the problem is but, here goes...
It seems like you're only checking the $_SESSION variable for the language and not the $_GET variable (which gets the language from the URL). Therefore the value only changes after you refresh the page.
Try this. I am assuming your intention is to show English as a default and only Russian if it is defined in the url, but once defined to keep that language until it is put in the URL again.
//start a session. must be called first on all pages you want it to work on
session_start();
//first check if there's a new language coming from the URL
if(isset($_GET['language']))
{
// if we have a new language setting from the URL, check which one and save it in the session.
// we check it is EN or RO before saving into the session since I don't know what you're using it for later. eg part of a DB query which would be a security risk if it is anything other than EN or RO.
if($_GET['language'] == 'EN')
{
$_SESSION['language'] = 'EN';
}
if($_GET['language'] == 'RO')
{
$_SESSION['language'] = 'RO';
}
}
//now check the session variable, which will have been updated above if changed
if(isset($_SESSION['language']))
{
// already have a language saved, so let's use it
define('LANGUAGE_ID', $_SESSION['language']);
}
else
{
// no language from URL and no language saved, so default to english
define('LANGUAGE_ID', 'EN');
}
Related
I have a language selector script. The codes looks like these:
index.php
<?php
session_start();
$allowed_lang = array('en','de');
if (isset($_GET['lang']) === true && in_array($_GET['lang'], $allowed_lang) === true) {
$_SESSION['lang'] = $_GET['lang'];
} else if (isset($_SESSION['lang']) === false) {
$_SESSION['lang'] = 'en';
}
include ''. $_SESSION['lang'] .'.php';
echo $lang['hello'] , '!';
?>
<ul>
<li>English</li>
<li>Deutsch</li>
</ul>
en.php
<?php
$lang = array(
'hello' => 'Hello',
);
?>
de.php
<?php
$lang = array(
'hello' => 'Hallo',
);
?>
The default language is english of course, and if I select another language (e.g German), then after the language change the url is index.php?lang=de. After switching to English the url is index.php?lang=en.
How can I remove or hide ?lang=en or ?lang=de from the url, so if I click the "deutsch" link, tthe language will switch but not append anything to the current URL (e.g index.php)?
Thank you in advance for your answers!
Instead of using GET parameters for the language selection, you could use POST parameters. So you'd have to make the language selector a form which calls the document itself (i.e. without an action attribute, but with method="post") and send and receive the language selection as a POST variable, i.e. not visible.
After that (i.e. for the following pages the user switches to) you could use session variables, cookies or local storage to save and call the selected language.
I have a form on every page that is a quick navigation. Based on the value of the drop-downs it will redirect to a specific page. The URL after using it will look like /page?first=abc&second=def&third=ghi. If you leave the page and go to pageB the url will just say /pageB. I'm trying to create a SESSION for second if it's set and when it's no longer set (navigating to a different page not using the quicknav) still store the value when it was set. Here is what I have so far...
function storeVariable {
global $saved_second
$store_second = $_GET['second'];
$saved_second ='';
if (isset($store_second)) {
$_SESSION['second'] = $_GET['second'];
$sessionsecond = $_SESSION['second'];
$saved_second = $sessionsecond;
}
return $save_second
}
In the header I have
global $saved_second;
echo $saved_second;
The above code is fine for the initial page the quick navigation goes to. It shows the value of $saved_second in the header. I'm not sure how I would say
if (isset($saved_second) && (!isset($stored_second) {
USE SESSION THAT WAS CREATED WHEN $stored_second WAS SET
I thought something like...
if ($store_second != $sessionsecond){
but that doesn't work either because the session was created in an IF statement.
Then I tried...
IF (!isset($saved_second)) {
IF (isset($_GET['second'])) {
$_SESSION['second'] = $_GET['second'];
$saved_second = $_SESSION['second'];
}}
Any suggestions?
I have a problem with my PHP (version 5.6) session. They are not being kept between pages, or even when I refresh my page.
On top of all my files I have :
<?php
session_start();
(there is no space or anything before).
When the user click on link, it's calling a function to set the website language to English or French. After some verification on the $_GET["lang"] value, I create the session like that :
$_SESSION["lang"] = $_GET["lang"];
If I do
var_dump($_SESSION["lang"]);
the server return string(2) "fr" or string(2) "en" regarding to which language the user select. Until there, everything is working great ;)
Problem is if I refresh the page, or go to another one, and try to return the session value, it's always NULL...
I know i could user other ways to translate the website, but I need the sessions to work for other functionality of my application (login, ...)
Because it was working few days ago, I first supposed it was a changed on the server, so I've contact my server administrator, but they told me they didn't change anything.
I have the PHP error reporting set to E_ALL but no errors are displayed...
If someone could help me with that, it would be great, i'm stuck on this bug since 3 days now ^^.
Thanks !
EDIT :
session_start();
var_dump($_SESSION["lang"]);
if(!isset($_SESSION["lang"]) || $_SESSION["lang"] == null){
$_SESSION["lang"] = "fr";
}
if(isset($_GET["lang"]) && ($_GET["lang"] == "fr" || $_GET["lang"] == "en")){
$_SESSION["lang"] = $_GET["lang"];
}
var_dump($_SESSION["lang"]);
from the limited code you're providing, my guess is that the $_GET argument isn't always set, which would then set the session to null.
try this...
if(isset($_GET["lang"])) {
$_SESSION["lang"] = $_GET["lang"];
} else {
echo 'lang not set';
}
EDIT: OP provided additional code.
This will return 'fr' if no value, or if an acceptable value hasn't been provided in the URL arguments. it's similar to what you have, however, I've wrapped the argument check in parentheses to make it a little tighter and changed the order. Your code was returning 'en' if nothing was provided.
session_start();
if(isset($_GET['lang']) && ($_GET['lang'] == 'fr' || $_GET['lang'] == 'en')) {
if($_GET['lang'] == 'fr') {
$_SESSION['lang'] = 'fr';
}
elseif($_GET['lang'] == 'en') {
$_SESSION['lang'] = 'en';
}
} else {
$_SESSION['lang'] = 'fr';
}
var_dump($_SESSION['lang']);
I found it !
My index.php file was encoded in UTF-8, i changed it to UTF-8 without BOM and it worked !
Really weird bug, I hope it will help someone ;)
After I read some stuff on-line I came up with this PHP script to detect the browser's language and redirect the user to the correct website's version. Short said, if the user has a browser in Swedish, then the script should redirect to index.php, if not, then it should redirect the user to en.php.
It works fine in some computers and mobile phones and in others it blocks the website. I suppose that the script is not OK and is causing some conflict in older browsers.
So, could you please take a look at my script and tell me if I am doing anything wrong and how can I fix it?
Cheers!
<?php
include ('administration/fonts.php');
?><?php
$lc = ""; // Initialize the language code variable
// Check to see that the global language server variable isset()
// If it is set, we cut the first two characters from that string
if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])){
$lc = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
}
// Now we simply evaluate that variable to detect specific languages
if($lc == "sv"){
header("location: index.php");
exit();
}
else if($lc == "en"){
header("location: en.php");
exit();
}
?>
P.S. - Yes, the script is before the tag and there are no spaces between the "?>" tag and tag.
New answer after added details from the OP.
English users should be redirected, but Swedish user should stay on this site, so we'll rewrite the code like this (I added comments with // Reeno):
<?php
include ('administration/fonts.php');
$lc = ""; // Initialize the language code variable
// Check to see that the global language server variable isset()
// If it is set, we cut the first two characters from that string
if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])){
// Reeno: I added strtolower() if some browser sends upper case letters
$lc = strtolower(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2));
}
// Now we simply evaluate that variable to detect specific languages
// Reeno: Redirect all non-Swedish users to the English page (they can have "en", but also "dk", "de", "fr"...
if($lc != "sv"){
header("location: http://www.domain.com/en.php");
exit();
}
// Reeno: Swedish users stay on this site
?>
HTML code...
Old answer
You check for $lc == "sv" and $lc == "en" but you forgot the third case: $lc could be empty!
Rewrite the if at the end like this, so everybody with a non Swedish browser will get to en.php:
if($lc == "sv"){
header("location: index.php");
exit();
}
else {
header("location: en.php");
exit();
}
?>
btw header("location: ..."); requires an absolute URI like header("location:http://www.domain.com/en.php"); (some clients also accept relative URIs)
I am using the internationalisation block for basic page translation between Spanish and English. By default the content is in Spanish. When I change the language to English the page content is translated, however the autonav appears blank. I have used page CID's to determine the language differences. In my header.php file for my theme I am using the follow statement:
if(!$c->getAttribute('english_menus')) {
$lh = Loader::helper('section', 'multilingual');
$lang = $lh->getLanguage();
$bt = BlockType::getByHandle('autonav');
$bt->controller->displayPages = 'custom';
if ($lang == "en_EN"){
$bt->controller->displayPagesCID = 166; //English section cID
} else {
$bt->controller->displayPagesCID = 171; //Spanish section cID
}
$bt->controller->orderBy = 'display_asc';
$bt->render('templates/top_nav/view');
}
This displays the top level navigation fine. however when the country flag is selected the auto nav menu disappears and does not display in english
Many Thanks
Do you mean to have the
if (! $c->getAttribute('english_menus')) {
at the top?
It doesn't make much sense to me. Any chance your english page (or whatever page you land on after the country flag) has that attribute set to something?