views:

109

answers:

5

Hello everybody, I'm just looking for some advice. I'm creating a website that offers (at least) 2 languages. The way I'm setting it up is by using XML files for the language, PHP to retrieve the values in the XML nodes. Say you have any XML file, being loaded as follows:

<?php
$lang = "en";
$xmlFile = simplexml_load_file("$lang/main.xml");
?>

Once the file contents are available, I just output each node into an HTML tag like so:

<li><?php echo $xmlFile->navigation->home; ?></li>
which in turn is equal to : <li><a href="#">Home</a></li>
as a nav bar link.

Now, the way in which I'm switching languages is by changing the value of the "$lang" variable, through a "$_POST", like so:

if(isset($_POST['es'])){
$lang = "es";
}elseif(isset($_POST['en'])){
$lang = "en";
}

The value of the "$lang" variable is reset and the new file is loaded, loading as well all the new nodes from the new XML file, hence changing the language.

I'm just wondering if there is another way to reset the "$lang" variable using something else, other than "$_POST" or "$_GET". I don't want to use query string either. I know I could use JavaScript or jQuery to achieve this, but I'd like to make the site not too dependable on JavaScript.

I'd appreciate any ideas or advice.

Thanks

A: 

How about storing it in a cookie?

Ben
I thought about that, and actually tried it, and it works as well, but what I'm trying to accomplish is setting the cookie without using a "$_POST", because if I refresh the page, I get an alert asking me if I want to resend.
jnkrois
ahh, I get it.. just do your normal post, once you've processed it do a redirect to the same page using header('location:') etc.. this will prevent that message from happening.
Ben
I did not think of that, I'll give this a try. As it is right now I'm posting to itself, would you say that it's better to put the script in a separate file, and then just get the value from the cookie in each individual page?
jnkrois
`If fancy features such as JavaScript, cookies, session IDs, frames, DHTML, or Flash keep you from seeing all of your site in a text browser, then search engine spiders may have trouble crawling your site.` from the Google Webmaster guidelines
Col. Shrapnel
I can't imagine that being a problem, so long as a default is provided.
Arda Xi
Yeah and and the other are just out of index.
Col. Shrapnel
Thanks to all for your kind answers. Now I have a wider perspective.
jnkrois
A: 

The most common way would be to use it as part of the url and extract it when a page loads:

http://www.your-site.com/en/somepage

Are you using a framework?

jeroen
Yes, I was afraid, I'd have to do something like that, I'm not using a framework, this is only an experiment, and if I figure it out, I might use it on my site, but I wanted to keep the URLS as clean as possible.
jnkrois
I don't know if it´s a requirement, but you're going to have a very hard time promoting your site with the search engines if the language is not part of the url in some way.
jeroen
You are absolutely right, the majority of the target audience are English speakers, for search engine purposes it would be only one language, the additional language is just in case I get hits from Spanish speaking countries.I agree with you though, thanks
jnkrois
Thanks to all for your kind answers. Now I have a wider perspective.
jnkrois
A: 

The most common way to pass a language identifier is subdomain.

http://en.wikipedia.com/

both subdomains should point to the same directory and actual language can be easily extracted from the HTTP_HOST

and for storing language files the solution is gettext

Col. Shrapnel
Actually, other than Wikimedia's sites it isn't really that common, and wouldn't be a good fit in this case either way. This would be a good idea if the languages were completely different sites, like is the case with Wikipedia. If it's the same site, it's kind of complicated to set up.
Arda Xi
@Arda nothing complicated. Actually path way is more complicated to implement. And there are bunch of sites who use it.
Col. Shrapnel
Path can simply be caught by a RewriteRule. For the domains setup you need to mess with DNS. And again, this is a good idea if en.example.com is a site that is completely different from es.example.com, but in this case it isn't. It's a translation of the same site.
Arda Xi
Thanks to all for your kind answers. Now I have a wider perspective.
jnkrois
+1  A: 

Have you thought about using $_SERVER["HTTP_ACCEPT_LANGUAGE"]? Something like this:

if ($_SERVER["HTTP_ACCEPT_LANGUAGE"]) { 
    $langs = explode(",", $_SERVER["HTTP_ACCEPT_LANGUAGE"]); 
    for ($i = 0; $i < count($langs); $i++) { 
        if ($langs[$i] == "en") { 
            $lang = "en";
            break;
        } 
        elseif($langs[$i] == "es") {
            $lang = "es";
            break;
        }
    } 
}

Of course, a switch statement might fit a bit better here, and there's more ways to say English than only en, but this should work without the user having to do a thing. If they manually change, store it in a cookie as per Ben's answer.

Arda Xi
HTTP_ACCEPT_LANGUAGE is the way to determine **default** language, not to choose among them. and you definitely need to renew your PHP knowledge, it has some changes since last century. register_globals depreciation is an example. And read my comment to Ben's answer
Col. Shrapnel
You're right, I took this from a snippet of my code, it had a definition for $HTTP_ACCEPT_LANGUAGE above it.
Arda Xi
Thanks to all for your kind answers. Now I have a wider perspective.
jnkrois
+1  A: 

I would go for session variable.

At the beginning of your pages you'll have:

 if (!isset($_SESSION['language']))
    $_SESSION['language'] = "en";

Then you'll have some links to change the language

<a href="changelanguage.php?lang=es">Español</a>
<a href="changelanguage.php?lang=fr">Français</a>

Changelanguage.php simply is something like

$language = $_GET['lang'];
// DO SOME CHECK HERE TO ENSURE A CORRECT LANGUAGE HAS BEEN PASSED
// OTHERWISE REVERT TO DEFAULT
$_SESSION['language'] = $language;
header("Location:index.php"); // Or wherever you want to redirect
nico
`If fancy features such as JavaScript, cookies, session IDs, frames, DHTML, or Flash keep you from seeing all of your site in a text browser, then search engine spiders may have trouble crawling your site.` from the Google Webmaster guidelines
Col. Shrapnel
@Col. Shrapnel: That is absolutely right, but... it doesn't say "don't use sessions", it says "if you're using sessions etc. in such a way that it may prevent you to see the content of the page Google may have trouble indexing it". There is no way this is going to happen here because of the checks that if the session is not set it will revert to default (English).
nico
And what about other languages? They don't need to be indexed?
Col. Shrapnel
@Col. Shrapnel: Of course they do need to be indexed. Maybe I'm missing something, but I'm assuming the spider will follow the link to the pages in the other language, why shouldn't it?
nico
Thanks to all for your kind answers. Now I have a wider perspective.
jnkrois