Site Language Selection

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hello world
Forum Newbie
Posts: 3
Joined: Tue Jul 04, 2006 7:03 pm

Site Language Selection

Post 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).
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
hello world
Forum Newbie
Posts: 3
Joined: Tue Jul 04, 2006 7:03 pm

Post 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?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
hello world
Forum Newbie
Posts: 3
Joined: Tue Jul 04, 2006 7:03 pm

Post 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?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Code: Select all

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