ASP.NET Database Localisation
I am working on a website that is going to be distributed in multiple countries, but each county should have its own site. My plan is to have a different database(with the same structure) for each country, but how do I select which database to use depending on which county they are access the website in.
I have seen other sites that use the same principle where they have a .com and a .co.uk address for example and no matter which one you go to you are always provided with content localized to you country. How is this done? Is it possible to customize the database connection string based on the browsers country.
This is the first time I have produced a multi national site so trying to lea开发者_Go百科rn as much as I can about localization
I would strongly suggest you not to use different databases for each language. In fact this would be very much against the DRY principle, as you would have to repeat all the data that is not language dependent in each database and if you want to update one of those fields you would have to do it in each database.
I would rather suggest you to use the normal global and local resources of ASP.NET. Or if you want to keep the text in the database I would suggest to keep a single one with all the data.
There are many types of structures of the tables that account for multiple languages and deciding amongst them depends on how many languages you need and other factors. here there is a good post about a few strategies: What's the best database structure to keep multilingual data?
For what concerns the selection of the language automatically I rather prefer using the country of the user based on their IP, same as Google and other big sites. To do this I bought the database of IP from maxmind (it was 50€) and it was easy to implement and very precise.
I hope this helps!
You can write the below code in the "Application_Start()" method of the "Global.asax.cs" file which gets the country name based on the browser and stores the "ConnectionString" in a session variable.
protected void Application_Start()
{
string[] languages = HttpContext.Current.Request.UserLanguages;
if (languages != null && languages.Length > 0)
{
string language = languages[0].ToLowerInvariant().Trim();
CultureInfo currentCulture = CultureInfo.CreateSpecificCulture(language);
if (currentCulture != null)
{
RegionInfo regionInfo = new RegionInfo(currentCulture.LCID);
}
}
switch (RegionInfo.CurrentRegion.Name)
{
case "...":
Session["ConnectionString"] = "...";
break;
}
}
protected void Application_End()
{
Session.Clear();
}
A resource file can be used to store all the connection strings instead of hard-coding here.
Hope this helps you!!
精彩评论