This can be done without a querystring, and without cookies, assuming your user has a session.
Same approach as in original post, but you put it in Global.asax's
Application_PreRequestHandlerExecute instead of Application_BeginRequest. In BeginRequest, there is no Session. Session is sometimes available during Application_PreRequestHandlerExecute though, assuming the request is generated for the actual page (with or
without a parent masterpage). For example, this method can be called when requesting external stylesheets, etc. So, we have to make sure the context of the request contains a session:
This example only sets the UI culture, because my app is only concerned with a few western languages at this point, but I'm sure it could be adapted to set the culture in addition to the uiculture.
My Signature: Mark a post as answered if it helped you, or don't, it makes no difference to me. Just PLEASE try not to use the word "random" when describing your problem.
I would most likely use session over cookies. Not all users browse with their cookies settings on. If you where to use this, I would most likely do a cookies enabled validation when the dropdownlist is clicked, then display some sort of message saying "cookies
must be enabled to change languages"
I was about to post my code I created, but I couldnt remember what project I saved it under. I pretty much did what was posted throughout the previous post. Create a base page then override the initculture() method. Then inheirt the base page into the
master page.
In order to have a completed process (multilanguages in a C# web application), what else do I have to add on web.config? what about creating resources files each related to specific language. As I am reading some articles, they have mentioned that. Please
I am in need for some clarifications related to this subject.
Hey danludwig I tried using your code for my page, and instead of spanish i changed it to German(the language im trying to translate the page to) but the problem is that nothing happens when I select german on the drop down...any ideas?
By using the code that was givin in this thread the transalation will not happen auto for you. Any static text that you may have on the page needs to be placed in a localize control and the page needs to have an .resx for each language you want to support.
The .resx files hold static text to be placed on the .aspx at runtime.
Example: You have a home page called home.aspx and in App_LocalResources you have
home.ascx.resx which would be your default supported langauge. You will also need to have one for each language.
After creating this class, I have put this class in App_Code folder. Please tell me how to call this class. Shall I call from master page or Default.aspx page or from Global.aspx class from Application_start method? Shall I have to do
Here is how you should apply it. Suppose you have placed the above mentioned class source in a file called BasePage.cs and placed it in the App_Code directory.
Should you create a new aspx page named MyPage under a directory MyDir, the code behind will look like so:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class MyDir_MyPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
You should change it so it looks like so:
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls;
public partial class MyDir_MyPage : BasePage { protected void Page_Load(object sender, EventArgs e) {
} }
What you have done is to declare that your page is a derivative of the BasePage class, which in turn is of System.Web.UI.Page class. So all methods & properties would have been inherited.
danludwig
Contributor
3020 Points
601 Posts
Re: How to do a language switch in a master page.
Jan 23, 2007 05:21 PM|LINK
This can be done without a querystring, and without cookies, assuming your user has a session.
Same approach as in original post, but you put it in Global.asax's Application_PreRequestHandlerExecute instead of Application_BeginRequest. In BeginRequest, there is no Session. Session is sometimes available during Application_PreRequestHandlerExecute though, assuming the request is generated for the actual page (with or without a parent masterpage). For example, this method can be called when requesting external stylesheets, etc. So, we have to make sure the context of the request contains a session:
void Application_PreRequestHandlerExecute(object sender, EventArgs e){
if (Context.Session != null && Context.Session["SelectedLanguage"] != null)
{
String selectedLanguage = Context.Session["SelectedLanguage"].ToString().ToLower();
String currentLanguage = CultureInfo.CurrentUICulture.TwoLetterISOLanguageName.ToLower();
if (!currentLanguage.Equals(selectedLanguage))
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo(selectedLanguage);
}
}
}
Then, these event handlers go with your DropDownList, be it in a masterpage or an aspx file:
<asp:DropDownList ID="LanguageDropDownList" runat="server" AutoPostBack="True"OnSelectedIndexChanged="LanguageDropDownList_SelectedIndexChanged"
OnPreRender="LanguageDropDownList_PreRender">
<asp:ListItem Value="en">English</asp:ListItem>
<asp:ListItem Value="es">Spanish</asp:ListItem>
</asp:DropDownList>
protected void LanguageDropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
// TODO: Validate that ddl.SelectedValue is valid TwoLetterISOLanguageName
Session["SelectedLanguage"] = ddl.SelectedValue;
Response.Redirect(Page.AppRelativeVirtualPath, true); // No query string
}
protected void LanguageDropDownList_PreRender(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
ddl.SelectedValue = CultureInfo.CurrentUICulture.TwoLetterISOLanguageName;
}
This example only sets the UI culture, because my app is only concerned with a few western languages at this point, but I'm sure it could be adapted to set the culture in addition to the uiculture.
CSharpSean
Contributor
5464 Points
917 Posts
Re: How to do a language switch in a master page.
Jan 24, 2007 02:44 AM|LINK
I would most likely use session over cookies. Not all users browse with their cookies settings on. If you where to use this, I would most likely do a cookies enabled validation when the dropdownlist is clicked, then display some sort of message saying "cookies must be enabled to change languages"
I was about to post my code I created, but I couldnt remember what project I saved it under. I pretty much did what was posted throughout the previous post. Create a base page then override the initculture() method. Then inheirt the base page into the master page.
CSharpSean
Contributor
5464 Points
917 Posts
Re: How to do a language switch in a master page.
Jan 24, 2007 02:49 AM|LINK
A quick question . . . .
Has anyone used localization for dynamic data? For example, allowing an admin to add/update/delete in different languages? (en/fr/es)
I've heard you cant edit the resource files that are associated with each page b/c it is compiled into a dll on run-time? How true is this statement?
Also, is it good practice to put long text strings into the resx files? for example - the body-text of a page?
Ranine2007
Member
26 Points
54 Posts
Re: How to do a language switch in a master page/multilanguages in a C# web application
Feb 26, 2007 01:01 PM|LINK
Hello,
In order to have a completed process (multilanguages in a C# web application), what else do I have to add on web.config? what about creating resources files each related to specific language. As I am reading some articles, they have mentioned that. Please I am in need for some clarifications related to this subject.
Regards,
Ranine
alug
Member
57 Points
20 Posts
Re: How to do a language switch in a master page.
Mar 27, 2007 01:17 PM|LINK
Hi:
You can find help in this context from the following URL.
http://www.codeproject.com/useritems/localization.asp
Kind Regards
sinner850408
Member
2 Points
1 Post
Re: How to do a language switch in a master page.
Mar 29, 2007 07:53 AM|LINK
vb89
Member
21 Points
43 Posts
Re: How to do a language switch in a master page.
Jul 20, 2007 04:54 PM|LINK
shawnregan
Member
4 Points
5 Posts
Re: How to do a language switch in a master page.
Aug 31, 2007 07:48 PM|LINK
By using the code that was givin in this thread the transalation will not happen auto for you. Any static text that you may have on the page needs to be placed in a localize control and the page needs to have an .resx for each language you want to support. The .resx files hold static text to be placed on the .aspx at runtime.
Example: You have a home page called home.aspx and in App_LocalResources you have home.ascx.resx which would be your default supported langauge. You will also need to have one for each language.
French: home.ascx.fr.resx
Spanish: home.ascx.es.resx
etc...
No, way to do any auto translation of everything for a site. You still need to translate your pages and data. :)
Check this video out for just this topic on localization: http://www.asp.net/learn/ajax-videos/video-179.aspx
It does some with ajax, but it also shows the basics for localization.
anilraja
Member
3 Points
5 Posts
Re: How to do a language switch in a master page.
Sep 14, 2007 01:04 AM|LINK
After creating this class, I have put this class in App_Code folder. Please tell me how to call this class. Shall I call from master page or Default.aspx page or from Global.aspx class from Application_start method? Shall I have to do
Base bs = new Base();
?
raybiez
Member
83 Points
60 Posts
Re: How to do a language switch in a master page.
Oct 31, 2007 10:38 AM|LINK
Here is how you should apply it. Suppose you have placed the above mentioned class source in a file called BasePage.cs and placed it in the App_Code directory.
Should you create a new aspx page named MyPage under a directory MyDir, the code behind will look like so:
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class MyDir_MyPage : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } }You should change it so it looks like so:
What you have done is to declare that your page is a derivative of the BasePage class, which in turn is of System.Web.UI.Page class. So all methods & properties would have been inherited.
Neil Craig | Web Developer
www.k2.com
Give credit where it is due, mark a post as an answer if it helped you.