I have the following code:
<?php
// List of available localized versions as 'lang code' => 'url' map
$sites = array(
"da" => "http://www.mysite.com/",
);
// Get 2 char lang code
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
// Set default language if a `$lang` version of site is not available
if (!in_array($lang, array_keys($sites)))
$lang = 'en';
// Finally redirect to desired location
header('Location: ' . $sites[$lang]);
?>
This will redirect the user to the Danish (da) version of the site, which is the main site, if it's a Danish website client. This is excellent.
But, I want, if the user isn't Danish, but Polish, German, etc. it redirects them to the English version of the site, which is located at the subdomain http://en.mysite.com/
How do I implement that into the existing code? Thanks in advance!
- Frederick Andersen
EDIT
A solution like;
$sites = array(
"da" => "http://www.mysite.com/",
"en" => "http://en.mysite.com/"
);
Doesn't work since it creates a loop error when redirecting - at least in Google Chrome.
EDIT 2
session_start();
if (isset( $_SESSION['redirect']))
{
// do nothing / continue with rest of page
}
else
{
$_SESSION['redirect'] = true;
// List of available localized versions as 'lang code' => 'url' map
$sites = array(
"da" => "http://www.mysite.com/",
"en" => "http://en.mysite.com/"
);
// Get 2 char lang code
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
// Set default language if a `$lang` version of site is not available
if (!in_array($lang, array_keys($sites)))
$lang = 'en';
// Finally redirect to desired location
header('Location: ' . $sites[$lang]);
exit();
}
You would need to add the default option to your array:
$sites = array(
"da" => "http://www.mysite.com/",
"en" => "http://en.mysite.com/"
);
Edit: If you are calling this same code in "http://en.mysite.com/" again, it will create a loop. The obvious solution would be to not call this code there, but an alternative solution would be to set a session variable to indicate that the language selection has already taken place.
To add that session variable you could do something like:
session_start();
if (isset( $_SESSION['redirect']))
{
// do nothing / continue with rest of page
}
else
{
$_SESSION['redirect'] = true;
// your language selection code with header call
exit();
}
$sites = array(
"da" => "http://www.mysite.com/",
"en" => "http://en.mysite.com/"
);
how about this
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
$sub_domain = ($lang == "da") ? "www" : "en";
$link = "http://".$sub_domain.".mysite.com/";
header('Location: ' . $link);
Related
I have the following index.php script to distribute users by country. I currently only have a index.html page which would be for english users.
The problem is when i run php -S localhost:8000 -t site/ then I can only view the php code from index.php in black and white (plain html)... it doesn't load the index.html file. What am i doing wrong?
index.php file:
// The script works according to the user browser language
$sites = array(
"en" => "index.html",
// "cn" => "index_cn.html",
// "de" => "index_de.html",
// "el" => "index_el.html",
// "es" => "index_es.html",
// "fr" => "index_fr.html",
// "id" => "index_id.html",
// "it" => "index_it.html"
);
// Determine the 2-character language en, etc.
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
// Setting default language if variable $lang do not match more than one value from the array $site
if (!isset($sites[$lang])) {
$lang = ‘en’;
}
// Redirect the user to the desired page.
header('Location: ' . $sites[$lang]);
exit();
index.html:
<h1> Hello <h1>
Thanks for any insight!
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 am trying to implement a language based redirection. Our site has sub-domains for different languages ie es.domain.com // de.domain.com etc. What i am trying to do is include the following code on all pages
<?php
$lng = array( 'en'=>'www', 'de'=>'de'); // more languages
$defaultLang = 'en'; // Index of the default (and fallback) language
$cookieName = "_dmLang"; // Set up the cookie name
$lang = trim(substr($_GET['lang'], 0, 2));
if (empty($lang)) $lang = trim($_COOKIE[$cookieName]);
if (empty($lang)) $lang = trim(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2));
if (empty($lang) || empty($lng[$lang])) $lang = $defaultLang;
setcookie($cookieName, $lang, time()+3600*24*30);
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://".$lng[$lang].".domain.com" . $_SERVER['REQUEST_URI']);
?>
The language will change when you call /?lang=de
Are there any server/load implications when using this code? Is there a better way to implement this?
I made a website, with 3 lang on it, and site works fine (localhost) but when I upload it, default lang is EN and whenever I change lang on the web, let's say from EN to FR, page goes on that lang in this case on FR and redirect me on home page and all text on site is on FR, that's ok, but after that, when I try to click on let's say about us, then on it's own change it back to default lang, and show me that page (about us) but on default lang...
Here is sample of code how it's look...
This is select.php and I include_once this file on every page...
<?php
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'];
}
else
{
$lang = 'en';
}
switch ($lang) {
case 'en':
$lang_file = 'lang.en.php';
break;
case 'fr':
$lang_file = 'lang.fr.php';
break;
case 'de':
$lang_file = 'lang.de.php';
break;
default:
$lang_file = 'lang.en.php';
}
include_once 'lang/'.$lang_file;
This is lang.en.php...
$lang = array();
// menu
$lang['txt1'] = 'Home';
$lang['txt2'] = 'About Us';
$lang['txt3'] = 'Services';
$lang['txt4'] = 'Contact';
It's the same for other two languages...
and I simply put in index.php this...
<ul>
<li><em><b><?php echo $lang['txt1'];?></b></em></li>
<li><em><b><?php echo $lang['txt2'];?></b></em></li>
<li><em><b><?php echo $lang['txt3'];?></b></em></li>
<li><em><b><?php echo $lang['txt4'];?></b></em></li>
</ul>
When is on default lang, I can navigate fine, I can go on any page without problems, but problem is when I try to change site into other language(s),
let's say I'm on page www.example.com/about.php and I want to see this page on let's say FR, site will redirect me, on www.example.com/index.php?lang=fr (this is ok) and then I can see FR lang on site but only on home page, but when I try to go on www.example.com/about.php then site put back default lang, in this case EN...
Strange thing is that this script works fine on localhost...
Any idea why?
Update:
OUTPUT:
Array ( [lang] => lang value to put in session [something_1] => something value to put in session )
Array ( [lang] => new lang value but not set in session [something_1] => something value to put in session )
<?php
session_start();
$lang = "lang value to put in session";
$something = "something value to put in session";
$_SESSION['lang'] = $lang;
$_SESSION['something_1'] = $something;
print_r($_SESSION);
$lang = "new lang value but not set in session";
$something = "new something value but not set in session";
print_r($_SESSION);
PHP shouldn't change the second array
Array ( [lang] => lang value to put in session [something_1] => something value to put in session )
Array ( [lang] => new lang value but not set in session [something_1] => something value to put in session )
It should look like this:
Array ( [lang] => lang value to put in session [something_1] => something value to put in session )
Array ( [lang] => lang value to put in session [something_1] => something value to put in session )
Any idea why?
Be sure your files does NOT echo anything before settings the session
if your saving files as UTF , be sure it's UTF without BOM
disable display_errorsin ini file . try ini_set('display_errors',false);
i hope this helps , i encountered this problem many times .