Localization with PHP
Posted: Tue Jan 12, 2010 12:34 pm
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.
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
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>
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