Forum Moderators: coopster

Message Too Old, No Replies

multilingual website

problem with multilingual website session

         

Roobik

12:29 pm on Dec 25, 2007 (gmt 0)

10+ Year Member



Hi
I find this code for a multilingual web site! but i dont know how i can change the variable in this session for changing languages from a link :( can any one help me?!


<?php
session_start();
if(!isset($lang)) {
$lang = getenv('HTTP_ACCEPT_LANGUAGE');
$lang_default = "fr";
}

if (is_file("lang/index.$lang.php"))
{
$_SESSION["lang"] = $lang;
include("lang/index.$lang.php");
} else {
$_SESSION["lang"] = $lang_default;
include("lang/index.$lang_default.php");
}
?>

jatar_k

1:08 pm on Dec 25, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



from a link you would need to add a bit to that that looks for $_GET['variablename']

I am thinking your setup has register_globals on so you may want to use something different than lang as your varname

maybe 'linklang' and setup a test like so

<?php
session_start();
if (isset($_GET['linklang'])) {
$_SESSION['lang'] = $_GET['linklang'];
}

if(!isset($lang)) {
$lang = getenv('HTTP_ACCEPT_LANGUAGE');
$lang_default = "fr";
}

Roobik

1:17 pm on Dec 25, 2007 (gmt 0)

10+ Year Member



may i don't describe my problem correctly!
I need users change the language by clicking in a link with language name and change the language from English to French!

eelixduppy

1:28 pm on Dec 25, 2007 (gmt 0)



From the example that you give in your first post, you are assuming that you have different index pages for each language labeled appropriately; for example: index.fr.php So if the user clicks the french link, you are just using the french version of the page instead of the english version. This is not a means of actually translating the page on the fly.

Roobik

1:34 pm on Dec 25, 2007 (gmt 0)

10+ Year Member



i have my translated words in files for example for English i put my english words in index.en.php it is not mater i can change the name of files for example from index.fr.php to fr.php

eelixduppy

1:46 pm on Dec 25, 2007 (gmt 0)



I misunderstood your question, then. To do the actual changing with the link, I would do it something like this in the general index.php:

$langs = array('fr','en');
session_start();
if(isset($_GET['l']) && [url=http://www.php.net/in-array]in_array[/url]($_GET['l'], $langs)) {
$_SESSION['lang'] = $_GET['l'];
} else {
$_SESSION['lang'] = 'en';
}
[url=http://www.php.net/header]header[/url]("Location: http://www.example.com/lang/index.".$_SESSION['lang'].".php");
exit;

Now the links on your page would be something along these lines:


<a href="index.php?l=fr">French</a>
<a href="index.php?l=en">English</a>

eelixduppy

1:56 pm on Dec 25, 2007 (gmt 0)



Just a note. It is a much better idea to place your languages in separate directories on the server with the same file names instead of the same directory with different names. For example:

lang/en/index.php
lang/fr/index.php

instead of


lang/index.en.php
lang/index.fr.php

The second method can get sloppy.

Roobik

2:06 pm on Dec 25, 2007 (gmt 0)

10+ Year Member



No! :)

eelixduppy

2:08 pm on Dec 25, 2007 (gmt 0)



I'm not sure I understand your response...

lammert

3:30 am on Dec 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm not sure I understand your response...

I think I understand the response, because I have a comparable language setup on one of my sites. index.XX.php files are automatically selected by the language content negotiation phase when the web browser connects to the server. With this automatic negotiation the larger part of the visitors (80% or more) will already get the language version they need without the need to do an additional language selection. The 20% unsatisfied visitors can select their own language by clicking on a link to another language version.

Content negotiation works with filenames like index.en.php, index.de.php etc, but not with sites where the languages are split in directories like /en/... /de/... etc.

Roobik

6:45 am on Dec 26, 2007 (gmt 0)

10+ Year Member



Hello lammert!
Yes you are right!
But i have problem in link section when users select their own language by clicking on a link to another language version. Now i like to know how link will be look like :( and how i can change languages by link!

Thanks