Page 1 of 1

Site Language Selection

Posted: Tue Jul 04, 2006 7:17 pm
by hello world
Hello,

I want my site to support multiple languages (English, French, German, Chinese, and etc.)

I am planning to create a language file for each language at ...

/language/en.lang
/language/fr.lang
/language/jp.lang

How do I allow visitors to select a language to display the site in?

English is the default. When someone clicks on French (French flag or "French"), the site will read the /language/fr.lang file and display the site in French. I want the language to stay intact while visitor goes through each page, unless the visitor selects a different language.

---------------
The above is enough for me for now. However, if you are able to help me go even futhur, it would be great.

Default language by IP:
I want to use Maxmind GeoIP to locate the visitor and display the default language by IP. For instance, if the user is from China (by IP), then the default site language is Chinese (not English). If the visitor is from France (by IP), the default site language is French (not English).

Posted: Tue Jul 04, 2006 8:00 pm
by RobertGonzalez
You can do one of a few things. The most common is to store the language preference in the users profile and when they visit and login, the site changes languages based on their selected language in their profile.

If you want even unknown visitors to be able to pick one, then you should set up a cookie to store the language file reference in, then if they choose a different language, reset the cookie.

Picking a default language based on location can be tricky if not completely unreliable. IPs are not the best means of determining location (searching these forums will explain it better than I can here). You may want a site default language and let the users pick, rather than trying to di magically.

Posted: Tue Jul 04, 2006 9:54 pm
by hello world
Mine doesn't have the login area nor membership where people signup. It is a couple of php pages for people to read. I just want to translate the text in several different languages.

Do you have the code for me to put in the header for each php page?

Posted: Wed Jul 05, 2006 12:56 am
by RobertGonzalez
This is just a stab at it...

Code: Select all

<?php
$lang_array = array(
    'en' => 'language/english.lang.php',
    'fr' => 'language/french.lang.php',
    'jp' => 'language/japanese.lang.php');

$site_lang = 'en';

if (isset($_GET['lang']))
{
    if (array_key_exists($_GET['lang'], $lang_array))
    {
        $site_lang = $_GET['lang'];
    }
}

setcookie('lang', $site_lang, 0); //this adds a new cookie to store the lang choice

// Include the language file
include $lang_array[$_COOKIE['lang']];
?>
To run the links, use a foreach loop on the $lang_array and echo out a link with ?lang=$key in the loop.

PS I didn't test this code. Try it and tweak to make it work for yourself. Hope it helps.

Posted: Thu Jul 06, 2006 12:20 am
by hello world
I placed the following code into my page:

Code: Select all

foreach ($lang_array as $lang => $key) {
    echo "<a href=\"?lang=$lang\">$lang</a><br>";
}
The output on the page:

en
fr
jp


When I click on "en", the url has this extention: test.php?lang=en
When I click on "fr", the url has this extention: test.php?lang=fr
When I click on "jp", the url has this extention: test.php?lang=jp

I am pretty new to php.

If I want it to read language/english.lang.php or language/french.lang.php or language/japanese.lang.php file, what do I need to do?

Posted: Thu Jul 06, 2006 1:02 am
by RobertGonzalez

Code: Select all

<?php
foreach ($lang_array as $lang => $key) {
    echo "<a href=\"?lang=$lang\">$key</a><br>";
}
?>