PHP changing language version - php

I have a website which i want to create other language version.
I don't want to create folder for each language. I was wondering it
it's possible to add a combobox on each page or on the main one
so that user can setup the language then using php i will
check the option and show the right version. Any suggesting
to achive that?

If you have a combobox, when the user submits it, store the language in the session (session_start(); has to be called) with $_SESSION['lang'] = $_POST['lang'];. I'd advise you to whitelist languages as such:
session_start();
// define language whitelist
$allowedLangs = array('en', 'de');
// only store the new user language if it's an allowed one
if (isset($_POST['lang']) && in_array($_POST['lang'], $allowedLangs)) {
$_SESSION['lang'] = $_POST['lang'];
}
// define the user language based on session data or use 'en' as default if not available
$userLang = isset($_SESSION['lang']) ? $_SESSION['lang'] : 'en';
// parse some language file according to the language
$translations = // TODO load some file with $userLang here
Of course you should adjust this to your own project and environment. For translation files, you can use a plain PHP file that returns an array like such:
<?php
// en.php
return array(
'some.key' => 'Translation',
);
Then if you include that file, the return value of the include will be the array, so in the above code you could do:
$translations = include 'translations/'.$userLang.'.php';
You then have to output all your text through this $translations variable, like echo $translations['some.key'].

if you wanted to use cookies... in the lang files you would include an array of words or content to use.
<?php
if($_GET['language']){
$lang = (string)$_GET['language'];
setcookie("lang", $lang, time()+3600);
header('Location: '.$_SERVER['PHP_SELF']);
die();
}elseif(!isset($_COOKIE['lang'])){
$lang='en';
}else{$lang=$_COOKIE['lang'];}
switch($lang){
case "en":
include('./lang/en.php');
break;
case "fr":
include('./lang/fr.php');
break;
case "pol":
include('./lang/pol.php');
break;
default:
include('./lang/en.php');
break;
}
?>

you mean something along the lines of
if ($_GET['language']) {
include $_GET['language'] . ".php";
}
and then save the languages in a php-file with there name, or a function depending on what you want to do with it

hey for language version.
have languages in combobox.
maintain your current language in session.
When u change language call an ajax call Update the changed language into session and reload the page.
display page view with respect to session stored language.
thats it........

Related

External language file

I have a little bit confusing problem.
I have website with multiple languages. For default labels and headings I made some external php file where I have variables with values for different languages.
For example, In file I have variable
$heading = "First Heading in English"
and variable
$heading = "First Heading in German"...
In session I have stored value for current language, and with if state I know what language variables to take.
My problem is next:
When I load my page for first time, all of fields where I call variables from external language file are empty...
And, when I refresh my page, variables are there, with right value....
Can someone help me with this problem??
I include external file before everything in my php file, with include function.
How do you include the file?
Perhaps first language variable is not saved in session yet.
It is always a good idea to check if language is not set you are going to use some default value i.e english.
if (!isset($language)) {
$language = 'en';
}
Modified this a little bit from what i have written a while ago...maybe this will help you a little bit.
$langSession = $_SESSION['lang'];
if(!isset($_SESSION['lang']){ // if the Session with language was not set
$browserlang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2); // Get the browser setting language, tested it in firefox
if($browserlang == "de"){
$_SESSION['lang'] = "de"; // German
}elseif($browserlang == "nl"){ // Dutch
$_SESSION['lang'] = "nl";
}else{ // Else English
$_SESSION['lang'] = "en";
}
}
include('language.php');

Include PHP code with the same ID

My problem is include() doesn't work in this example:
...
$lang = $_GET["lang"];
$id = $_GET["id"];
if ($lang == "fr"){
include ('indexFr.php?id='.$id);
}
else if ($lang == "ar"){
include ('indexFr.php?id='.$id);
}
else if ($lang == "en"){
include ('indexFr.php?id='.$id);
}
...
I work with this:
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
switch ($lang){
case "fr":
header("Location: indexFr.php?id=".$id);
break;
case "ar":
header("Location: indexAr.php?id=".$id);
break;
case "en":
header("Location: indexEn.php?id=".$id);
break;
default:
header("Location: indexEn.php?id=".$id);
break;
}
But if I want to include something else (not language page) I think this is the right code but, it doesn't work:
include ('www.monsite.com/indexFr.php?id='.$id);
How can I do it?
If your $_GET array already has a value for id, you don't need that query string on the end if you're doing an include. It will use the $_GET array you already have and get the same $_GET['id'] value.
include is, in effect, putting the code of the external file into the PHP code that is already running. So, for example, if you have this file:
index.php?id=5
echo $_GET['id'];
include "otherfile.php";
And then this other file:
otherfile.php
echo $_GET['id'];
The output will be:
55
Because you are effectually creating a file that looks like this:
echo $_GET['id'];
echo $_GET['id'];
The include tag doesn't work with query strings because since it's a local file, the query string isn't used.
If you want to include the file from a different domain, you could try:
include ('http://www.monsite.com/indexFr.php?id='.$id);
That said, including files cross-domain is considered bad practice and will possibly just include the generated HTML and not the PHP. If you're including a file from your local filesystem, you really should just use the $_GET variable that's already there.
You need to specify a full URL for this to work. What you're specifying will look for a file on your local filesystem called indexFr.php?id=123, which is not what you're trying to do. You need an http:// or https:// in there, so it knows to go through a web-server, which will pass your arguments.
http://www.php.net/manual/en/function.include.php
Indeed, they provide an example case which matches yours quite closely:
// Won't work; looks for a file named 'file.php?foo=1&bar=2' on the
// local filesystem.
include 'file.php?foo=1&bar=2';
// Works.
include 'http://www.example.com/file.php?foo=1&bar=2';

Create a multilingual site

I start today in creating a multi-language system for my site.
To do so I created my two language files then created my switch that here below:
link
To call the file I created simple links
<p>EnglishFrançais</p>
When I'm on the home page and I click on "English" translation that do well but if I click on another link to my site I automatically revert to French when I should remain in English!
How can I fix this problem?
Your problem is that you always read the GET variable for language and if it's missing, you switch back to the default one (French), which makes storing it inside a session variable - useless.
You should either add ?lang=XX in all the links and remove the $_SESSION, or you should make the following changes in order to use the session variable:
}else {
$_SESSION['lang'] = 'fr';
include('french.php');
$langage=$_SESSION['lang'];
}
to:
}else {
if(isset($_SESSION['lang'])){
if($_SESSION['lang'] == 'fr'){
include('french.php');
}else{
include('english.php');
}
}else{
$_SESSION['lang'] = 'fr';
include('french.php');
$langage=$_SESSION['lang'];
}
}
That way, if no language alteration is requested, your script will check if it has a stored language variable, before switching to the default one.
Ideally you should use case instead of IFs if you have more than 2 languages.
}else {
switch($_SESSION['lang']){
case 'en':
include('english.php');
break;
case 'es':
include('spanish.php');
break;
//insert other language cases here
default:
include('french.php');
break;
}
}
My advice is, the third option. Many sites have a separate directories for each of languages and look like this: www.domainname.ext/ for the default language and www.domainname.ext/xx/ for the other languages, where xx is the abbreviation of the language (en for English and etc.)

PHP: A way to manually switch languages?

I have a website that has the following language switching algorithm:
First, it detects the default browser language (I do not know why? but Chrome always gives something like en-EN,ru,lv, so Chrome's default language always is English, it seems).
Then it writes the language value into a session variable lang and requests the desired string file (i.e. /assets/includes/en-US/strings.php);
And every string from this file is being included in the HTML code, so the pure HTML has not any plain text in.
Of course, a default language detection is not the reason to stop - I need a manual language switcher like links (LV | EN | RU). So, what id the possible (and maybe the best) way to switch the language and to overwrite the session variable after user clicks to the desired language?
The best way is the simpliest way :)
$langs = array('LV', 'EN', 'RU');
<?php foreach ($langs as $lang): ?>
<?=$lang;?>
<?php endforeach; ?>
so you give the user opportunity to change lang via GET in this example.
Overwrite the session to the sent request:
<?php
if(in_array($_GET['lang'], $langs) {
$_SESSION['lang'] = $_GET['lang']; // to prevent user to change its session to something you don't want to
}
?>
Afterwards you just interact with this session to display content.
You can use redirection, if you have each page written in different language:
(but I guess the logic how to interact with the language you have already implemented from the automatic language detection, but still... let me suggest some ways at fast?)
<?php
if (isset($_SESSION['lang']) && $_SESSION['lang'] !== 'EN') {
header("Location: mysite.com/".$_SESSION['lang']."/index.php");
exit;
}
?>
Or, you can use translation method.
All of your translations are in a database under columns with the same names as your $langs array.
So you output the content from this particular column:
SELECT lang_{$_SESSION['lang']} FROM translations WHERE string = '$string';

problems with inheritance of variables over many pages

I have this code on bootconfig.php that is loaded in all of my pages.
// What languages do we support
$available_langs = array('en','it');
// Set our default language session
$_SESSION['lang'] = 'en';
if(isset($_GET['lang']) && $_GET['lang'] != ''){
// check if the language is one we support
if(in_array($_GET['lang'], $available_langs))
{
$_SESSION['lang'] = $_GET['lang']; // Set session
}
}
// Include active language
include($_SESSION['path'].'languages/'.$_SESSION['lang'].'/lang.'.$_SESSION['lang'].'.php');
//Include LIB
include($_SESSION['path']."lib/auth.class.php");
include($_SESSION['path']."lib/register.class.php");
include($_SESSION['path']."lib/string.class.php");
//Include Other Config File
require_once('./config/stringsecure.php');
require_once("./config/menuconfig.php");
require_once("./config/headerconfig.php");
require_once("./config/contentconfig.php");
require_once("./config/forumconfig.php");
require_once("./config/titleconfig.php");
require_once("./include/vocaboli/vocaboliconf.php");
in the file lang.en.php i have for example $lang['contactme'] but if i use it in menuconfig.php it doesn't exist...
if i put echo $lang['contactme'] at the end of bootconfig it works but in menuconfig not. why?
Probably it is a simple scoping issue. Probably in one of the cases you are inside a function.
Inside a function you can't access global variables.
Probably $GLOBALS['lang']['contactme'] would work.
See the details here:
http://www.php.net/manual/en/language.variables.scope.php
As it stands here menuconfig will appear in bootconfig but not the other way around.
I think the includes are backwards: if you want $lang to appear in menuconfig you need to use require_once( 'bootconfig.php' ) in that file.

Categories