multi language site PHP ,illegal offset string - php

Hello i am facing a problem which is really confusing me when I want to print the strings stored in the array . I have
Warning: Illegal string offset 'TITLE' in C:\xampp\htdocs\site1\index.php on line 443
...
Actually the site is multi language and i want to display diffenet languages upon the desire of the user. What is the origin of this problem ?
<?php
include_once 'common.php';
?>
<form method="get" name="signup" >
<h1 id="head"></head></br>
<h1 ><?php echo $lang['USER_REGISTRATION']; ?></h1>
<h4 id="req">*Donates Required Fields</h4>
<table>
<tr>
<td><?php echo $lang['TITLE']; ?>:*</td>
<td><input type="text" name="title"/></td>
</td>
</tr>
</table>
</form>
<?php
session_start();
header('Cache-control: private'); // IE 6 FIX
if(isset($_GET['lang']))
{
$lang = $_GET['lang'];
// register the session and set the cookie
$_SESSION['lang'] = $lang;
setcookie("lang", $lang, time() + (3600 * 24 * 30));
}
else if(isset($_SESSION['lang']))
{
$lang = $_SESSION['lang'];
}
else if(isSet($_COOKIE['lang']))
{
$lang = $_COOKIE['lang'];
}
else
{
$lang = 'en';
}
switch ($lang) {
case 'en':
$lang_file = 'lang.en.php';
break;
case 'pr':
$lang_file = 'lang.pr.php';
break;
case 'ar':
$lang_file = 'lang.ar.php';
break;
default:
$lang_file = 'lang.en.php';
}
include_once 'languages/'.$lang_file;
?>
$lang['USER_REGISTRATION']="User Registration";
$lang['TITLE']="Title";

This error means that $lang does not have the key and value of TITLE. To check what keys and values an array has use var_dump(). You need to define $lang['TITLE'] before echo'ing.

As the error message clearly says, $lang is a string, not an array. Total shot in the dark here:
Your include_once is not including the file, because you've already included it elsewhere and it's not being included a second time due to the _once. That, or that file does not exist, in which case you need to check your error logs.

$language='ru';
if ($language=='ru') {
$lang['Add']='Добавить';
$lang['Back']='Назад';
$lang['Cancel']='Отмена';
$lang['Date']='Дата';
$lang['Days']='Дней';
$lang['Upload']='Загрузить';
$lang['Value']='Значение';
$lang['Hello']='Привет';
}
/* Functions */
//translation
function __($text){
global $lang;
if (isset($lang[$text])){ return $lang[$text];}
else {return $text;}
};
echo __('Add');

Related

PHP | How do I access/modify a switch statement variable globally?

Perhaps this question is worded wrong, but I am having some trouble with my PHP switch statement below.
My goal is to have access to the $page variable across all files. I have tried to do this with sessions, but could not get it to work. An example of my code is shown below. Could this problem be solved with $_GLOBALS or $_SESSIONS? Please point me in the right direction.
Index.php
<?php
/* Fetch needed files. */
require_once 'app/paths.php';
require_once THEME_LAYOUT_PATH . 'container.phtml';
if(!isset($_GET['page']) || $_GET['page'] == ''){
$page = 'home';
} else {
$page = $_GET['page'];
}
$layoutPath = 'themes/neutron/layout/' . $page . '/' . $page . '.phtml';
switch($page)
{
case 'home':
include $layoutPath;
break;
case 'login':
include $layoutPath;
break;
case 'register':
include $layoutPath;
break;
default:
include 'themes/neutron/layout/404/404.phtml';
}
?>
Container.phtml
<html>
<?php if($page == 'login'){ ?> //Error: $page is undefined
<link rel = "stylesheet" type = "text/css" href = "<?php ASSETS_PATH . 'css/file.css' ?>">
<?php } ?>
</html>
You're requiring container.phtml before you set the $page variable. The files included in your switch statement should all have access to the global $page variable, assuming they aren't wrapped inside a class, function, or other construct.
Edit: Here's what your code would look like with this change:
<?php
if(!isset($_GET['page']) || $_GET['page'] == ''){
$page = 'home';
} else {
$page = $_GET['page'];
}
/* Fetch needed files. */
require_once 'app/paths.php';
require_once THEME_LAYOUT_PATH . 'container.phtml';
$layoutPath = 'themes/neutron/layout/' . $page . '/' . $page . '.phtml';
switch($page)
{
case 'home':
include $layoutPath;
break;
case 'login':
include $layoutPath;
break;
case 'register':
include $layoutPath;
break;
default:
include 'themes/neutron/layout/404/404.phtml';
}
?>

PHP - Changing session value to change the language

I am trying to change the session to a value of "en" or "no" by clicking on a link so that the correct language is being displayed on a website.
I have 2 variables, $langNO and $langEN. These are variables containing keys and values of each language.
I am using this:
if (!isset($_SESSION['lang'])) {$_SESSION['lang'] = "en";}
if ($_SESSION['lang'] === "no") {$lang = $langNO;}
if ($_SESSION['lang'] === "en") {$lang = $langEN;}
$lang is related to the strings in the text.
Example:
<h1><?php echo $lang['heading']; ?></h1>
To change the session value, I use this in changelang.php:
session_start();
$_SESSION['lang'] = $_GET['lang'];
header("Location:index.php");
When I click on NO to change the language, it just stays on the default language.
I would love to get some feedback for how this can be solved.
Thank you!
I just changed the index code a little bit
index.php
<?php
session_start();
if (!isset($_SESSION['lang'])) {
$lang = "en";
} else {
$lang = $_SESSION['lang'];
}
switch ($_SESSION['lang']) {
case "en":
$lang = $langEN;
break;
case "no":
$lang = $langNO;
break;
}
?>
NO
changelang.php
<?php
session_start();
$_SESSION['lang'] = $_GET['lang'];
header("Location:index.php");
?>
changelang.php
Set the header() in a function
function redirect(url) {
header('Location: '. url);
}
session_start()
$_SESSION['lang'] = $_GET['lang'];
redirect('index.php');

Converting multi-language function

This is my first ask here so don't judge me if i posted it wrong.
I have this function for my multi-lang support
<?
header('Cache-control: private'); // IE 6 FIX
if(isSet($_GET['lang']))
{
$lang = $_GET['lang'];
// register the session and set the cookie
$_SESSION['lang'] = $lang;
setcookie('lang', $lang, time() + (3600 * 24 * 30));
}
else if(isSet($_SESSION['lang']))
{
$lang = $_SESSION['lang'];
}
else if(isSet($_COOKIE['lang']))
{
$lang = $_COOKIE['lang'];
}
else
{
$lang = 'en';
}
switch ($lang) {
case 'bg':
$lang_file = 'bg.php';
break;
default:
$lang_file = 'en.php';
}
define('IN_VALID',true);
include_once '_src/lang/'.$lang_file;
?>
It gets languages by &lang=en or &lang=bg .
How i can convert it to get &lang:en instead of &lang=en (if it's possible)
?key1=value1&key2=value2 is standard application/x-www-form-urlencoded format.
To use your format, you should parse it on your own, route that I don't advise.
You can check for isset($_GET["lang:en"]).
But this is not to advice. = is the standard separator for keys and values. There's no need to change it.

PHP Redirect by Browser Language not working!

I change languages using php arrays which makes the URL look like this:
http://alexchen.zxq.net/index.php?lang=es
I added $lang=$_SERVER['HTTP_ACCEPT_LANGUAGE']; in my 'controller' file in default sections, and I choose another language in the preferred languages option (Mozilla Firefox),
but it didn't work.
common.php:
<?php
session_start();
header('Cache-control: private'); // IE 6 FIX
if(isSet($_GET['lang'])) {
$lang = $_GET['lang'];
// register the session and set the cookie
$_SESSION['lang'] = $lang;
setcookie("lang", $lang, time() + (3600 * 24 * 30));
}
else if(isSet($_SESSION['lang'])) {
$lang = $_SESSION['lang'];
}
else if(isSet($_COOKIE['lang'])) {
$lang = $_COOKIE['lang'];
}
else {
//$lang = 'en'; <-this was previous code
//I tried this:
$lang=$_SERVER['HTTP_ACCEPT_LANGUAGE'];
}
// use appropiate lang.xx.php file according to the value of the $lang
switch ($lang) {
case 'en':
$lang_file = 'lang.en.php';
break;
case 'es':
$lang_file = 'lang.es.php';
break;
case 'tw':
$lang_file = 'lang.tw.php';
break;
case 'cn':
$lang_file = 'lang.cn.php';
break;
default:
//$lang_file = 'lang.en.php'; <-this was before
//I also tried this:
$lang=$_SERVER['HTTP_ACCEPT_LANGUAGE'];
}
//translation helper function
function l($translation) {
global $lang;
return $lang[$translation];
}
include_once 'languages/'.$lang_file;
?>
Any suggestions?
$_SERVER['HTTP_ACCEPT_LANGUAGE'] is more complex than just a 2-letter ISO code. Check out this question for a good answer on how to parse HTTP_ACCEPT_LANGUAGE in PHP.

How can I display a language according to the user's browser's language inside this code?

How can I display a language according to the user's browser's language inside this mini-framework for my multilingual website?
Basically, it has to display the default language of the user if there's no cookies.
Example of index.php: (rendered output)
<h2><?php echo l('tagline_h2'); ?></h2>
common.php: (controller of which language to output)
<?php
session_start();
header('Cache-control: private'); // IE 6 FIX
if(isSet($_GET['lang']))
{
$lang = $_GET['lang'];
// register the session and set the cookie
$_SESSION['lang'] = $lang;
setcookie("lang", $lang, time() + (3600 * 24 * 30));
}
else if(isSet($_SESSION['lang']))
{
$lang = $_SESSION['lang'];
}
else if(isSet($_COOKIE['lang']))
{
$lang = $_COOKIE['lang'];
}
else
{
$lang = 'en';
}
//use appropiate lang.xx.php file according to the value of the $lang
switch ($lang) {
case 'en':
$lang_file = 'lang.en.php';
break;
case 'es':
$lang_file = 'lang.es.php';
break;
case 'tw':
$lang_file = 'lang.tw.php';
break;
case 'cn':
$lang_file = 'lang.cn.php';
break;
default:
$lang_file = 'lang.en.php';
}
//translation helper function
function l($translation) {
global $lang;
return $lang[$translation]; }
include_once 'languages/'.$lang_file;
?>
Example of /languages/lang.en.php: (where multilingual content is being stored)
<?php
$lang = array(
'tagline_h2' => '...',
I think you are looking for this
Here is very good class for checking and even getting best match against supported languages:
http://snippets.dzone.com/posts/show/6539

Categories