Need help implementing a multilingual website using php - php

I am trying to create a multilingual website
I may be going about this the wrong way but here it goes
So just to test first in index.php i have:
<?php
$CURRENT_LANG = "en";
require'./includes/essentials/config.php';
include_once'./includes/essentials/header.php';
include_once'./includes/plugins/search-bars/global-nav.php';
include_once'./includes/essentials/footer.php';
php echo $test
?>
in my config.php i have:
<?php
if($CURRENT_LANG == "en"){
include_once $_SERVER['DOCUMENT_ROOT'].'/gps/includes/language/lang-en.php';
}else{
if($CURRENT_LANG == "pt"){
include_once $_SERVER['DOCUMENT_ROOT'].'/gps/includes/language/lang-en.php';
}
}
?>
in lang-en.php i have:
<?php
$test = "Hi";
?>
in lang-pt.php i have:
<?php
$test = "Olá";
?>
The question at hand is how would i make it so that the user can change the var from en to pt so i can have to php files with all the translations needed?
Or is this a really bad way to implement multiple languages?

Related

Trouble with PHP sessions for including langauge files

I'm trying to rework my company's old website. Currently it supports two languages - English and Hebrew. The way it works is that it simply runs include_once on a language file "lang_en.php" or "lang_he.php" depending on what is contained in $_SESSION['lang'], which would be either 'en' or 'he'.
Right now I'm working on the first pages - Index.php (homepage), Login.php and Register.php. All 3 of them start out exactly alike with a bit of a nested include call.
<!DOCTYPE html>
<html lang="en">
<?php
include '../Common/HtmlHead.php';
?>
The file HtmlHead.php is where I run session_start(), include the file LanguageParser and do some other buildup for the html part of the pages.
<?php
session_start();
include_once("../../Controller/Config.php");
include_once("../../Tests/ObjectModels/ServerResponse.php");
include_once("../../Tests/ObjectModels/Users/User.php");
include_once("../Language/LanguageParser.php");
?>
Then finally is the file LanguageParser.php which looks into GET and SESSION and determines which language file to run include_once on:
if (isset($_GET['lang'])) {
$_SESSION['lang'] = $_GET['lang'];
}
// Include Language file
if (isset($_SESSION['lang'])) {
$langFile ="lang_" . $_SESSION['lang'] . ".php";
if (file_exists($_SERVER['DOCUMENT_ROOT']."/Logic/SmartView/Language/".$langFile)) {
include_once $langFile;
} else {
include_once "lang_en.php";
}
} else {
include_once "lang_en.php";
}
Yet for some reason, the language saved in $_SESSION['lang'] just doesn't seem to stick. Each page behaves as if it has its own session with its own language saved. Even if I change language, navigate to another page, then navigate back, the language reverts.
The session cannot start when some output has been sent to the client. You can make a trick and use $_SESSION before any HTML or echo call.
LanguageParser.php
<?php
session_start();
$lang = 'en';
if (isset($_GET['lang'])) {
$_SESSION['lang'] = $_GET['lang'];
}
if (isset($_SESSION['lang'])) {
$lang = $_SESSION['lang'];
}
?>
<!DOCTYPE html>
<html lang="<?php echo $lang?>">
<?php
include '../Common/HtmlHead.php';
?>
HtmlHead.php
// Include Language file
$langFile ="lang_" .$lang . ".php";
if (file_exists($_SERVER['DOCUMENT_ROOT']."/Logic/SmartView/Language/".$langFile)) {
include_once $langFile;
} else {
include_once "lang_en.php";
}

How can I limit the parameters of the GET values to the ones in my array?

I have set up a basic script for simple multi-language support.
My problem is that if the user types manually on the url his own GET params it's showing error(of course since it doesn't lead anywhere correctly). For example I have set up an array for 'en' and 'el' but if the user types manually 'de' since this language is not in my array it will lead to an error page of undefined index. I want to redirect the user to index.php with the default language selected.
Here is my index.php
<?php
include('lang.php');
set_lang();
$current = $_SESSION['lang_ses'];
?>
<html>
<body>
<div style="height:100px; background-color:red; color:black;">
ENG
EL
</div>
<div style="height:400px; background-color:gray; color:white;">
<h2><?php echo $langarray[$current]['service1']; ?></h2>
<h2><?php echo $langarray[$current]['service2']; ?></h2>
<h2><?php echo $langarray[$current]['service3']; ?></h2>
</div>
</body>
</html>
Here is my script lang.php
<?php
function set_lang() {
session_start(); // Starting php session
$default = 'en'; // Here you can set with which language the website should start.
if(!isset($_SESSION['lang_ses'])) { // Setting up chosen language or load default language
if(isset($_COOKIE['lang'])) {
$_SESSION['lang_ses'] = $_COOKIE['lang'];
} else {
$_SESSION['lang_ses'] = $default;
}
}
if(isset($_GET['lang'])) {
$_SESSION['lang_ses']=$_GET['lang'];
setcookie('lang',$_GET['lang'],time()+24*3600); // writing cookie
}
}
$langarray = array(
'en'=>array(
'service1'=>'Health',
'service2'=>'Beauty',
'service3'=>'Strength'
),
'el'=>array(
'service1'=>'Υγεία',
'service2'=>'Ομορφιά',
'service3'=>'Δύναμη'
)
);
?>
if (!in_array($_GET['lang'], ["en","el"])) { // PHP 7
header('Location: /index.php');
}
Another way is to set a default value for the $_SESSION['lang_ses'] on top of your script before running set_lang() function.
$_SESSION['lang_ses'] = "en";
set_lang();
use a comparison like this inside your function
if (!in_array($_GET['lang'], ["en","el"])) { // PHP 7
$defaultLangCode = $_SESSION['lang_ses'];
} else {
$defaultLangCode = $_GET['lang'];
}
... continue your script using $defaultLangCode, this way there is no need for redirect. You will have as default language for my.site/de your English page.
check the condition in "lang.php" file follows as
$Arr_languages=array("en","el");
if (!(in_array($_GET['lang'],$Arr_languages))
header('Location: index.php');exit;
else {
//your process;
}

PHP using variable for require location

I believe my problem should be simple, but no matter how much looking I do I cant find a solution :(
I am trying the following....
File structure.....
-Folder 1
-Folder 2
-loader.php
-admin.php
loader.php contents...
<?php // $_GET['l'] == admin.php
$location = $_GET['l'];
require $location;
?>
admin.php contents .....
<?php echo "test"; ?>
But this doesnt work. Anything I search only brings results regarding passing the variable to the loaded file. I'm wanting to use the variable to define the location of the file to require.
Surely this is possible somehow!
<?php // $_GET['l'] == admin.php
$location = $_GET['l'];
require $location;
$location = $_GET['l'];
require '$location';
?>
i have also tried require|include|require_once|include_once

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

Having Difficulty passing variables through includes using globals

I have a three tier tree for displaying content on a page. It uses includes to display specific PHP pages based on the URL. What doesn't happen is the variables are not understood in included PHP files.
index.php
// example url http://fakesite.com/?color=red&user=999
$user = $_GET['user'];
if ($_GET['color'] == 'red')
{$color = 'red';}
elseif ($_GET['color'] == 'white')
{$color = 'white';}
else
{$color = 'blue';}
global $color;
global $user;
include 'page2.php';
page2.php
global $color;
global $user;
echo 'hi '.$user.'I hear you like '.$color;
There is no need at all for those $global lines. Any variables defined in the main script are defined in the included file. It's basically like taking the code in the included file and shoving it in the place of the include call (with a few exceptions)
This line:
include_once 'page2.php;
should change to:
include_once 'page2.php';
You have a missing quote.
Have you tried removing ALL those four global lines? I don't know if that's the problem, but they're not necessary at all!
When including or requiring a file all the variables declared above are available to the included/required file.
If that didn't solve it, maybe you have the wrong path on include.
index.php
<?php
$user = $_GET['user'];
if ($_GET['color'] == 'red')
{$color = 'red';}
elseif ($_GET['color'] == 'white')
{$color = 'white';}
else
{$color = 'blue';}
include 'page2.php';
?>
page2.php
<?php
echo 'hi '.$user.'I hear you like '.$color;
?>
global example
function dosomethingfunky(){
global $user, $color;
echo 'hi '.$user.'I hear you like '.$color;
}

Categories