Как сделать что-то, чтобы позволить пользователю менять язык на многоязычном веб-сайте, который использует php-массивы? - PullRequest
0 голосов
/ 20 февраля 2010

Я следовал следующему руководству, чтобы создать многоязычную веб-страницу с использованием массивов php: http://bytes.com/topic/php/answers/880915-how-develop-multi-lingual-websites-php

код:

дерево файлов:

locale/
    english.php
    french.php
set_locale.php
index.php

локаль / english.php:

<?php

$locale = array(
    'title' => 'Title in English',
    'h1' => 'The following in in English:',
    'p1' => 'This is a sample text, in English'
);
?>

локаль / french.php:

<?php
$locale = array(
    'title' => 'Titre en français',
    'h1' => 'Le texte suivant en en français:',
    'p1' => 'Il s\'agit d\'un échantillon de texte, en français.'
);
?>

set_locale.php:

<?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;
?>

index.php:

<?php

// Include the "set_locale" script to fetch the locale
// that should be used.
include_once "set_locale.php"

// Print the HTML, using the selected locale.
?>
<html>
    <head>
        <title><?php echo $GLOBALS['L']['title']; ?></title>
    </head>
    <body>
        <h1><?php echo $GLOBALS['L']['h1']; ?></h1>
        <p><?php echo $GLOBALS['L']['p1']; ?></p>
    </body>
</html>

Теперь я хочу сделать что-нибудь, чтобы позволить пользователю щелкнуть ссылку и изменить язык

Примерно так:

<ul id="language-selection">
   <li><a href="something should go here but I'm not sure what">English</a></li>
   <li><a href="ssomething should go here but I'm not sure what">English</a></li>
</ul>

Какой лучший способ сделать это?

Ответы [ 2 ]

1 голос
/ 20 февраля 2010

передать язык как часть строки запроса:

<ul id="language-selection">
   <li><a href="index.php?lang=english">English</a></li>
   <li><a href="index.php?lang=french">English</a></li>
</ul>

В вашем коде вы используете $_GET['lang'], так что это должно его поймать. Вы можете изменить код, если это необходимо.

1 голос
/ 20 февраля 2010
index.php?lang=english 

OR

index.php?lang=french
...