Localization with PHP

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
pagod
Forum Newbie
Posts: 5
Joined: Wed Mar 05, 2008 10:59 am

Localization with PHP

Post 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
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Localization with PHP

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
pagod
Forum Newbie
Posts: 5
Joined: Wed Mar 05, 2008 10:59 am

Re: Localization with PHP

Post 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
Post Reply