Page 1 of 1

Localization with PHP

Posted: Tue Jan 12, 2010 12:34 pm
by pagod
Hi everyone,

I'm writing a web application (a kind of search engine) which is localized in 4 languages (for now, there will be more). As this is the first time I'm doing this, I'm not too sure about what would be the best way to store all the localized text.
Right now, I have a file "localization.php", which defines a large array where the keys are identifiers for the different pieces of text I'm displaying, and values are again arrays where the keys are the language abbreviations (e.g. "de-DE", "en-GB" etc) and the values are the phrases themselves. Various PHP files that need to display text include this file and access to the phrases via a function that gets the current interface language from the current $_SESSION and fetches the phrase in that language for the key passed as argument. So basically, displaying a bit of text amounts to calling e.g.

Code: Select all

 
<title><?= get_localized_text( "title" ) ?></title>
 
This works fine, however that means, if I'm not mistaken, that the files has to be reloaded each time one of the scripts is called, doesn't it? Or does PHP/Apache have a caching system that may keep the script/variable in RAM so as not to load it every time?

Well, anyway, I've been thinking about other ways to do what I want. One obvious way seems to be using e.g. a MySQL database and retrieve the phrases from there, however I doubt this would be very efficient. Storing the phrases in $_SESSION would be faster but would mean that I'm storing the whole table for each user, which isn't efficient in terms of memory. I was thinking about using some kind of environment variable, but AFAIK putenv only allows storing strings and isn't persistent through script calls.

So my question: is there a way to load such constant data (in possibly "complex" data structures -- i.e. not only strings) once and for all when e.g. the Apache server starts (or on demand the first time the variable is needed) so that it's available for all PHP scripts without having to load it each time? I guess that would actually mean creating my own superglobals with the contents I want...
Alternatively and if there are other possibilities, what would you say would be the best way to do what I want?

Thanks a lot for your help!

Cheers,

pagod

Re: Localization with PHP

Posted: Tue Jan 12, 2010 1:23 pm
by AbraCadaver
APC or some other caches may cache the include file, however, FWIW I wouldn't worry about it. This is pretty common practice with the exception that I normally see multiple files like 'en-GB-localization.php' or something and after you determine the user's language you would load the correct file. This might be a better approach if the one file is or can become very large. It also helps you and translators, so they aren't editing the one large file. I would do the files approach and not worry about having to include it every time, or do it in a DB table so that you could code an interface for translators to add/edit.

Re: Localization with PHP

Posted: Wed Jan 13, 2010 3:49 am
by pagod
Thx AbraCadaver,

I see one advantage in having just one file with all languages in it: it makes translation easier because you can see all the equivalents of a phrase you're trying to translate in one glance. This of course will change as soon as I get a chance to code a web interface for external localization and it will then definitely make sense to separate everything, but right now it's just me and my web application ;-)

So that means there's no way of storing "personal" superglobals (may even be constants) in PHP?

cheers,

pagod