Ultrashock Forums > Development > Server Side
[PHP] Multi language

You are currently viewing our website as a guest which gives you limited access to forums, files and other resources.

Click here to join now for free, and start interacting with our members, download files and much more!

Click here if you are looking for our Flash files and other professional assets.
 
Post Reply | View first unread | Rate Thread Search this Thread | Thread Tools | Display Modes

#1
Bookmark and Share!
[PHP] Multi language
Old 2007-01-29

I bet this one's gonna hit you hard! Not.

I've been asked by a client to make a site in three languages. Now, I've never tackled such a task. And I'm pretty sure some of you actually have. So I beg you (*lol*) to share your wisdom with this humble coder. I've been spitting out ideas like using a database table for strings used around the site, and echoing them according to the language, which I may store in a cookie or the session. Then I've been thinking of plain text files, using some king of separator and containing those strings in some order. Both of these will require a strict coordination of string identifiers and their positions on the site. And both will give me the ability to easily outsource the translations if I need so (I speak the 3 languages fluently, but I might be short of time...)

Now please tell me your thoughts. I promise I'll even be around to answer
Chris Hoeppner
Passionate about experience. Faithful about copy.

Freelance coding service for designers. You design. I code. Drop me a pm, and get us talking.
postbit arrow 7 comments | 237 views postbit arrow Reply: with Quote   
Registered User
Pixware is offline
seperator
Posts: 1,457
2003-10-16
Age: 24
seperator

Ultrashock Member Comments:
Pixware Pixware is offline 2007-01-30 #2 Old  
I'm pretty sure there's someone here knowing more than me on this topic. Anyone to share his/her knowledge?
Reply With Quote  
poppa's Avatar poppa poppa is offline Moderator poppa lives in Sweden 2007-01-30 #3 Old  
If you're running a Linux server you can checkout gettext (I don't think it works on Windows, but I'm not sure).

I've done a more simple solution that's platform independent and I can give you an example later when I get back from work.

Cheers
Reply With Quote  
Pixware Pixware is offline 2007-01-30 #4 Old  
I'd really dig that, poppa. You're the man
Reply With Quote  
petremetre's Avatar petremetre petremetre is offline petremetre lives in Greece 2007-01-30 #5 Old  
I use to create a .php file where i define all my string variables and use these within my pages instead of text.
If text is stored in a database i create multiple table and access them accordingly by a table prefix i.e. gr_news, eng_news, de_news etc.

Cheers
'A'
Reply With Quote  
poppa's Avatar poppa poppa is offline Moderator poppa lives in Sweden 2007-01-30 #6 Old  
Last edited by poppa : 2007-01-30 at 14:34.
Okey, here we go:

The code is popped out of a project I'm working on but I think I got it pretty generic.

You'll see that there are some <langcode>.lang.php files in there and these are the files with the actual translations. Note, that you can very well populate the array in these files with content from a DB. They are just regular PHP files with a single array and how you populate these arrays is up to you.

At the moment the code decides what language to use based on the primary language set in the user's browser. But there would be no problem implementing the ability to swap language manually.

Last in the Locale.php you'll see a function called getLocale which returns the Locale object. The function assume you name your locale object $LOCALE but if you name it something else just change that function accordingly.

You'll find the code here, and there's an example implementation. If you use Firefox you can add "Swedish" to languages and set it as default to see the example live.

[EDIT]
Here are two screen dumps, one where I have Swedish as default language and one where I have English:

http://www.poppa.se/us/locale-sv.png
http://www.poppa.se/us/locale-en.png
[/EDIT]

If you have any questions just ask.
And BTW: This is PHP5 code.
Reply With Quote  
Pixware Pixware is offline 2007-02-01 #7 Old  
I have been looking into gettext since your last post. Seems like neat stuff. I'm not really for the idea of using arrays of strings and of the kind.
I'll have a look at your solution before deciding. Thanks poppa!
Reply With Quote  
Pixware Pixware is offline 2007-02-03 #8 Old  
I've been thinking on this subject for two days now. I've decided, I'm going with a mixed solution. On one side, my static content is going to be stored in arrays. It's a fairly low amount of strings, so it should be fairly maintainable. If the amount ever increases I can always switch over to gettext, which I have been unable to get to work with CakePHP. On the other side, a big part of the site's content is editable through a hand crafted cms, and the client needs to enter most of this information in several languages. This leads me to store these strings in a translations table in my database. However, I'm still unsure about the exact structure of my data. Consider this a rant.

This site is getting interesting. I'll be writing a little guide for my upcoming weblog.
Reply With Quote  
Thread Tools
Display Modes Rate This Thread
Rate This Thread: