Piers MacDonald
Piers MacDonald

Reputation: 563

Best practice to localize web page text (given criteria)?

I'm on a new project for an existing website and it's current localization scheme is this:

en_welcome.aspx
fr_welcome.aspx
de_welcome.aspx
es_welcome.aspx

and each of those contains significant business logic. I'm told the reason for this is that the only persistant memory around is the SQLServer and it's already getting the snot beaten out of it so more DB calls to get the localized language string aren't welcome. However our web servers have gobs of RAM and our site is only a dozen or so pages and I can't imagine all the language strings being much over 300kb so we could just have IIS load them into memory once in awhile from the DB.

What is the best way to do this and how would I access it in code? Is this a terrible idea? How would you trigger the web server to reload the strings from DB?

Upvotes: 0

Views: 170

Answers (2)

Mike Parkhill
Mike Parkhill

Reputation: 5551

You could route all your text lookups through a cached data provider. Eg ICachedTextDataProvider.GetString(string key, CultureInfo culture) or some thing like that.

A simple cache in your dataprovider could then be created using a Dictionary:

static Dictionary<CultureInfo, Dictionary<string,string>> m_labelsByCulture;

You'd then get the right label dictionary by looking up the culture as a key.

As far as expiring it goes you could simply keep a last updated time and whenever a request is made you check how long it's been and if need be re-load the labels. You can se this time globally for all labels or if you want to make it perform better for each language or even label individually - depends on how complex you want to make it and what your performance concerns are.

That's a very basic strategy. There are more complicated approaches to caching out ther though.

Upvotes: 0

StriplingWarrior
StriplingWarrior

Reputation: 156469

This is typically managed using a resource file.

Rather than being stored in your database, the resx file can be part of your code base, or it can be provided in any other way you see fit (see the recommendation for Sisulizer in the comments). You'd have a single "welcome.aspx" page, which uses string keys (usually from a constants class) to look up each message in the user's language.

Upvotes: 1

Related Questions