tags:

views:

27

answers:

4

I have the following code:

  <html>
        <head>
            <title><?php echo $GLOBALS['L']['title']; ?></title>
        </head>
        <body>
            <ul id="language-selection">
                <li><a href="index.php?lang=english">English</a></li>
                <li><a href="index.php?lang=french">French</a></li>
            </ul>
            <h1><?php echo $GLOBALS['L']['h1']; ?></h1>
            <p><?php echo $GLOBALS['L']['p1']; ?></p>
            <ul id="language-selection">
                <li><a href="about.php">About Page</a></li>
                <li><a href="contact.php">Contact Page</a></li>
            </ul>
        </body>
    </html>

set_locale.php:

<?php
/*
 * File: set_locale.php
 */

// Get the language from the query string, or set a default.
($language = @$_GET['lang']) or $language = 'english';

// Set up a list of possible values, and make sure the
// selected language is valid.
$allowed_locales = array('english', 'french');
if(!in_array($language, $allowed_locales)) 
    $language = 'english'; // Set default if it is invalid.


// Inlclude the selected language
include "locale/$language.php";

// Make it global, so it is accessible everywhere in the code.
$GLOBALS['L'] = $locale;
?>

It works OK, but if I click the about.php and contact.php link. The page returns to the default language: English. What can I do so that when I click about.php or contact.php ends up like this:

about.php?lang=english
contact.php?lang=french

respectively, in other words I want the URL to remember the ?lang= ending. What's the best way of doing it?

A: 

You will want to learn about storing information in sessions.

http://learnitscreencasts.net/2009/07/18/beginners/beginners-guide-php-sessions/

Moak
Sessions won't work because I could well have a tab open with the english, and another with the french version.
Pekka
+3  A: 

You'll have to append it to every outgoing link:

 <li><a href="about.php<?php echo "?lang=".$GLOBALS['L']; ?>">About Page</a></li>

a nice way of dealing with multi-language sites in general is, if your server supports it, mod_rewrite to rewrite "virtual" URLs like

www.example.com/en/about.php

and map them internally to

www.example.com/about.php?lang=en

there's a beginner's guide on that here and official documentation here.

I'm no mod_rewrite guru but this works for me:

 RewriteEngine on
 Options +FollowSymlinks

 RewriteCond %{REQUEST_URI} ^/([a-z][a-z])(/.*)?$      
 RewriteRule (.*) %2?lang=%1&%{QUERY_STRING}

it maps

  • www.domain.com/en/about.php to /about.php?lang=en

  • www.domain.com/fr/about.php to /about.php?lang=fr

  • www.domain.com/es/ to /?lang=es = usually index.php

It maps any occurrence of a two-letter, lowercase www.example.com/xy, so you shouldn't have any directories with two letters on your root level to work with this.

Pekka
+1 Yes, my server have it enabled. Thanks, but how do I apply the tutorial to my example?
janoChen
I tried the first example and the URL ended up like this: http://localhost/...contact.php?lang=ArrayWhat happened?
janoChen
@jano hang on, I'll write an example.
Pekka
+1 Always keep your language in the URL. You can use an automatic redirect from `/` to choose a default language based on the `Accept-Language` header or account settings, but after that there should generally be a different URL for each language version of a page, so that users can navigate between languages as necessary, and search engines can index each language.
bobince
You don't even have to use rewriting to achieve this; you can use a simple `Alias` to point each language to the same set of PHP files, and then have the script sniff `$_SERVER['SCRIPT_NAME']` to choose which language to display.
bobince
@bobince good point, but I have often had trouble using `Alias` on shared hosting in the past, while rewrite worked like a charm. Anyway, care to write down an `Alias` example for completeness? @jano I added a working mod_rewrite example.
Pekka
Thanks Pekka I'll try it out. DO I have to create a .htaccess file and upload it to the root of my server?
janoChen
@jano yes you do.
Pekka
@jano what happens when you enter `domain.com/en/about.php`?
Pekka
@jano there is only one file in play. mod_rewrite creates *virtual* URLs that are mapped to your php files. There is no need to change anything in your files except take into account that the browser thinks you are in a directory (`/en/`) while really you are not.
Pekka
this is the file tree of my files:D:\wamp\www\tests\php-multilingual-arrays\locale\en.phpD:\wamp\www\tests\php-multilingual-arrays\locale\fr.phpDoes this affect your example?(I can't enter the /en/about.php because it doesn't exists)
janoChen
@jano You need to update the mod_rewrite example to reflect your directory structure (add /tests/php-..... everywhere) or move the whole thing down to the root level.
Pekka
I moved everything to my root directory but when I click about.php, it still coming back to English (contact.php?lang=Array so it calls the default). Never mind I used the Session and Cookie technique and arrays from a previous question of mine (http://stackoverflow.com/questions/2298372/my-multilingual-website-with-only-basic-php-without-zend-translate-gettext-etc) and it seems to works. Thanks anyway.
janoChen
@jano when you call `/fr/about.php`, all links inside it (the ?lang= part is not necessary in that case) will automatically point to the french version. That's the beauty of it - the browser is tricked into thinking it's in the /fr directory while in reality, all calls go to just one `about.php`. This works, and is definitely better than sessions - worth trying again.
Pekka
A: 

You might want to look into sessions and store the persistent options there. It has the advantage of allowing people to copy links to others without forcing their settings upon them, if you so desire such.

Amber
A: 

You could use the output_add_rewrite_var to add that argument to the URLs. Just call the following before you output your contents:

output_add_rewrite_var('lang', $language);
Gumbo