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).
Site Language Selection
Moderator: General Moderators
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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.
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
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
This is just a stab at it...
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.
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']];
?>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
I placed the following code into my page:
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?
Code: Select all
foreach ($lang_array as $lang => $key) {
echo "<a href=\"?lang=$lang\">$lang</a><br>";
}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?
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Code: Select all
<?php
foreach ($lang_array as $lang => $key) {
echo "<a href=\"?lang=$lang\">$key</a><br>";
}
?>