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
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’
- 30 January 2007 12:03 PM
-
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:


[/EDIT]
If you have any questions just ask.
And BTW: This is PHP5 code.
- 30 January 2007 02:47 PM
-
Author
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.
- 03 February 2007 10:06 AM
-
- Log in or join for free to make a comment.


