My website has two languages (English and German) between which a user can chose by clicking a button. It then sets a cookie to store this decision for 24h. I'm also using the php-i18n class from GitHub. It works by storing all language texts in specific *.ini files. According to the chosen language, either the German or English text gets displayed via PHP variables. I hope I explained it well enough.
That all works, except for my "legal details" page. Since this page has a lot of text, I made two versions of this one page, one English, one German. Depending on what language is chosen, I want to display the according page version. But it somehow always ends up being the "default German" version. I'm probably missing something very obvious as to why my code isn't working.
PHP in the header (dynamic, shows up on all pages before all else):
require_once 'i18n.class.php';
$i18n = new i18n('lang/lang_{LANGUAGE}.ini', 'langcache/', 'de');
// Parameters: language file path, cache dir, default language (all optional)
$i18n->setForcedLang('de');
// language buttons & set cookie
if(isset($_GET['lang'])) {
$lang=$_GET['lang'];
if ($lang == 'de') {
$i18n->setForcedLang('de');
setcookie('language', 'lang-de', time() + 86400, '/');
echo "german cookie set!";
}
if ($lang == 'en') {
$i18n->setForcedLang('en');
setcookie('language', 'lang-en', time() + 86400, '/');
echo "english cookie set!";
}
}
// get language cookie
if (isset($_COOKIE['language']) && $_COOKIE['language']== "lang-de") {
echo "german cookie is really set, now get it!";
$i18n->setForcedLang('de');
} else if (isset($_COOKIE['language']) && $_COOKIE['language']== "lang-en") {
echo "english cookie is really set, now get it!";
$i18n->setForcedLang('en');
} else {
echo "no cookie set";
}
// init object: load language files, parse them if not cached, and so on.
$i18n->init();
PHP on the legal details page:
include 'templates/header.php';
// show english or german legal details
if(isset($_GET['lang'])) {
$lang=$_GET['lang'];
if ($lang == 'de') {
echo "k, german impressum";
include 'impressum-de.php';
} else if ($lang == 'en') {
echo "english impressum";
include 'impressum-en.php';
}
} else if ($language == 'de') {
echo "german impressum b/c cookie";
include 'impressum-de.php';
} else if ($language == 'en') {
echo "english impressum b/c cookie";
include 'impressum-en.php';
} else {
echo "default German impressum";
include 'impressum-de.php';
}
The buttons:
<div class="language">
DE/
EN
</div>
Does anyone have some input? Ideas how to improve the code are also always much appreciated since I'm still learning.
Try This: (not "else if" -> its "elseif")
include 'templates/header.php';
// show english or german legal details
if(isset($_GET['lang'])) {
$lang=$_GET['lang'];
if ($lang == 'de') {
echo "k, german impressum";
include 'impressum-de.php';
} elseif ($lang == 'en') {
echo "english impressum";
include 'impressum-en.php';
}
} elseif ($language == 'de') {
echo "german impressum b/c cookie";
include 'impressum-de.php';
} elseif ($language == 'en') {
echo "english impressum b/c cookie";
include 'impressum-en.php';
} else {
echo "default German impressum";
include 'impressum-de.php';
}
Related
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 :)
I am coming to you to find out if it is possible to change the path in a URL using a PHP variable.
My goal would be to organize my website with 2 folders (one for each language), choose the language on a random page in one of the two folders and be able to change the language by modifying the URL.
For example, I'm on http://truc.com/fr/test.php; I change the language and I get http://truc.com/en/test.php.
I've done a test that works but I'd prefer something more dynamic, that will work on all the pages of the website:
fichier index.php
<?php
session_start();
if (!isset($_SESSION['lang']))
if (!isset($_SESSION['lang']))
$_SESSION['lang'] = "fr";
else if (isset($_GET['lang']) && $_SESSION['lang'] != $_GET['lang'] && !empty($_GET['lang']))
{
if ($_GET['lang'] == "fr")
$_SESSION['lang'] = "fr";
else if ($_GET['lang'] == "en")
$_SESSION['lang'] = "en";
}
if($_SESSION['lang']==fr)
{
header('Location: http://localhost/site/languages/fr/index.php');
}
else if($_SESSION['lang']==en)
{
header('Location: http://localhost/site/languages/en/index.php');
}
?>
Adjusted your code and added a new variable $page_language that handles the page language and redirects to specific link for various languages and before redirecting to the page a session language is set to remember it on next page . Good luck
if (isset($_SESSION['lang'])) {
$page_language = $_SESSION['lang'];
} elseif (isset($_GET['lang']) && !empty($_GET['lang'])) {
$page_language = $_GET['lang'] ;
} else {
$page_language = "fr" ; //set default language if no session and url has language
}
if ($page_language == "fr") {
$_SESSION['lang'] = "fr"; //set session language for later visits
header('Location: http://localhost/site/languages/fr/index.php');
exit;
} elseif ($page_language == "en") {
$_SESSION['lang'] = "en"; //set session language for later visits
header('Location: http://localhost/site/languages/fr/index.php');
exit;
}
I have faced a problem. The code writes in the html body parts. I want the site to come up as by default when the site is opened. At the moment I click on these codes and it shows up when I click on the page and click on the page again. I want the page to always open in English by default.
<?php
session_start();
if($_GET['lang']) {
$_SESSION['lang'] = $_GET['lang'];
header("Location:index.php");
}
if ($_SESSION['lang'] == "en") {
$lang= "en";
}
elseif ($_SESSION['lang'] == "gr") {
$lang= "gr";
}
else {
$lang= substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,2);
}
include 'languages/'.$lang.'.php';
?>
"I want the page to always open in English by default"
Does this do the trick?
if (! isset($_SESSION['lang'])) {
$_SESSION['lang'] = 'en';
}
By default $lang= substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,2); will work. If you want by default English then change this to $lang= "en".
I have a codebase that uses the logic,
if($_SERVER['HTTP_HOST'] == 'fr.example.com')
{
$_SESSION["lang"] = "fr";
}
elseif($_SERVER['HTTP_HOST'] == 'en.example.com')
{
$_SESSION["lang"] = "en";
}
I want:
www.example.com should serve english
www.example.com/fr should serve french.
All the text is served from the database.
How can I use same index.php to serve english and french content based on what language is there in the URI?
Since you want to handle this in PHP anyway, there is no need for an .htaccess file. Just use $_SERVER['REQUEST_URI'], e.g.
$uri = $_SERVER['REQUEST_URI'];
if (stripos($uri, "/fr/") === 0) {
// handle french
} elseif (stripos($uri, "/de/") === 0) {
// handle german
} else {
// handle english
}
This question already has answers here:
Detect Browser Language in PHP
(16 answers)
Closed 6 years ago.
EDIT: Wasn't a duplicate to began with. Originally was how to improve or use different code to redirect based on the visitor's COUNTRY.
Here is the code I'm currently using:
require_once('geo/geoip.inc');
$gi = geoip_open('geo/GeoIP.dat', GEOIP_MEMORY_CACHE);
$country = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
geoip_close($gi);
if ($country == 'FR') {
header('Location: http://fr.mysite.com');
}
if ($country == 'US') {
header('Location: http://us.mysite.com');
}
on a couple of static (html + javascript) viral sites that bring in around 100,000 visitors a day, sometimes more and sometimes less.
Are there any better (and free) solutions to redirect based on country? I recently switched from a dedicated server to a VPS and the current code seems to use a lot of CPU usage (or so I'm told by my host). I'll probably just go back to the dedicated server but I would still like to know if there is a better way, one that doesn't stress the server as much.
Also, since I'm redirecting based on language:
// french
if ($country == 'FR') {
header('Location: http://fr.mysite.com');
}
//french
if ($country == 'BE') {
header('Location: http://fr.mysite.com');
}
//french
if ($country == 'CA') {
header('Location: http://fr.mysite.com');
}
//english
if ($country == 'US') {
header('Location: http://us.mysite.com');
}
//english
if ($country == 'UK') {
header('Location: http://us.mysite.com');
}
and extremely tired right now, what's the better way? this or no:
//english
if ($country == 'US') || ($country == 'CA') {
header('Location: http://us.mysite.com');
}
So anyone visiting from US or Canada would be redirected to google.com
Thanks in advance
EDIT: Source ended up being an answer to the duplicate question.
If the only differences between the sites are languages and nothing to do with their actual country, you should be redirecting based on preferred language instead.
However, this gets really complex, as HTML headers can contain multiple languages, with certain languages preferred over others. A solution I found a while back, and unfortunately cannot find the original source to is to make a list of available languages, parse the languages out of the HTML Header with their order of preference, and determine based on that which redirect to follow:
I DO NOT OWN THE prefered_language() FUNCTION AND DID NOT TAKE ANY PART IN ITS CREATION!
But I can't find the original anywhere.. If someone else is able to, please, link it...
$available_languages = array("en", "fr");
$default_language = "en";
function prefered_language($available_languages, $http_accept_language) {
global $default_language;
$available_languages = array_flip($available_languages);
$langs = array();
preg_match_all('~([\w-]+)(?:[^,\d]+([\d.]+))?~', strtolower($http_accept_language), $matches, PREG_SET_ORDER);
foreach($matches as $match) {
list($a, $b) = explode('-', $match[1]) + array('', '');
$value = isset($match[2]) ? (float) $match[2] : 1.0;
if(isset($available_languages[$match[1]])) {
$langs[$match[1]] = $value;
continue;
}
if(isset($available_languages[$a])) {
$langs[$a] = $value - 0.1;
}
}
if($langs) {
arsort($langs);
return key($langs);
} else {
return $default_language;
}
}
if(isset($_COOKIE["client_lang"])){
$lang = $_COOKIE["client_lang"];
}else{
$lang = prefered_language($available_languages, strtolower($_SERVER["HTTP_ACCEPT_LANGUAGE"]));
setcookie("client_lang", $lang, time() + (86400 * 30 * ), "/");
}
header('Location: http://' . $lang . '.mysite.com');
I'd also suggest creating a cookie for the client's preferred language as I have done above, since this function will still require some CPU usage.