Changing the path in a URL for a multilingual website - php

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;
}

Related

How to automatically reflect changes when adding PHP cookie?

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

PHP can't retrieve cookie on one page

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';
}

Multilanguage default opening in php

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".

Is there a better way to do a language stringtable in php? [duplicate]

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)
?>

Session on page load

I'm building multiple language web and found problem. My language is changing depending on session variable and on first load the session is empty,only after refreshing the page it gets the right session variable. How to set the variable before the page load? This is the code :
session_start();
$available_langs = array('en','rus');
if ($_SESSION['lang']=='') {
$_SESSION['lang'] = 'en';
}
if(isset($_GET['lang']) && $_GET['lang'] != ''){
if(in_array($_GET['lang'], $available_langs))
{
$_SESSION['lang'] = $_GET['lang'];
}
}
include('language/'.$_SESSION['lang'].'.php');
session_start();
// Direct override beats session
$lang = $_GET['lang'] ?: $_SESSION['lang'];
$available_langs = array('en','rus');
// If the requested language isn't available, or not provided, fall back to first
if(!in_array($lang, $available_langs))
$lang = $available_langs[0];
// Store it in the session and include the template
$_SESSION['lang'] = $lang;
include 'language/'.$lang.'.php';

Categories