PHP language redirection can't switch back - php

I have a very simple static one-page website, which I have available in English (default) and also in German - what I would like to achieve is to automatically redirect German users to the German version of the website which seems to work with the code I use here:
<?php
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
switch ($lang){
case "de":
header("Location: http://www.website.net/de");
break;
}
?>
The problem now is that I still would like to give them the option to switch to English as well however once they click English they will always be redirected to German version of the site and are stuck with it. Is there a way to have this fixed and working properly?

You need another variable. You can do it with a URL param or a cookie, either way will get you what you want.

Related

Getting a user's language in PHP

I am trying to do localization for a site I'm working on at the moment and am doing something like this:
if(!isset($_SESSION['lang'])){
$_SESSION['lang'] = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
}else if(isset($_GET['lang']) && !empty($_GET['lang'])){
$_SESSION['lang'] = $_GET['lang'];
}
This is fine and works as expected, however, sometimes there is no value for $_SERVER['HTTP_ACCEPT_LANGUAGE'], after a quick test of visitors to our site approximately 20% actually have a value and 80% do not.
Is there a better way to detect a user's language?
As suggested I ended up falling back to English in an else statement if I cannot determine the language

PHP - Make manual language selector override GeoIP redirect

I'm using Maxmind's GeoIP redirect service, but as we know it isn't 100% accurate. That's why there's a manual language selector on the header of my website, so people who were mistakenly redirected to the wrong country can change it.
The problem is to add an exception to the GeoIP itself, to make the manual selector override the GeoIP, couldn't come up with something to do it, thought about doing with cookies but there must be a better way
<?php
include("geoip.inc");
$ip=$_SERVER['REMOTE_ADDR'];
$gi = geoip_open("GeoIP.dat",GEOIP_STANDARD);
$country_code = geoip_country_code_by_addr($gi, "$ip");
geoip_close($gi);
switch($country_code) {
case "BR": header("Location: https://mysite.com/br"); break;
case "ES": header("Location: https://mysite.com/es"); break;
default: header("Location: https://mysite.com");
}
?>
Select your language:
English
Spanish
Portuguese
You need to keep the database updated in order to be as much accurate as possible, and you can accomplish this using the instructions listed here, in the occasion of an incorrect IP detection, i would second your usage for cookies and i would recommend setting the cookie's life time as long as possible.

Auto detect language and redirect user

I am doing my own website and I managed to write some code that makes directs user to the language version according to the browser's language. Here is the script:
<?php
if ($_SERVER["HTTP_ACCEPT_LANGUAGE"] == "sv")
header("location: index.php");
if ($_SERVER["HTTP_ACCEPT_LANGUAGE"] == "pt")
header("location: pt/index.php");
else
header("location: en/index.html");
?>
I have put this in the index.php before the . It seems to be working because I am not in an English speaking country but my browser is in English and I am being redirected to the English version.
Is this correct? Is there a better/cleaner way to do this?
PHP 5.3.0+ comes with locale_accept_from_http() which gets the preferred language from the Accept-Language header.
You should always prefer this method to a self-written method as the header field is more complicated than one might think. (It's a list of weighted preferences.)
You should retrieve the language like this:
$lang = locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']);
But even then, you won't just have en for every English user and es for Spanish ones. It can become much more difficult than that, and things like es-ES and es-US are standard.
This means you should iterate over a list of regular expressions that you try and determine the page language that way. See PHP-I18N for an example.
Well, I came across some problems with my code which is no surprise due to I am not a PHP expert. I kept therefore on searching for a possible solution and I found the following code on another website:
<?php
// Initialize the language code variable
$lc = "";
// 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 == "fr"){
header("location: index_french.php");
exit();
} else if($lc == "de"){
header("location: index_german.php");
exit();
}
else{ // don't forget the default case if $lc is empty
header("location: index_english.php");
exit();
}
?>
This did the job perfectly! I only had a problem left. There was no way to change language, even with direct links into another language because as soon as the page was loading, the php block would redirect me to the borwser's language. This can be a problem if you are living in another country and have for instance Swedish as a mother language but you have your browser in English because you bought your computer in the UK.
So my solution for this issue was to create folders with a duplicate version for every language (even the one for the main language) without this php code on the index.html (and therefore not index.php). So now my website is auto detecting the language and the user also has the option to change it manually in case of they want!
Hope it will help out someone else with the same problem!
I think your idea is great. May be help you shortest code:
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
header("location: ".$lang."/index.php");
That should work fine. You could also use http_negotiate_language and discusses here
Most useful this code
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
if(file_exists('system/lang/'.$lang.'.php'))
{
include('system/lang/'.$lang.'.php');
}else{
include('system/lang/en.php'); //set default lang here if not exists translated language in ur system
}

User language redirect + default language on root : it doesn't work

Sorry for my approximative english.
I'm using this method to provide a redirection according user language, and keep the data in cookies:
http://dallascao.com/en/use-cookies-to-remember/
However, the redirection doesn't work if I decide to choose the domain root as the default language, like this way:
<?php $lang=$_COOKIE["lang"];
switch ($lang) {
case 'en':
header('Location: http://hawalove.com/');
break;
case 'fr':
header('Location: http://www.hawalove.com/fr');
break;
#Get the default language of the browser if no cookies are found.
default:
$lang = getDefaultLanguage();
switch ($lang) {
case 'fr' :
header('Location: http://www.hawalove.com/fr');
break;
default:
header('Location: http://hawalove.com/');
break;
}
break;
}
?>
May you help me to achieve this ? I'd like to have english version at root (mydomain.com) and french version at mydomain.com/fr.
Thanks.
I'd guess that "doesn't work" means "infinite redirection loops for English speakers".
All you need is a way to not redirect at all when the chosen language is English. Add an associative array which maps the supported languages to the redirect location then do this:
Grab $lang from $_COOKIE["lang"].
If $lang is not a supported language (or not even set) then $lang = getDefaultLanguage().
If $lang is not 'en', then redirect, otherwise display the English homepage without redirecting at all.
You can use the associative array for (2) and (3). The basic strategy is simple: don't redirect at all if the language ends up being English. You'd probably want to set your language cookie once you have a valid language too.

Language neutral entry pages

My old web site has an index.html page … nothing strange! Everything is fine.
The new web site has an english and a french version, so the new index is index.php?lang=eng…. That makes sense.
I don’t like to make a front page that will say “english” or “french”. But that’s not good for ranking or seo.
So the question is: How do I manage to get a default index.php with request (?lang=eng) to become the front page?
domain.com/en/index.php
domain.com/fr/index.php
Use url rewriting with regular expressions (mod_rewrite, ISAPI, whatever) to handle requests to relevant pages so
domain.com/en/index.php REWRITE TO domain.com/index.php?lang=en
domain.com/fr/index.php REWRITE TO domain.com/index.php?lang=fr
This way your pages are two seperate pages to search engines but handled via one gateway in code. I'm not a regex expert but it would be a very simple regex I would imagine
I'm not sure I understand the question. It seems to have two parts:
How to provide a default language of English:
$lang = empty($_GET['lang']) ? "eng" : $_GET['lang'];
Do you also have a problem of where to put the English/Francais links so search engines don't ding you? I wasn't aware of this problem.
It might also help to let us know if you're using a CMS, and if so which one.
Unless I'm misunderstanding the question, in index.php, when you check the language, put something like this:
$lang = #$_GET['lang'];
if ( empty($lang) ) $lang = 'eng';
Just put an argument in the php code that says :
if (lang == "") // haven't done php in a while so the syntax is probably wrong
{
lang = "eng";
}
In other words, if there isn't an argument on the lang variable, you can just set it to be eng automatically, and so the first page will default to English every time, unless told otherwise.
Just make the default english and offer an option on the index page to change to french? This, of course, depends on what language most of the visitors speak, which isn't all that hard to figure out with visitor logs.
I would use a neutral URL for entry, such as:
http://example.com/foo/bar
On this page I would do some language negotiation or simply ask the user for the prefered language. Then I can redirect to the language specific URL:
http://example.com/en/foo/bar
what do you think about that solution
<?php
$lang = $_GET['lang'];
if ( empty($lang) ) $lang = 'fra';
header( 'Location: http://acecrodeo.com/new/01-acec.php?lang='.$lang) ;
?>

Categories