multilingual website using $_SESSION - php

My website has two languages: French and English. The solution I chose is working but is causing me trouble. I use session in order to keep a "clean" url which is the same in English and in France.
My solution:
To choose the language, I locate the user using IP, and if it is not in France, i set
$_SESSION['language'] = "ENG"
Otherwise,
$_SESSION['language'] = "FRA"
Then I includes my file words.php which contains all the text like this:
if( $_SESSION['language'] == "ENG")
{
$word1 = "hello"
$word2 = ....
}
else
{
$word1 = "bonjour"
$word2 = ....
}
Finally on my website, I have several echo $word1;.
To change language, I have a two links (one for each language) to a webpage language.php with a get parameter that just change the session variable and redirect to the webpage:
if($_GET['l']=="1")
{
$_SESSION['language'] = "FRA";
header('Location: ' . htmlspecialchars($_SERVER['HTTP_REFERER']));
}
elseif($_GET['l']=="2")
{
$_SESSION['language'] = "ENG";
header('Location: ' . htmlspecialchars($_SERVER['HTTP_REFERER']));
}
My problem:
My main problem is that Google is indexing my website only in English ( guess because the IP of crawler is not in France?). In google.fr and google.com, my website is in English.
What can I do to have the website indexed in both languages?

You must use additional folders for languages. You can use example.com/fr for French content and example.com/en for English content.
You can tell this website is multilingual and multiregional to Google crawler bots. Edit this html code for your purpose.
<link rel=”alternate” href=”http://example.com/en-gb” hreflang=”en-gb” />
<link rel=”alternate” href=”http://example.com/en-us” hreflang=”en-us” />
<link rel=”alternate” href=”http://example.com/en-au” hreflang=”en-au” />
<link rel=”alternate” href=”http://example.com/” hreflang=”x-default” />

Related

staying on the current page when changing language

I'm setting up a language switcher for my website but have an issue with the actual switcher which always brings back to the homepage (index.php). :-s very annoying
I a complete newbie in php and have been following this tutorial to do so on youtube: https://www.youtube.com/watch?v=cgvDMUrQ3vA
I am trying to implement it on a multi pages website. The actual language swap works like a charm and seems to keep the set language throughout the pages but the mechanism for the switch doesn't because it uses a static link to load index.php + new language attribute, bringing the visiter inevitably back to the homepage instead of reloading the current page.
<?php echo $lang['lang-selector-href'] /* set the <a> tag src="index+lang='x'" */ ?>
<?php echo $lang['lang-selector-switch'] /* display language to be set */ ?>
</a>
i'm using 2 similar files (en.php and fr.php) containing an array to store the translated content
en.php :
<?php
$lang = array(
"lang-selector-href" => "<a href='index.php?lang=fr'>",
"lang-selector-switch" => "Français (French)",
...
...
);
?>
fr.php :
<?php
$lang = array(
"lang-selector-href" => "<a href='index.php?lang=en'>",
"lang-selector-switch" => "English (anglais)",
...
...
);
?>
Question:
How could i could I change the link to:
<a href='current-page.php?lang=fr'> Français (French) </a>
or:
<a href='current-page.php?lang=en'> English </a>
according to the set language?
here is my config.php file
<?php
session_start();
// if no language selected go for english
if (!isset($_SESSION['lang']))
$_SESSION['lang'] = "en";
else if (isset($_GET['lang']) && $_SESSION['lang'] != $_GET['lang'] && !empty($_GET['lang'])) {
if ($_GET['lang'] == "en")
$_SESSION['lang'] = "en";
else if ($_GET['lang'] == "fr")
$_SESSION['lang'] = "fr";
}
// load content
require_once "content/languages/" . $_SESSION['lang'] . ".php";
?>
Sorry if it sounds very basic, i'm sure it is but couldn't find a simple answer that would work...
Thank you!
Make Dropdown list in html which options have language value like below as,
<select onchange="callSomeFunc()">
<option value="fr">French</option>
<option value="en">English</option>
</select>
On change of dropdown list you need to call function which will set selected language value in to php $_SESSION.

How to remove or hide ?lang= from url if select other languages

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.

$_SESSION based language selection is not working properly

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

Detect browser preferred language to serve html pages [duplicate]

This question already has answers here:
Detect Browser Language in PHP
(16 answers)
Closed 8 years ago.
I'm a designer with little coding knowledge outside of html/css. Reading on the 'net I find many references to some php using http_accept_language but cannot find a step-by-step "do this, do that" explanation and consequently I don't know what to do with / where to put the code that people present for the job.
I'm developing a site with an English main home page. There, users are presented with a choice of two branches. One branch will be only in English and the other in English plus 3 Scandinavian languages. The non-English pages are pre-translated, not created on the fly by Google.
I want that after arriving at the main home, a user who opts to go into the 4-language section will be initially served a page in the language of their browser (on all pages there will be always be a menu to manually select another language, if preferred).
Is this a job for php? If so I'd be grateful for some code and help in how to use it.
You can use HTTP_ACCEPT_LANGUAGE header. Save supported languages to array and then loop the header using foreach.
$languages = array('en', 'fi', 'sv', 'no');
$header = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
foreach($header as $lang) {
if(in_array($lang, $languages)) {
header("Location: $lang.php"); // i.e. fi.php or se.php
break;
}
}
HTTP_ACCEPT_LANGUAGE contains something like this:
Accept-Language: en-gb,en;q=0.5
As you can see, there is multiple languages, en-gb and en. That's why it's clever to loop the header. If you prefer functions, here's one:
function get_user_lang() {
$languages = array('en', 'fi', 'sv', 'no');
$header = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
foreach($header as $lang) {
if(in_array($lang, $languages)) {
return $lang; // i.e. fi.php or se.php
break;
}
}
}
echo 'Your language is ' . get_user_lang();
Step-by-step guide:
Create new files for each language. Name them like this: "fi.php" or "se.php".
Place the first code part to very top of your home page. That file must include .php ending, so it must be php file. If you don't understand, that should be where to place it:
<?php
$languages = array('en', 'fi', 'sv', 'no');
$header = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
foreach($header as $lang) {
if(in_array($lang, $languages)) {
header("Location: $lang.php"); // i.e. fi.php or se.php
break;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>title</title>
</head>
<body>
contents
</body>
</html>
Navigate to your home page in your browser. If your browser's language is english, it'll redirect to "en.php", if swedish; "se.php" and so on.
You can see all language codes from this link, swedish is "sv".
If you have the INTL extension you can use Locale::acceptFromHttp().
Here's the example from the PHP documentation.
$locale = Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);
echo $locale;
$locale will be something like "en_US". If you only want the language and not the country, use something like this:
$locale = explode('_', Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']));
echo $locale[0];

Internationalisation language switch?

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?

Categories