Application_BeginRequest(ByVal sender
As
Object,
ByVal e
As EventArgs)
Dim lang As
String = String.Empty
Dim cookie
As HttpCookie = Request.Cookies("lang")
If Not (cookie
Is Nothing)
AndAlso Not (cookie.Value
Is Nothing)
Then
Class _Default
Inherits System.Web.UI.Page
Protected Overrides
Sub InitializeCulture()
Dim lang As
String = Request("Language1")
If Not lang =
"auto" Then
If lang IsNot
Nothing Or lang <>
"" Then
End If
End If
End Sub
Protected Sub Language1_SelectedIndexChanged(ByVal sender
As Object,
ByVal e As System.EventArgs)
Dim cookie
As HttpCookie = New HttpCookie("lang")
cookie.Value = Language1.UniqueID
Response.SetCookie(cookie)
End Sub
Protected Sub Page_Load(ByVal sender
As Object,
ByVal e As System.EventArgs)
Handles Me.Load
If Language1.SelectedItem.Value =
"auto" Then
it’s not that clean but I think it’s better than nothing and I like to share the idea with you as you are more experienced, I’m going to tell you the situation how I solved that.
Scenario:
1-Build a multilingual website using ASP.NET 2.0 for my company
2-MaterPage with a change language combo that is localized at the first request of the page [suing asp.net 2.0 new browser request culture detection] and then the user can change the language any time from the master page. [So the new culture reflects all the
website pages]
3-It’s supposed that there are 3 languages, English, French and Arabic [Arabic needs the pages to rendered in RTL
L I did it but there is some issues]
I didn’t put any code in the global.asax -
Application_BeginRequest, I figured out a way for doing that, thanks for the great hint of
Ran Davidovitzto use the helper class which actually is a class that inherits from a webpage to add the
InitializeCulture() , and then make all site pages inhereit from this baseclass.
a. Add the global resource files under the App_GlobalResources folder:
AllSite.ar.resx
AllSite.resx
AllSite.ar.resx:
<dataname="TextDirection"xml:space="preserve">
<value>RTL</value>
</data>
AllSite.resx:
<dataname="TextDirection"xml:space="preserve">
<value>LTR</value>
</data>
b. HTML direction attribute to be retrieved from a global resource to adjust page direction [in case of English, French it will be LTR and Arabic will be RTL
4-Build the web pages [the will be in the content place holder in the master page] and generate local resources, and DON’T FORGET INHERIT FROM THE BASEPAGE CLASS ;), and add your local resources
for them.
My Issues is that some controls doesn’t render to RTL well , I don’t know why and what’s going to drive me nuts is the same control (ex: asp:Label) renders sometimes right and sometimes wrong
.. I think there is a web design consideration that should be taken or layout considerations to support both RTL and LTR languages; I hope any one can help me in this area
first I want to say that I prefer the global.asax solution - it's much easier for locaolize an homepage which already exists. also microsoft means that editing the global.asax is a goot way to make changes for a whole website.
but it doesn't matter, my problem is a little bit different
set the uiculture and co to auto and starting my browser with different language settings let everything work fine, especially the Convert.ToDateTime(Textbox1.Text); works fine.
But if I do a language switch with a drop down box like mentioned in the post - I get an exception when my code tries to convert the same textbox with the localized date.
so whats the main problem hier - letting the browser set the language everything looks the same on the frontend then setting the language by hand. only problem is - when I set the language by hand the convert.todatetime throws an exception.
Hello, could you explain this step in detail please.
4-Build the web pages [the will be in the content place holder in the master page] and generate local resources, and
DON’T FORGET INHERIT FROM THE BASEPAGE CLASS ;), and add your local resources for them.
well i've mentioned before that you should create a helper class, that all your webpages will inherit from to execute the code in the InitializeCulture() in all tour pages without having to override this mehtod in every webpage
code
"3-Build a BaseClass under the App_Code\ forlder in your project ,that all pages will inherit from :
using System;
using System.Data;
using System.Configuration;
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;
using System.Threading;
using System.Globalization;
///<summary>
/// Summary description for BasePage
///</summary>
public
classBasePage :Page
{
public BasePage()
{
//
// TODO: Add constructor logic here
//
}
protectedoverridevoid InitializeCulture()
{
string lang =string.Empty;//default to the invariant culture
Thak you for this forum, it has helped mi so much!!![:D] I've used EngMotagaly 's solution and it works fine, but i will need to introduce some changes, because my applicattion will execute in two servers and because of that i won't be able to use cookies.
Mi code in the Master Page is:
ViewState["Idiom"]= lang;
Mi code in the class BasePage is like this:
string lang= (string)ViewState["Idiom"];
And then the code is the same as
EngMotagaly, but it doesn't work and i don't know if i need to enable viewState somewhere (i'm beginning whit asp.net). Any ideas?
Than you so much and sorry for my english (i'm spanish)
Hello, it´s me again, i've finnaly coded it using session and it works all right, there´s only a handicap and it´s that i need to double-click on the select culture button, any ideas? thanks very much
saratjandhya...
Member
35 Points
7 Posts
Re: How to do a language switch in a master page.> could you check it once> why it doesn't work
Jul 03, 2006 10:10 AM|LINK
<
asp:content id="ContentLogin" contentplaceholderid="Main_Content" runat="server"> <div> <asp:Label ID="Label1" runat="server" Text="Select Culture" meta:resourcekey="Selectculture"></asp:Label> <asp:DropDownList ID="Language1" AutoPostBack="true" OnSelectedIndexChanged="Language1_SelectedIndexChanged" runat="server"> <asp:ListItem value="auto">Auto</asp:ListItem> <asp:ListItem Value="en-US">English</asp:ListItem> <asp:ListItem Value="zh-sg">Chinese</asp:ListItem> <asp:ListItem Value="ru">Russain</asp:ListItem> </asp:DropDownList> <asp:Calendar ID="Calendar1" runat="server"></asp:Calendar> </div></
asp:Contentin global.asax
Sub
Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs) Dim lang As String = String.Empty Dim cookie As HttpCookie = Request.Cookies("lang") If Not (cookie Is Nothing) AndAlso Not (cookie.Value Is Nothing) Thenlang = Request.Form(cookie.Value)
End IfThread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(lang)
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang)
End Subin .vb
Imports
System.GlobalizationImports
System.ThreadingPublic
Class _Default Inherits System.Web.UI.Page Protected Overrides Sub InitializeCulture() Dim lang As String = Request("Language1") If Not lang = "auto" Then If lang IsNot Nothing Or lang <> "" ThenThread.CurrentThread.CurrentUICulture =
New CultureInfo(lang)Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang)
End If End If End Sub Protected Sub Language1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Dim cookie As HttpCookie = New HttpCookie("lang")cookie.Value = Language1.UniqueID
Response.SetCookie(cookie)
End Sub Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Language1.SelectedItem.Value = "auto" ThenThread.CurrentThread.CurrentUICulture = CultureInfo.CurrentCulture
End If End SubEnd
ClassGuya
Member
79 Points
18 Posts
Re: How to do a language switch in a master page.
Jul 10, 2006 02:45 PM|LINK
Hi
What happen if cookie is not enabled on the client browser? any other solutions?
EngMotagaly
Member
52 Points
10 Posts
Re: How to do a language switch in a master page.
Jul 12, 2006 07:46 AM|LINK
Dear ppl,
I figured out a solution for the problem in the thread, its a combination of all posts including this one http://forums.asp.net/1219973/ShowPost.aspx :)
it’s not that clean but I think it’s better than nothing and I like to share the idea with you as you are more experienced, I’m going to tell you the situation how I solved that.
Scenario:
1-Build a multilingual website using ASP.NET 2.0 for my company
2-MaterPage with a change language combo that is localized at the first request of the page [suing asp.net 2.0 new browser request culture detection] and then the user can change the language any time from the master page. [So the new culture reflects all the website pages]
3-It’s supposed that there are 3 languages, English, French and Arabic [Arabic needs the pages to rendered in RTL L I did it but there is some issues]
I didn’t put any code in the global.asax - Application_BeginRequest, I figured out a way for doing that, thanks for the great hint of
Ran Davidovitz
to use the helper class which actually is a class that inherits from a webpage to add the
InitializeCulture() , and then make all site pages inhereit from this baseclass.
What I did:
1-web.config
<globalization enableClientBasedCulture="true" culture="en-US" uiCulture="en-US" requestEncoding="utf-8" responseEncoding="utf-8" />
2-MasterPage:
Design:
a. Add the global resource files under the App_GlobalResources folder:
AllSite.ar.resx
AllSite.resx
AllSite.ar.resx:
<data name="TextDirection" xml:space="preserve">
<value>RTL</value>
</data>
AllSite.resx:
<data name="TextDirection" xml:space="preserve">
<value>LTR</value>
</data>
b. HTML direction attribute to be retrieved from a global resource to adjust page direction [in case of English, French it will be LTR and Arabic will be RTL
<html runat="server" xmlns="http://www.w3.org/1999/xhtml" dir='<%$ Resources:AllSite, TextDirection %>'>
c. localized label and combo for language selection
<td align="left">
<asp:Label ID="lblSelLang" runat="server" meta:resourcekey="lblSelLangResource1"
Text="Select your language:"></asp:Label>
</td>
</tr>
<tr>
<td align="left">
<asp:DropDownList ID="comboSelLanguage" runat="server" AutoPostBack="True" meta:resourcekey="comboSelLanguageResource1"
OnSelectedIndexChanged="comboSelLanguage_SelectedIndexChanged">
<asp:ListItem meta:resourceKey="ListItemResource1">Choose ...</asp:ListItem>
<asp:ListItem meta:resourceKey="ListItemResource2" Text="English" Value="en"></asp:ListItem>
<asp:ListItem meta:resourceKey="ListItemResource3" Text="Francais" Value="fr"></asp:ListItem>
<asp:ListItem meta:resourceKey="ListItemResource4" Text="العربية" Value="ar"></asp:ListItem>
</asp:DropDownList></td>
</tr>
Code: to handle the language (culture selection) and then return the referrer page
protected void comboSelLanguage_SelectedIndexChanged(object sender, EventArgs e)
{
HttpCookie cookie = new HttpCookie("SelLang");
cookie.Value = comboSelLanguage.SelectedValue;
Response.SetCookie(cookie);
Response.Redirect(Request.UrlReferrer.AbsoluteUri); // this is for the redirecting to the referrer page
}
3-Build a BaseClass under the App_Code\BL forlder in your project ,that all pages will inherit from.
Code:
using System;
using System.Data;
using System.Configuration;
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;
using System.Threading;
using System.Globalization;
/// <summary>
/// Summary description for BasePage
/// </summary>
public class BasePage :Page
{
public BasePage()
{
//
// TODO: Add constructor logic here
//
}
protected override void InitializeCulture()
{
string lang = string.Empty;//default to the invariant culture
HttpCookie cookie = Request.Cookies["SelLang"];
if (cookie != null && cookie.Value != null)
{
lang = cookie.Value;
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(lang);
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang);
}
base.InitializeCulture();
}
}
4-Build the web pages [the will be in the content place holder in the master page] and generate local resources, and DON’T FORGET INHERIT FROM THE BASEPAGE CLASS ;), and add your local resources for them.
My Issues is that some controls doesn’t render to RTL well , I don’t know why and what’s going to drive me nuts is the same control (ex: asp:Label) renders sometimes right and sometimes wrong .. I think there is a web design consideration that should be taken or layout considerations to support both RTL and LTR languages; I hope any one can help me in this area
Hope this helps
gotchi
Member
133 Points
39 Posts
Re: How to do a language switch in a master page.
Jul 18, 2006 08:20 AM|LINK
hi
first I want to say that I prefer the global.asax solution - it's much easier for locaolize an homepage which already exists. also microsoft means that editing the global.asax is a goot way to make changes for a whole website.
but it doesn't matter, my problem is a little bit different
set the uiculture and co to auto and starting my browser with different language settings let everything work fine, especially the Convert.ToDateTime(Textbox1.Text); works fine.
But if I do a language switch with a drop down box like mentioned in the post - I get an exception when my code tries to convert the same textbox with the localized date.
so whats the main problem hier - letting the browser set the language everything looks the same on the frontend then setting the language by hand. only problem is - when I set the language by hand the convert.todatetime throws an exception.
caos
Member
45 Points
9 Posts
Re: How to do a language switch in a master page.
Jul 31, 2006 09:55 AM|LINK
Hello, could you explain this step in detail please.
4-Build the web pages [the will be in the content place holder in the master page] and generate local resources, and DON’T FORGET INHERIT FROM THE BASEPAGE CLASS ;), and add your local resources for them.
Thank you!
EngMotagaly
Member
52 Points
10 Posts
Re: How to do a language switch in a master page.
Jul 31, 2006 10:24 AM|LINK
well i've mentioned before that you should create a helper class, that all your webpages will inherit from to execute the code in the InitializeCulture() in all tour pages without having to override this mehtod in every webpage code
"3-Build a BaseClass under the App_Code\ forlder in your project ,that all pages will inherit from :
using System;
using System.Data;
using System.Configuration;
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;
using System.Threading;
using System.Globalization;
/// <summary>
/// Summary description for BasePage
/// </summary>
public class BasePage :Page
{
public BasePage()
{
//
// TODO: Add constructor logic here
//
}
protected override void InitializeCulture()
{
string lang = string.Empty;//default to the invariant culture
HttpCookie cookie = Request.Cookies["SelLang"];
if (cookie != null && cookie.Value != null)
{
lang = cookie.Value;
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(lang);
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang);
}
base.InitializeCulture();
}
}"
the bold text was just a hint not to forget :) to do that for very web page:
public
partial class [PageClassName] : BasePagecharo
Member
7 Points
2 Posts
Re: How to do a language switch in a master page.
Dec 01, 2006 10:12 AM|LINK
Thak you for this forum, it has helped mi so much!!![:D] I've used
EngMotagaly 's solution and it works fine, but i will need to introduce some changes, because my applicattion will execute in two servers and because of that i won't be able to use cookies.
Mi code in the Master Page is:
ViewState["Idiom"]= lang;
Mi code in the class BasePage is like this:
string lang= (string)ViewState["Idiom"];
And then the code is the same as EngMotagaly, but it doesn't work and i don't know if i need to enable viewState somewhere (i'm beginning whit asp.net). Any ideas?
Than you so much and sorry for my english (i'm spanish)
a4nsd
Participant
1124 Points
299 Posts
Re: How to do a language switch in a master page.
Dec 14, 2006 05:02 PM|LINK
Hello rmprimo,
I use your code to apply my page. But there is an error
"Value cannot be null.
Parameter name: name"
Error occurs at line
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(lang);I don't know why. Can you show me how...Thanks very much
a4nsd
Participant
1124 Points
299 Posts
Re: How to do a language switch in a master page.
Dec 14, 2006 05:20 PM|LINK
charo
Member
7 Points
2 Posts
Re: How to do a language switch in a master page.
Dec 20, 2006 10:52 AM|LINK