开发者

How to maintain the previously selected language (I used php arrays and ?lang=) after clicking a link?

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 defa开发者_如何学Pythonult 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?


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.


You will want to learn about storing information in sessions.

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


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.


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜