You could store the default language/culture information in a session and set it to a current webpage. This change could be triggered through any control. Read on for the code snippet.
Create a Page called "MultiLanguagePage.aspx". For the explanation, i have used a textbox,button, calendar and a drop down list (has the various cultures listed).
Let's name the TextBox has "inputbox", drop down as "ddlCultures", button as "btnClick"
In the page load event of the "MultiLanguagePage.aspx" write the following code.
//Page_Load event
/// <summary>
/// Handles the Load event of the Page control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
protected void Page_Load(object sender, EventArgs e)
{
List<CultureInfo> result = new List<CultureInfo>();
foreach (CultureInfo culture in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
{
ddlCultures.Items.Add(new System.Web.UI.WebControls.ListItem(culture.NativeName, culture.Name));
}
}
/// <summary>
/// Handles the Click event of the btnClick control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
protected void btnClick_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(ddlCultures.SelectedValue))
{
ResetCulture(new CultureInfo(ddlCultures.SelectedValue));
}
}
/// <summary>
/// Resets the culture.
/// </summary>
/// <param name="culture">The culture.</param>
protected void ResetCulture(CultureInfo culture)
{
if (null != culture)
{
//Apply New Culture
ApplyNewCulture(culture);
//Refresh the current page
Response.Redirect(Request.Url.AbsoluteUri);
}
}
/// <summary>
/// Applies the new culture.
/// </summary>
/// <param name="culture">The culture.</param>
private void ApplyNewCulture(CultureInfo culture)
{
//Set current Thread UI Culture
Thread.CurrentThread.CurrentUICulture = culture;
//Set current Thread Culture
Thread.CurrentThread.CurrentCulture = culture;
//Store current language in session
Session.Add("Current_Culture", culture);
}
You will see that on refresh, all the controls on the page will reflect the new culture. Retrieve it in the page_load as preference from the session using the following line of code.
//use sessopn culture
if (null != Session["Current_Culture"])
{
ApplyNewCulture((CultureInfo) Session["Current_Culture"]);
}
Let's name the TextBox has "inputbox", drop down as "ddlCultures", button as "btnClick"
In the page load event of the "MultiLanguagePage.aspx" write the following code.
//Page_Load event
/// <summary>
/// Handles the Load event of the Page control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
protected void Page_Load(object sender, EventArgs e)
{
List<CultureInfo> result = new List<CultureInfo>();
foreach (CultureInfo culture in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
{
ddlCultures.Items.Add(new System.Web.UI.WebControls.ListItem(culture.NativeName, culture.Name));
}
}
/// <summary>
/// Handles the Click event of the btnClick control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
protected void btnClick_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(ddlCultures.SelectedValue))
{
ResetCulture(new CultureInfo(ddlCultures.SelectedValue));
}
}
/// <summary>
/// Resets the culture.
/// </summary>
/// <param name="culture">The culture.</param>
protected void ResetCulture(CultureInfo culture)
{
if (null != culture)
{
//Apply New Culture
ApplyNewCulture(culture);
//Refresh the current page
Response.Redirect(Request.Url.AbsoluteUri);
}
}
/// <summary>
/// Applies the new culture.
/// </summary>
/// <param name="culture">The culture.</param>
private void ApplyNewCulture(CultureInfo culture)
{
//Set current Thread UI Culture
Thread.CurrentThread.CurrentUICulture = culture;
//Set current Thread Culture
Thread.CurrentThread.CurrentCulture = culture;
//Store current language in session
Session.Add("Current_Culture", culture);
}
You will see that on refresh, all the controls on the page will reflect the new culture. Retrieve it in the page_load as preference from the session using the following line of code.
//use sessopn culture
if (null != Session["Current_Culture"])
{
ApplyNewCulture((CultureInfo) Session["Current_Culture"]);
}
No comments:
Post a Comment