Reputation: 103338
I'm building a website which will have a Spanish and Italian version.
In the past, when building a site which must be available in different languages, I've created a SQL Table like follows:
dbo.News
--------
ID int
EnglishTitle nvarchar(200)
SpanishTitle nvarchar(200)
ItalianTitle nvarchar(200)
EnglishContent nvarchar(max)
SpanishContent nvarchar(max)
ItalianContent nvarchar(max)
Then, depending on a query string (domain.com/NewsArticle.aspx?id=123&l=es) I would do something like:
Select Case Request.QueryString("l")
Case "en"
TitleLtl.Text = "SELECT EnglishTitle..."
Case "es"
TitleLtl.Text = "SELECT SpanishTitle..."
Case "it"
TitleLtl.Text = "SELECT ItalianTitle..."
End Select
However, I've found this really time consuming (especially if the client later asks for another language option as a second phase).
What is the best practice for doing something like this, allowing scope for extra languages?
I'm open to VB.NET and C# solutions, Cheers!
Upvotes: 4
Views: 2379
Reputation: 27464
Try something more like this:
text_code int
language char(2)
text varchar(max)
Where text_code is some identifier of which piece of text you need, like 1=title, 2=content, etc. Or you could use a mnemonic code, I don't think that would hurt performance much as long as it is short.
Then any place you need text, you should know the code for the piece of text you need, and you know the language. Use those to select the record. Then you don't need a big case statement.
i.e. your code is then something like:
command.query="select text from local_text where text_code=@text_code and language=@language"
command.Parameters.AddWithValue("@text_code", 17) ' Probably use symbols there
command.Parameters.AddWithValue("@language", language)
dim text as string=command.executeScalar()
There's no CASE statement as you only get one language from the query.
Upvotes: 0
Reputation: 21630
How about something like this?
dbo.News
--------
ID int
Label varchar(200)
DateAdded smalldatetime
(and any other common fields)
dbo.NewsCultures
----------------
ID int -- FK to the dbo.News PK
LocaleID smallint
Title nvarchar(200)
Content nvarchar(MAX)
Where LocaleID would map to a .net LCID - such as 1033 for American English.
From a non-database perspective, there are already several responses that include links to some good resources.
Upvotes: 0
Reputation: 1249
From your question it looks like you are more interested in showing content in different rather then something like a label.
If that's the case, you could always look at a subclass model, so your dbo.News table would have an extra column (Discriminator) and would only have one title and content columns
Your class model would have News as the parent class and EnglishNews, SpanishNews etc as subclasses.
if you're looking for the italian version of a news story, you would retrieve the ItalianNews object with the proper news id (the newsId would be the same for each language that has the same story).
NHibernate allows for easy subclassing and object retrieval, so while adding another language will require some coding, you wont have to rebuild your DB and greatly change your code around.
Hope this helps.
Upvotes: 0
Reputation: 44595
Simply read some articles, start from here:
ASP.NET Globalization and Localization
there are many features of .NET and ASP.NET to help you in localize and globalize your site without such time consuming and home-made solutions like yours above...
Upvotes: 4