views:

68

answers:

2

Hi, I would like some advice on storing data for different languages on my website. I've spent hours searchin and can't seem to find what I need. Here's my situation, so let me know what you think.

I have a website that multiple users can log into to view and store information. I use php, Mysql, Apache, on a FreeBSD operating system (although during development right now its on my home box with Vista). I store some account information in the $_SESSION, but mostly use classes with local variables to store the data I need. I'll have anywhere from 2,000 to 6,000 users of the webapp, so I'm concerned about performance. I want to make this available in multiple languages. Some sites I've seen provide a dropdown list the user can select their language preference in, which I like.

I've got two options I thought of, but have no clue as to which is better, or if there is a better way to accomplish this. One would be to store the language specific data in the $_SESSION object. So, the user would log in, and based on the language preference the $_SESSION would be populated with the appropriate language text into the variables used throughout the webapp. This could mean I would have around 300 or so variables with string data (no objects)...such as $_SESSION['My_Title'] = "This is the title to my website, in english, or german, etc.". The other option would be to use CONSTANTS and define each CONSTANT in a config text file and load that file upon login based on the language preference set. I read somewhere that using CONSTANTS is somewhat slower than the $_SESSION, but the Session would use up more RAM.

Any ideas, or resources you could point me to? Thanks.

+1  A: 

The data you put into $_SESSION is on a per-user basis -- which means if you put the same data in $_SESSION for 10 users, then you'll have that data duplicated 10 times.

I would not put the localized string of your application in $_SESSION, personnaly : it's something that is "constant", the same for every users -- so it doesn't have its place in a space that's specific to each user.


Using PHP constants might be an idea ; for instance :

en.php :

define('LANG_TITLE', 'The title of the site');
define('LANG_WElCOME', 'Welcome on my site');

And fr.php :

define('LANG_TITLE', 'Le titre du site');
define('LANG_WELCOME', 'Bienvenue sur mon site');


Using a PHP array would be another ; for example, you'd have en.php :

$lang = array(
    'title' => "The title of the site", 
    'welcome' => "Welcome on my site", 
);

And fr.php :

$lang = array(
    'title' => "Le titre du site", 
    'welcome' => "Bienvenue sur mon site", 
);


Which one should you choose ? I suppose it's mainly a matter of taste, and there shouldn't be much of a difference between those two ideas.


Or, another totally idea would be to use something like gettext (see also), which is a standard way of doing translations, and is (of course) usable from PHP.

If I had to choose, I might go for a solution based on gettext, as it's pretty standard -- or I'd use the Zend_Translate classes from Zend Framework, at least for a ZF-based project (btw, amongst those, there is one adapter which is using gettext ;-) )

Pascal MARTIN
Thanks for the reply. If I use PHP Constants, is this the same scenario as using the $_session, meaning, if I have 1000 users, there will be 1,000 different instances of the constant variables? When using constants, what would happen if a user from France logged in, then a user user from america logged in and the config file updated the constant var's would the french user still see the website in french, or would the constant vars be updated to english? I'm confused...are the constant vars "session specific"? I'll look at Zend_Translate...I've never heard of this. thanks.
Ronedog
O.k, I'm sort of an idiot...I decided to use Constants, and as I found out, they don't work like $_session...there in lies my idiocracy....constants are working like I wanted them to. thanks for your help
Ronedog
A: 

Try to store as few data as possible in your session. By default the session data is stored in you file system by PHP and file system access is almost always slow.

I use session to store an identifier for the user. Apart from that I think twice about putting anything into the session.

Possible alternatives for storing data that needs to be persistent between requests are memory data chaches or your database (maybe in a table that is only stored in memory).

Techpriester
Thanks for the reply. I am storing my session data in a mysql database. I didn't want to leave it in a file on the server for security purposes. The challeng I have is I have to be able to allow all users the ability to view the website in the language of their choice, which is why I thought that the session was a good place to keep each users language data unique to that user. I agree with you, I don't like storing that much stuff in the session. I guess I could have a table with each language as its own table, then just pull from that as needed, but I won't that caus performance problems?
Ronedog