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.
Related
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');
I have a piece of code that should redirect user if there's no cookies in the session..
if (isset($_GET['lang'])) {
if (in_array($_GET['lang'], $jezici)) {
$lang = $_GET['lang'];
// register the session and set the cookie
$_SESSION['lang'] = $lang;
setcookie("lang", $lang, time() + (3600 * 24 * 30));
} else {
$lang = 'hr';
$_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 if (empty($_GET['lang']) || !isset($_GET['lang'])) {
$trenutni_file = basename($_SERVER['PHP_SELF']);
?>
<META http-equiv="refresh" content="0;URL=http://<?php echo $domena; ?>/<?php echo $trenutni_file; ?>?lang=hr">
<?php
exit();
}
?>
This is how I include it in index.php
$langArray = include 'lang/'.$lang.'.php';
But sometimes I get this error failed to include lang/.php ... This code is obviously not doing everything right.. If no cookie is set in the session I would redirect user to index.php?lang=hr... Any help?
Your last condition (empty($_GET['lang']) || !isset($_GET['lang'])) is redundant itself, since empty already checks if the function is set, as you can read here. Also, it is redundant with the first condition.
Moreover, you are lacking a condition to determine if in the case the var is set in session or cookie, it actually is not empty (it could be set as an empty string, for example)
I would change all your isset calls for an empty call, and add a final, standalone condition, to chek if $lang is set, no matter how, and if it has a valid value
if (!empty($_GET['lang'])) {
if (in_array($_GET['lang'], $jezici)) {
$lang = $_GET['lang'];
// register the session and set the cookie
$_SESSION['lang'] = $lang;
setcookie("lang", $lang, time() + (3600 * 24 * 30));
} else {
$lang = 'hr';
$_SESSION['lang'] = $lang;
setcookie("lang", $lang, time() + (3600 * 24 * 30));
}
} else if (!empty($_SESSION['lang'])) {
$lang = $_SESSION['lang'];
} else if (!empty($_COOKIE['lang'])) {
$lang = $_COOKIE['lang'];
}
if (empty($lang) || (!in_array($_GET['lang'], $jezici))) {
$trenutni_file = basename($_SERVER['PHP_SELF']);
?>
<META http-equiv="refresh" content="0;URL=http://<?php echo $domena; ?>/<?php echo $trenutni_file; ?>?lang=hr">
<?php
exit();
}
I am working on a multilingual website, and I have a problem now. When the $_GET['setlang'] is empty, language is set to en_US, even if I had ?setlang=pl opened before (which is working fine - language is changing)
empty($_GET['setlang']) ? $_GET['setlang'] = '' : $_GET['setlang'];
switch ($_GET['setlang']) {
case 'en':
$lang = 'en_US';
putenv('LC_ALL='.$lang);
setlocale(LC_ALL, $lang.'.UTF8');
setcookie('lang', $lang, time()+60*60*24*365);
$_SESSION['lang'] = $lang;
$_config['lang'] = $lang;
break;
case 'pl':
$lang = 'pl_PL';
putenv('LC_ALL='.$lang);
setlocale(LC_ALL, $lang.'.UTF8');
setcookie('lang', $lang, time()+60*60*24*365);
$_SESSION['lang'] = $lang;
$_config['lang'] = $lang;
break;
default:
if((isset($_SESSION['lang']) && $_SESSION['lang'] ='en_US') || (isset($_COOKIE['lang']) && $_COOKIE['lang'] = 'en_US')){
$lang = 'en_US';
putenv('LC_ALL='.$lang);
setlocale(LC_ALL, $lang.'.UTF8');
}
else{
$lang = 'pl_PL';
putenv('LC_ALL='.$lang);
setlocale(LC_ALL, $lang.'.UTF8');
}
$_config['lang'] = $lang;
break;
}
$my_name = 'default';
bindtextdomain($my_name,'./locale');
textdomain($my_name);
I can't get this script to change the language to pl_PL after I run ?setlang=pl action. When this get is empty, cookie value is changing to en_US ;/
How should I check it and avoid this problem?
Also I am working with MVC. Does this code seem to be right or should I put some code into model instead of controller?
if((isset($_SESSION['lang']) && $_SESSION['lang'] =='en_US')
|| (isset($_COOKIE['lang']) && $_COOKIE['lang'] == 'en_US'))
make comparisons with == not = this way you assign 'en_US' whatever the $_SESSION["lang"] or $_COOKIE["lang"] is.
Have you started the session? You cannot set or query session variables if you have not.
Use session_start();
$lang = "";
// Check if lang cookie is set
if(isset($_COOKIE['lang']))
{
$lang = $_COOKIE['lang'];
}
// Cookie is not set
else
{
// Get language GET variable
$set_lang = (empty($_GET['setlang'])) ? '' : $_GET['setlang'];
switch($set_lang)
{
case 'en':
$lang = "en_US";
break;
case 'pl':
$lang = "pl_PL";
break;
default:
$lang = "en_US";
break;
}
// Set language cookie
setcookie('lang', $lang, time()+60*60*24*365);
}
// Set locale
putenv('LC_ALL=' . $lang);
setlocale(LC_ALL, $lang . ".UTF8");
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 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