Multilanguage support

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
nitrino
Forum Newbie
Posts: 10
Joined: Tue Jan 02, 2007 10:22 am
Location: Riga, Latvia

Multilanguage support

Post by nitrino »

Hello,

I would like to know your suggestions and theoretical solutions about creating multi-language support in sites based on PHP/MySQL.
It's clear that separating web root for each language is bad solution.

Does anybody know a better, more scalable, and simplier solution? Just theoretically?

Method I use for now:
1) Make a special language include with array, containing all sentences in site for each language:

Code: Select all

$LANG['login-required'] = 'Login required.'
2) Keep the current language in session, and based on that, include a specific file.
3) Display the text by keyword:

Code: Select all

echo $LANG['login-required'];
It downgrades code readability and makes diffucult to modify it, and possibly decreases performance.

Thanks.
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

Code: Select all

//eng.php
DEFINE('_TXTEN',"This is some Text");
//esp.php
DEFINE('_TXTESP',"Esto es un texto");

// index.php
if ($lang=="eng")  // use session, cookies, or $_GET
   get eng.php
else
  get esp.php
end if
nitrino
Forum Newbie
Posts: 10
Joined: Tue Jan 02, 2007 10:22 am
Location: Riga, Latvia

Post by nitrino »

thanks, a DEFINE would speed up a little bit, but usage of constants remains. Maybe is there a completely ANOTHER method for dealing with languges?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

tried gettext?
nitrino
Forum Newbie
Posts: 10
Joined: Tue Jan 02, 2007 10:22 am
Location: Riga, Latvia

Post by nitrino »

seems like it needs php compiled with that support... i'm not sure there are many hostings where it's allowed.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Instead of using constants you could try storing information in a database. Depending on the scale of the translation you could potentially, once loaded store that information in a session for fast access the next time.
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

CoderGoblin wrote:Instead of using constants you could try storing information in a database. Depending on the scale of the translation you could potentially, once loaded store that information in a session for fast access the next time.
I would stick to files. What if you have problems with your db. In what language will you show error message?
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

jmut wrote:I would stick to files. What if you have problems with your db. In what language will you show error message?
Same could be said of files with data corruption 8O. For most sites I have dealt with, the database going down for any reason is a major problem which would need redirection to a "website is undergoing essential maintenance.. We apologize for any inconvenience" page in whatever language, which normally would have to be file based...

We have several different systems already in place using both database and file based translations. The database based translation systems tend to be more flexible having facilities where administrators can update translations on the fly on a page and see the result directly, automatically track translation changes, when and who etc.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

CoderGoblin wrote:Same could be said of files
Yes, but it is less likely to happen with files.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

I think Codergoblin was referring partly to the idea that a failing database is going to close the application anyway. What good are files if the application isn't working to start with?

The database can add flexibility, and you can still cache translations to files if it helps performance (usual benchmarking of filesystem vs database justifying that of course).
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Maugrim_The_Reaper wrote:I think Codergoblin was referring partly to the idea that a failing database is going to close the application anyway.
Yeah I know... but he also meant to what I replied to :P
Maugrim_The_Reaper wrote:The database can add flexibility, and you can still cache translations to files if it helps performance (usual benchmarking of filesystem vs database justifying that of course).
I just want to make sure... You believe that the ultimate solution is to store the data in a database, but deliver it to the output from files which will be updated once in a while with the data in the database, so we gain the performance of the filesystem while not losing the flexibility that we can get with database implementation. Is that what you mean?
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Oren wrote: Yeah I know... but he also meant to what I replied to :P
I admit the DB is more likely to go down than a filesystem, although DB access should be nearly as stable in a production system so the argument what if the DB goes down is a bit of a non-argument for or against... If your database is not stable, in all likelyhood your whole web site is in trouble.
Oren wrote: I just want to make sure... You believe that the ultimate solution is to store the data in a database, but deliver it to the output from files which will be updated once in a while with the data in the database, so we gain the performance of the filesystem while not losing the flexibility that we can get with database implementation. Is that what you mean?
The technique I supplied was as an option to those already listed. I do not claim it is the ultimate solution. As such a possible solution is to get "translations" from the database when a user first loads a page or accesses a "translation category". The information could be stored either in a users session or cached in some other way to be determined during design and benchmark tests. As I also previously stated we have both file based and database translations working and both are equally valid depending on the situation and the requirements.
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

I would start by encapsulating this into a set of objects, that way you can change the storage later and not change all of your code. Also you can use an interface or abstract class and implement one class for each language.

Code: Select all

// This gets pulled from a config, database, or user prefs or something
$selectedLanguage = Language::TYPE_ENGLISH ;

$language = Language::factory($selectedLanguage) ;

echo $language->get('login-required') ;
echo $language->getType() ; // Would echo 'English'
Now your language objects can pull from a database, files, xml, babblefish or anything else and you can change it depending on your mood or the alignment of the planets.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

I must admit - should have seen the Strategy Pattern coming to the rescue...;)
Post Reply