How do I create an efficient multi-language site?

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
Jordban
Forum Newbie
Posts: 6
Joined: Thu Feb 28, 2008 1:42 am

How do I create an efficient multi-language site?

Post by Jordban »

(I tried searching the forums)

I'm trying to create a multi-language supported site as efficiently as possible.

As of now a user specifies their language on registration and then after logging in a variable is set.

Code: Select all

$iLang = mysql_result($userinfo,0,'language');
Then the phrases.php file is included, which does the following:

Code: Select all

<?PHP
 
switch ($iLang) {
 
// ----------------------- ENGLISH ----------------------- 
default:
 
// BASIC SITE INFORMATION
$LangSiteName = "Site Name";
$LangSiteUrl = "http://www.site.com";
$LangAutomatedEmail = "Forum@Site.com";
$LangSiteTitle = "Site.com";
 
// TOP BAR TEXT
$LangWelcome = "Welcome";
$LangGuest = "Guest";
$LangLogOut = "Log out";
$LangCreateAccount = "Create an account";
$LangPrivateMessages = "Private Messages";
$LangGold = "Gold";
You get the idea.

I know there are a few problems with this:

1) All languages are loaded, when you only need one. Solution: Create a different PHP file for each language containing the translations?
2) All text of x language is loaded, when only some of the text is needed. For example I don't need the phrases for the registration page if i'm on the login page. Solution: Specify your page and load phrases by page individually.

Now this is just me brainstorming. I'd love to hear your advice, and your thoughts on this subject.

Thank you!

Jordan Berry

PS. First post, Hi! :roll:
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Re: How do I create an efficient multi-language site?

Post by shiznatix »

To make a good multi-language site you must sacrifice a virgin on the altar of Hermes. Then realize that everyone has a different way of doing this but here is my way which works well:

I have a folder for each page (index, faq, contact us, etc...) and in this folder I hold the translations for each language. These files are just a list of constants that I use as my variables. For each language I have a different file (view.en.php, view.ee.php, view.pt.php, etc...) which has the same constants but of course has a different translation in each file. This makes it easy to snag the file based on the users preference without doing any extra work.

Since my company is going for many languages for our site since out of the 5 people working here there are 5 languages able to be spoken. To keep all the files with the same constants in them to avoid massive confusion and possible missing constant errors, we only edit the English translation file and when I do an update from the test site to the live site I run a script that automatically changes the other language files to either add or delete a phrase based on the English file.

As for translating, I have a system setup for the admins on our site that shows a link "translate this page" on every page they go to. When they click this a popup comes up and allows you to edit and of the translation files in any of the languages available through just a simple form. This makes editing these php files easy for non-tech people.

And thats how I do it.
Jordban
Forum Newbie
Posts: 6
Joined: Thu Feb 28, 2008 1:42 am

Re: How do I create an efficient multi-language site?

Post by Jordban »

Thank you sir. That sounds like a pretty logical and efficient method.

I have another question about this but in order to help out the rest of the site I think i'll post a second thread.
Post Reply