Multi-lingual page method

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
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Multi-lingual page method

Post by anjanesh »

Hi

Im trying to make my page multi-lingual. But I dont want a separate page for each language - that would require too many pages and same updates.

Instead I have a table that has fields like StringNo, English, Lang1, Lang2, Lang3.
The other 3 fields are not Lang1, Lang2 or Lang3 - I just replaced the actual field names.

Somewhere in the beginning of the page, I have

Code: Select all

$Language_Words = GetLanguageStrings(
$Language,
array(
      'Search','News','Enter your name','Something else','etc'
      )
);
This will return the language equivalent of the English String as an array.
I then do

Code: Select all

<tr><td><? echo $Language_Words['News']; ?></td></tr>
This is how my current page is like and is easier because all are in a single page.
But will this be very resource intensive ? Is this efficient ?
How are sites multi-lingual ?

Thanks
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

One common way of making a multi-lingual site is to use constants. For example, on the front end display code you'd use:

Code: Select all

<?php
echo NAME.":<br>";
echo TELEPHONE.":<br>";
echo EMAIL.":<br>";
?>
Then you'd have a series of include files that define the words in each language. For example, english.inc.php might be:

Code: Select all

<?php
define("NAME","Name");
define("TELEPHONE","Telephone");
define("EMAIL","Email");
?>
french.inc.php might be:

Code: Select all

<?php
define("NAME","Nom");
define("TELEPHONE","La Telephone");
define("EMAIL","La Email");
?>
Then, to change language you'd just include() the correct language file.

PS. I can't speak French.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Yes. This is how phpMyAdmin does it too.
But wont defining all contants in a php file take too much memory ?
I know that loading all the values from a MySQL db will take more queries (resources) but I would need to load just the required ones - not all of them.

Thanks.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

what i did was make the array like you did

Code: Select all

$words = array
(
  'HELLO' => array
  (
    'eng' => 'Hello',
    'est' => 'Tere',
  ),
  'BYE' => array
  (
    'eng' => 'Bye',
    'est' => 'Hea Aiga',
  ),
)

function dol($word)
{
  $word = $this->words[$word][$_SESSION['lang']];
  return $word;
}
//this was a class blah blah blah

//then to get the word i did 
$lang->dol('HELLO');
that might not have been the best way of doing it but it proved to not take up many resources because there really wasnt much to it other than an array and it was easy to add things and easy to see what i was doing
theda
Forum Contributor
Posts: 332
Joined: Sat Feb 19, 2005 8:35 am
Location: USA

Post by theda »

Or you could be really lazy about it and just make a second page entirely for each page of yours ^_^;; That's what I use to do. Now I do it in an even lamer way... I do the same thing as before, but on the same file using switch(). but most of my content is static and isn't in the form of a forum or sorts, so I don't plan on changing from my current method.

My method explaineD:

Code: Select all

<?
switch($language) {
  default:
  case "en": print "Good day, how are you?"; break;
  case "de": print "Guten tag, wie gehts?"; break
}
?>
Post Reply