...

View Full Version : How to develop a multilingual website?



santwize
04-25-2007, 01:43 PM
hi,
I am trying to develop a multilingual website using .net c#
in which i'll have three languages English,spanish,Arabic.
Do anyone has code how to write the code in which onbuttonclick
the respective culture will be launched and correspondind input
language be activated programatically.

Every kind of help will be appreciated.

Thanks and Regards:
Sant

Krokador
04-25-2007, 06:43 PM
Yup, I've recently done this hehe.

#1 put this in the configuration section of your web.config


<globalization uiCulture="auto:en-US" culture="auto:en-US" />
<anonymousIdentification enabled="true"/>
<profile>
<properties>
<add name="Lang" type="string"
allowAnonymous="true" />
</properties>
</profile>
Note: this will detect the browser/OS configuration and display the appropriate language, or fallback to english us, if you ant another default language, you gotta change en-US for the desired language and locale. Google is your friend :P

Next, in your app_code folder, make a new class and call it BasePage, make it inherit from system.web.ui.page. In the code section, add this sub: (note, code is in C# and you might need to sue the imports on some pages too.)



using System.Resources;
using System.Globalization;
using System.Threading;

...

protected override void InitializeCulture()
{
ProfileCommon Profile = (ProfileCommon)HttpContext.Current.Profile;
string UserCulture = (String)Profile.GetPropertyValue("Lang");
if (UserCulture != "")
{
// there is a user language setting in the profile: switch to it
Thread.CurrentThread.CurrentUICulture = new CultureInfo(UserCulture);
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(UserCulture);
}
}

now lets say you got that cool btnEnglish button on your page to switch the language to english, the btnEnglish_Click event should look like this


protected void btnEnglish_Click()
{
Profile.SetPropertyValue("Lang", "en-US");
Profile.Save();
Response.Redirect(Request.Url.PathAndQuery, false);
}

Next thing you need are the resource files.
In your aspnet project, create the aspnet folder App_GlobalResources.
Create a file you'll name global.resx (or whatever you want).
Input a few keys in it, like Title "Hello World".
Now copy the file in the same folder, and rename to global.es.resx, go and change the key values to the spanish text.

To access it, all you need to do now is to use something like
lblTitle.Text = (String)GetGlobalResourceObject("global", "Title");

you can also use local files. the App_LocalResource folder has to be on the same level as the page, and you gotta name your files in it according to your page name (EG MyPage.aspx local resource files would be named MyPage.es.resx and MyPage.resx).

You can add keys like MyLabel.Text "text of a label", MyLabel.ForeColor "Blue" and so on as if it was the label properties themself you were declaring, and then in your asp code you'd have


<asp:Label ID="lblTest" runat="server" meta:resourcekey="MyLabel" />


You'll also need to make sure that all your pages inherit from your basepage instead of system.web.ui.page

I hope that helped you. If you're still not too sure about it, search localization aspnet on google, there are plenty of tutorials out there, thats how I learned ;)

santwize
04-26-2007, 01:40 PM
thanks a lot Krokador,
But i have what u have given here ...and as i am new to this development environment i am unable to integrate all these things,
so can u give me more specific codes for this.
and one more problem is that i want my textboxes to take input in arabic from right to left....how to do that.

Thanks.
sant

Krokador
04-27-2007, 01:35 AM
well I could give you a small example zip, do you want VB.Net or C# ?
And for the right to left hmm, I think that's do-able through resource files too, I just forgot the name of the property to set for that. I'll get back to you with that once I have a bit of time to put together the example.

santwize
04-27-2007, 08:17 AM
well I could give you a small example zip, do you want VB.Net or C# ?
And for the right to left hmm, I think that's do-able through resource files too, I just forgot the name of the property to set for that. I'll get back to you with that once I have a bit of time to put together the example.

Hi Krokador,
i want codes in c#...thanks

Lallo
05-01-2007, 12:36 PM
You can just use database or xml files for all languages and using cookies or a table that will save the user's language and everytime the user goes to the site it wil be on its language. I woul suggest you to use database because it is easy to manage you have tables and everything is in one file and the xml will be more files - according to the languages that you want to use.


So start to think of DATABASE!!!!!!


Regards!

Krokador
05-01-2007, 02:02 PM
I havent had the time to build that example yet (will try during my lunch break today) but about what you said Lallo

using the aspnet profiles IS storing into a database. Allowing anonymous just makes it so a user who cant login can also save his own preference (and as far as ive tested, it works, a computer will keep the info.)

putting language files into the database makes for some fun doing the queries. You have to put in an if or a case statement depending on what language you want to load, whereas resource files (which are xml, btw) just need a call to the key name and voilą you get the right language thats stored in your profile whenever you visit the site.

Lallo
05-01-2007, 06:50 PM
That was just an idea!!! I would do it this way!

Regards!

berkut2006
03-13-2011, 05:23 AM
Another idea:

http://www.docstorus.com/viewer.aspx?code=c76083e2-047b-4ad4-bf39-ffd5fb72c8bc

works pefectley for me :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum