473,320 Members | 1,988 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to develop multi lingual websites in PHP

HI all,
I want to develp a multi lingual website in PHP.(english and french)
I am a novice developer in this aspect.
Pls suggest us what are the methods used in general.
Do we need any extra s/w.
Feb 3 '10 #1

✓ answered by Atli

Hey.

This can be as simple as:
  1. Define an array of values in different languages, preferably each language having it's own array defined in a separate file.
  2. Create a script that figures out which language to use (by, for example, reading session, cookie or get/post values) and includes the proper file.
  3. Include said script into your pages, using the array defined in the language files to echo the text in those pages.

Consider, if you had these files:
Expand|Select|Wrap|Line Numbers
  1. locale/
  2.     english.php
  3.     french.php
  4. set_locale.php
  5. index.php
You could define each of the "locale" files as such:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. /*
  3.  * File: locale/english.php
  4.  */
  5. $locale = array(
  6.     'title' => 'Title in English',
  7.     'h1' => 'The following in in English:',
  8.     'p1' => 'This is a sample text, in English'
  9. );
  10. ?>
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. /*
  3.  * File: locale/french.php
  4.  */
  5. $locale = array(
  6.     'title' => 'Titre en français',
  7.     'h1' => 'Le texte suivant en en français:',
  8.     'p1' => 'Il s\'agit d\'un échantillon de texte, en français.'
  9. );
  10. ?>
Then, in your "set_locale.php" file, you could select a language based on some input variable, like say; a variable named "lang" from the GET array, and include one of those files based on that value:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. /*
  3.  * File: set_locale.php
  4.  */
  5.  
  6. // Get the language from the query string, or set a default.
  7. ($language = @$_GET['lang']) or $language = 'english';
  8.  
  9. // Set up a list of possible values, and make sure the
  10. // selected language is valid.
  11. $allowed_locales = array('english', 'french');
  12. if(!in_array($language, $allowed_locales)) {
  13.     $language = 'english'; // Set default if it is invalid.
  14. }
  15.  
  16. // Inlclude the selected language
  17. include "locale/{$language}.php";
  18.  
  19. // Make it global, so it is accessible everywhere in the code.
  20. $GLOBALS['L'] = $locale;
  21. ?>
Then, in your pages (the index.php page, in this example), include the "set_locale.php" script and use the $GLOBALS['L'] array to set language specific phrases in the HTML:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. /*
  3.  * File: index.php
  4.  */
  5.  
  6. // Include the "set_locale" script to fetch the locale
  7. // that should be used.
  8. include_once "set_locale.php"
  9.  
  10. // Print the HTML, using the selected locale.
  11. ?>
  12. <html>
  13.     <head>
  14.         <title><?php echo $GLOBALS['L']['title']; ?></title>
  15.     </head>
  16.     <body>
  17.         <h1><?php echo $GLOBALS['L']['h1']; ?></h1>
  18.         <p><?php echo $GLOBALS['L']['p1']; ?></p>
  19.     </body>
  20. </html>
See what I mean?

6 4468
Dormilich
8,658 Expert Mod 8TB
basicly you do it like a monolingual website, where you can decide, whether to put the english or french content in (e.g. by using templates)
Feb 3 '10 #2
Atli
5,058 Expert 4TB
Hey.

This can be as simple as:
  1. Define an array of values in different languages, preferably each language having it's own array defined in a separate file.
  2. Create a script that figures out which language to use (by, for example, reading session, cookie or get/post values) and includes the proper file.
  3. Include said script into your pages, using the array defined in the language files to echo the text in those pages.

Consider, if you had these files:
Expand|Select|Wrap|Line Numbers
  1. locale/
  2.     english.php
  3.     french.php
  4. set_locale.php
  5. index.php
You could define each of the "locale" files as such:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. /*
  3.  * File: locale/english.php
  4.  */
  5. $locale = array(
  6.     'title' => 'Title in English',
  7.     'h1' => 'The following in in English:',
  8.     'p1' => 'This is a sample text, in English'
  9. );
  10. ?>
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. /*
  3.  * File: locale/french.php
  4.  */
  5. $locale = array(
  6.     'title' => 'Titre en français',
  7.     'h1' => 'Le texte suivant en en français:',
  8.     'p1' => 'Il s\'agit d\'un échantillon de texte, en français.'
  9. );
  10. ?>
Then, in your "set_locale.php" file, you could select a language based on some input variable, like say; a variable named "lang" from the GET array, and include one of those files based on that value:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. /*
  3.  * File: set_locale.php
  4.  */
  5.  
  6. // Get the language from the query string, or set a default.
  7. ($language = @$_GET['lang']) or $language = 'english';
  8.  
  9. // Set up a list of possible values, and make sure the
  10. // selected language is valid.
  11. $allowed_locales = array('english', 'french');
  12. if(!in_array($language, $allowed_locales)) {
  13.     $language = 'english'; // Set default if it is invalid.
  14. }
  15.  
  16. // Inlclude the selected language
  17. include "locale/{$language}.php";
  18.  
  19. // Make it global, so it is accessible everywhere in the code.
  20. $GLOBALS['L'] = $locale;
  21. ?>
Then, in your pages (the index.php page, in this example), include the "set_locale.php" script and use the $GLOBALS['L'] array to set language specific phrases in the HTML:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. /*
  3.  * File: index.php
  4.  */
  5.  
  6. // Include the "set_locale" script to fetch the locale
  7. // that should be used.
  8. include_once "set_locale.php"
  9.  
  10. // Print the HTML, using the selected locale.
  11. ?>
  12. <html>
  13.     <head>
  14.         <title><?php echo $GLOBALS['L']['title']; ?></title>
  15.     </head>
  16.     <body>
  17.         <h1><?php echo $GLOBALS['L']['h1']; ?></h1>
  18.         <p><?php echo $GLOBALS['L']['p1']; ?></p>
  19.     </body>
  20. </html>
See what I mean?
Feb 3 '10 #3
kovik
1,044 Expert 1GB
Or you could just tell your users to learn how to use Google Translate. ;)
Feb 4 '10 #4
thanks for your reply
Feb 6 '10 #5
thanks for your valuable code
Feb 6 '10 #6
Given the code, how would you go about coding an <a></a> tag with alternating classes?

For example when the home page is selected, the home link looks:
<a href="index.php?lang=english" class="selected">Home</a>

but if I'm on the contact us page for example, I want the home link to be (without the class="selected"):
<a href="index.php?lang=english">Home</a>
Aug 24 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: dc | last post by:
Product Announcement: Multi-Lingual web site using Nested Templates and Menus http://www.decloak.com/Products/Dreamweaver/NestedTemplates/ You can see the TONGUES of FIRE demo web site at...
115
by: J | last post by:
I've run CSSCheck on my style sheets and I always get a warning similar to this: "font: bold 9pt/100% sans-serif Warning: Absolute length units should not generally be used on the Web ..." ...
2
by: Z D | last post by:
Hello, I need to support multiple languages in a product package being developed in both ASP.NET (vb.net) and WinForm .net (c#). I was wondering if there are any best practice guides,...
1
by: Ahmad Jalil Qarshi | last post by:
hi! i want to develop an application in VC7 which will have multilingual support. I m new to VC7. plz help me to find some useful link or something else tht may be helpful. thanx in advance....
0
by: THY | last post by:
anyone know a good way to create a multi-lingual website in ASP.Net ? thanks
7
by: Bart Schelkens | last post by:
Hi, what is the best way (or the most interesting) way to make my application multi-lingual? I have french, dutch and english users and they all want the menu's and the labels,... in their...
1
by: Cédric | last post by:
Hello, wanting to have a multi-lingual windows form application. Does anyone know, how to do, or where articles on that topic could be found. Thanks
2
by: Dylan Parry | last post by:
Hi folks, I was wondering if anyone here has experience of making Web applications that are multi-lingual? The solution that I have in my head is to have several "language" files as resources so...
2
by: Aussie Rules | last post by:
Hi, I have a site that Iwant to either display my text in english or french, based on the users prefernces ? I am new to webforms, but I know in winforms, this is pretty easy with a resource...
1
by: Jonathan | last post by:
Before I start creating a new asp.net website, is there a standard way of making it multi-lingual? For example, rather than type in body text to a page, should I rather refer by number code to a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.