I created 3 language packs for my site: English, Spanish and French. I am just having trouble implementing them based on user selection. I have the following drop down menu:
<select onchange='document.location.href=this.options[this.selectedIndex].value;'>
<option>Select</option>
<option value="?lang=eng">US English</option>
<option value="?lang=esp">Español</option>
<option value="?lang=fra">Français</option>
</select>
How can I include the language files based on what the user selects, I just don't know what to put as the condition in the if statement.
Thanks.
As you're bringing in the option via a query string, you can access it with
$lang = $_GET['lang']
At first, save the value that the user selected in the user session. e.g.:
switch($_POST['lang']) {
case 'en': $_SESSION['lang'] = 'English'; break;
case 'sp': $_SESSION['lang'] = 'Spanish'; break;
default: $_SESSION['lang'] = 'English'; break;
}
On every page request, fetch the language files from the relevant language folder according to the value the you saved.
For example, this how will look the files structure:
English
Global.php
Register.php
Spanish
Global.php
Register.php
Then, whenever you need to load a text file, use:
function load_text_file($filename) {
include 'languages/' . $_SESSION['lang'] . '/' . $filename.'php';
return $txt; // $txt should be an array
}
//...
$text = array();
$text += load_text_file('Global');
$text += load_text_file('Register');
For my template, that piece of code is
$_SESSION['language'] = 'english';
Related
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.
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.
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');
}
1st problem
I have a dynamic site where the pages are included in the index page dynamically in the following way:
$page = (empty($_GET["page"])) ? "home" : $_GET["page"];
$page = "folder/".basename($page).".php";
if (is_readable($page)) {
include($page);
} else {
echo 'Site doesn't exist!';
exit;
}
But there is also a language menu which changes the languages with another $_GET parameter:
<form name="switch" id="lang" action="" method="get">
<select name="multilingual" onchange="this.form.submit()">
<option value="lang_it">Italian</option>
<option value="lang_de" >German</option>
<option value="lang_en">English</option>
</select>
</form>
if(!isset ($_COOKIE['multilingual'])){
$lingua = null;
}else{
$lingua = $_COOKIE['multilingual'];
}
if(isset($_GET['multilingual'])){
$lingua = $_GET['multilingual'];
$_SESSION['multilingual'] = $lingua;
setcookie('multilingual', $lingua, time() + (3600 * 24 * 30));
}
switch($lingua){
case 'lang_it': include_once('file_it.php'); break;
case 'lang_de': include_once('file_de.php'); break;
case 'lang_en': include_once('file_en.php'); break;
default: include_once('file_it.php'); break;
}
The second $_GET parameter is passed to the URL but on the pages it will redirect me always to the default home page.
How to change the language without beiing redirected to the home page?
In other words, ho to change the language and stay on the same page?
2nd problem
Is it possibile to change the lang_it, lang_de, lang_en parameter in the URL to get redirected to the current language of the page.
If this will happen, how to change the language switcher when the page changes to another language?
<form name="switch" id="lang" action="<?php echo $page; ?>" method="get">
<select name="multilingual" onchange="this.form.submit()">
<option value="lang_it" <?php if($_COOKIE['multilingual']=='lang_it') echo "selected='selected'" ?>>Italian</option>
<option value="lang_de" <?php if($_COOKIE['multilingual']=='lang_de') echo "selected='selected'" ?>>German</option>
<option value="lang_en"<?php if($_COOKIE['multilingual']=='lang_en') echo "selected='selected'" ?>>English</option>
</select>
</form>
this.form.submit() not gonna work without page refresh/redirect.
If i understood your problem correctly:
You need to start using one-page JS frameworks
or load language vars dynamically in the background via (again) JS (load laguage vars via json config populate/change/mutate DOM contents)
EDIT
Forgot to mention: be careful with your "dynamic" website. The way you do it is quite unsafe, security wise.
EDIT
Try changing forms action attibute. Put there current filename
EDIT
More secure way is to have an array with pages that are actually exist and safe and after form submit check $_GET parameters against that array
<? $config = parse_ini_file('/list.ini', true); ?>
<?echo'<select id="LBox" name="listbox" size="20" style="display:none;">';
foreach($config[MPR] as $id=>$label){
switch ($id)
{
case ($id==select):
echo'<option value="0" selected="selected"></option>';
break;
case ($id>0 && $id<=10):
echo'<optgroup label="'.$label.'">';
break;
case ($id>10 && $id<=20):
echo'</optgroup>';
break;
default:
echo'<option value="'.$id.'">'.$label.'</option>';
}
}
echo'</select>';?>
Above is code that builds a hidden list box and fills its options from an INI file. I would Like to replicate this for each section in my ini, but I am unsure of the best way to do this, other than copy and paste this 8 times with a new $config[x] value. Is there a way to replicate this for each section?
Write a function that does it and takes whatever changes (e.g. $config[x]) as an argument.