Create a new class and derive it from System.Web.UI.Page. In this class override InitializeCulture(). Then derive all your other Pages from this new class. For example:
public partial class localizedPage : System.Web.UI.Page
{
protected override void InitializeCulture()
{
....
}
.....
}
Imports Microsoft.VisualBasic
Imports System.Threading
Imports System.Globalization
Public Class localizedPage
Inherits System.Web.UI.Page
Protected Overrides Sub InitializeCulture()
Dim UserCulture As String = Profile.GetPropertyValue("PreferredCulture").ToString()
If UserCulture <> "" Then
Thread.CurrentThread.CurrentUICulture = New CultureInfo(UserCulture)
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(UserCulture)
End If
End Sub
End Class
and this is the Error: 'GetPropertyValue' is not a member of 'Profile'
please note the italic part is exaclty what I have in every and each page (working...) and would like not to repeat but to derive once for all. Where do I miss?
This is what I do. I use a property named "Language" to store the users preference. Place this code in your new class (localizedPage) and derive all the other pages from this class. Should work.
Public Class localizedPage
Protected Overloads Overrides Sub InitializeCulture()
Dim cCulture As String = ""
Dim objProfile As ProfileCommon = CType(HttpContext.Current.Profile, ProfileCommon)
If Not (objProfile.Language Is Nothing) Then
cCulture = objProfile.Language
End If
If cCulture.Length > 0 Then
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture(cCulture)
System.Threading.Thread.CurrentThread.CurrentUICulture = New System.Globalization.CultureInfo(cCulture)
End If
MyBase.InitializeCulture()
End Sub
End Class
Errors:
1) sub initializeculture cannot be declared ovverrides because it does not override a sub in a base class
2) language is not a member of profilecommon
3) initializecluture is not a member of objects
Public Class localizedPage
Protected Overloads Overrides Sub InitializeCulture()
Dim cCulture As String = ""
Dim objProfile As ProfileCommon = CType(HttpContext.Current.Profile, ProfileCommon)
If Not (objProfile.Language Is Nothing) Then
cCulture = objProfile.Language
End If
If cCulture.Length > 0 Then
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture(cCulture)
System.Threading.Thread.CurrentThread.CurrentUICulture = New System.Globalization.CultureInfo(cCulture)
End If
MyBase.InitializeCulture()
End Sub
End Class
Errors:
1) sub initializeculture cannot be declared ovverrides because it does not override a sub in a base class
2) language is not a member of profilecommon
3) initializecluture is not a member of objects
Create a new class and derive it from System.Web.UI.Page. In this class override InitializeCulture(). Then derive all your other Pages from this new class. For example:
public partial class localizedPage : System.Web.UI.Page
{
protected override void InitializeCulture()
{
....
}
.....
}
public partial class myPage : localizedPage
{
...
}
This only works if the page that sets the profile is dedicated. In order to set the profile you need some sort of an input control. This page will then be ALWAYS ONE CULTURE BEHIND, in the history of changing cultures. It is kind of ok, it
is just one page exception to the rule. This one page can be done DIRECTLY, via the Request.Form collection as described inthis
video demo, just set the profile too.
It is not the form of persistence(you chose the profile, which is fine) but the fact that InitializeCulture() occurs early on when there are no controls yet, so whichever the persistence medium, it would be reading yesterdays news.
You can build the dropwdown into the OO hierarchy. Its(any control's) members are not available yet for the InitializeCultue method, so you need to use the Request.Form collection. This is a real coupled solution though and limited as to UI
templating.
Putting the dropdown to be in the MasterPage. Then every page becomes the exception to the rule.
See the links I pointed to for a solution.
Another way to solve this is a design with iframes(frown, frown). Then the parent page with the dropdwon set the cullture directly. The child page loads properly because that request occurs after the parent. The child page can use OO and of the ways of persistence. As
a matter of fact I use complex navigational system and not to repost it, I keep it in the parent page. Frames are a huuuge hassle though, and far from best practices. MSDN uses frames because the navigational system requires it, but they are not for the faint
at heart.
Bottom line what you are asking for is not simple but doable. Under all scenarios it is either coupled or brittle, which is why Microsoft didn't offer it as a canned feature. Microsoft pushes the "AUTO" feature which essentially persists in a browser setting
which is always available early on, and if switched, it occurs BEFORE the request, as it is done in the browser GUI and not even the page itself. We all agree "auto" is the lamest of all, until they expose the browser setting to scripting, which will be NEVER,
for security reasons.
I agree, and this is what I do. I have got a dropdown control for choosing the language in my masterpage. When the selection index is changed, then in set the new profile value and reload the page. Sorry, I forgot to mention this...
lucasp
Member
100 Points
20 Posts
declaring Protected Overrides Sub InitializeCulture() once for all
Feb 08, 2006 06:23 PM|LINK
Is there any alternative solution to declaring same "Protected Overrides Sub InitializeCulture()" in every page of a globalized website?
Flo1973
Member
55 Points
15 Posts
Re: declaring Protected Overrides Sub InitializeCulture() once for all
Feb 17, 2006 06:02 AM|LINK
Create a new class and derive it from System.Web.UI.Page. In this class override InitializeCulture(). Then derive all your other Pages from this new class. For example:
public partial class localizedPage : System.Web.UI.Page
{
protected override void InitializeCulture()
{
....
}
.....
}
public partial class myPage : localizedPage
{
...
}
lucasp
Member
100 Points
20 Posts
Re: declaring Protected Overrides Sub InitializeCulture() once for all
Mar 07, 2006 04:32 PM|LINK
I cannot have this working..
this is the new class I created:
Imports Microsoft.VisualBasic
Imports System.Threading
Imports System.Globalization
Public Class localizedPage
Inherits System.Web.UI.Page
Protected Overrides Sub InitializeCulture()
Dim UserCulture As String = Profile.GetPropertyValue("PreferredCulture").ToString()
If UserCulture <> "" Then
Thread.CurrentThread.CurrentUICulture = New CultureInfo(UserCulture)
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(UserCulture)
End If
End Sub
End Class
and this is the Error:
'GetPropertyValue' is not a member of 'Profile'
please note the italic part is exaclty what I have in every and each page (working...) and would like not to repeat but to derive once for all. Where do I miss?
rmprimo
Contributor
3639 Points
738 Posts
Re: declaring Protected Overrides Sub InitializeCulture() once for all
Mar 08, 2006 03:58 AM|LINK
check these out
http://forums.asp.net/1219973/ShowPost.aspx
http://forums.asp.net/1219954/ShowPost.aspx
http://forums.asp.net/1199336/ShowPost.aspx
HTH!
Rob
Flo1973
Member
55 Points
15 Posts
Re: declaring Protected Overrides Sub InitializeCulture() once for all
Mar 08, 2006 06:16 AM|LINK
Hi.
This is what I do. I use a property named "Language" to store the users preference. Place this code in your new class (localizedPage) and derive all the other pages from this class. Should work.
protected
override void InitializeCulture(){
string cCulture = ""; ProfileCommon objProfile = (ProfileCommon)HttpContext.Current.Profile;if(objProfile.Language != null)
cCulture = objProfile.Language;
if
(cCulture.Length > 0){
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture(cCulture);
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(cCulture);
} base.InitializeCulture();
}
lucasp
Member
100 Points
20 Posts
Re: declaring Protected Overrides Sub InitializeCulture() once for all
Mar 08, 2006 09:59 AM|LINK
Imports Microsoft.VisualBasic
Imports System.Threading
Imports System.Globalization
Public Class localizedPage
Protected Overloads Overrides Sub InitializeCulture()
Dim cCulture As String = ""
Dim objProfile As ProfileCommon = CType(HttpContext.Current.Profile, ProfileCommon)
If Not (objProfile.Language Is Nothing) Then
cCulture = objProfile.Language
End If
If cCulture.Length > 0 Then
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture(cCulture)
System.Threading.Thread.CurrentThread.CurrentUICulture = New System.Globalization.CultureInfo(cCulture)
End If
MyBase.InitializeCulture()
End Sub
End Class
Errors:
1) sub initializeculture cannot be declared ovverrides because it does not override a sub in a base class
2) language is not a member of profilecommon
3) initializecluture is not a member of objects
Hi Rob,
I would prefer not to rely on cookies
regards
Luca
lucasp
Member
100 Points
20 Posts
Re: declaring Protected Overrides Sub InitializeCulture() once for all
Mar 08, 2006 10:51 AM|LINK
Imports Microsoft.VisualBasic
Imports System.Threading
Imports System.Globalization
Public Class localizedPage
Protected Overloads Overrides Sub InitializeCulture()
Dim cCulture As String = ""
Dim objProfile As ProfileCommon = CType(HttpContext.Current.Profile, ProfileCommon)
If Not (objProfile.Language Is Nothing) Then
cCulture = objProfile.Language
End If
If cCulture.Length > 0 Then
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture(cCulture)
System.Threading.Thread.CurrentThread.CurrentUICulture = New System.Globalization.CultureInfo(cCulture)
End If
MyBase.InitializeCulture()
End Sub
End Class
Errors:
1) sub initializeculture cannot be declared ovverrides because it does not override a sub in a base class
2) language is not a member of profilecommon
3) initializecluture is not a member of objects
Hi Rob,
I would prefer not to rely on cookies
regards
Luca
rmprimo
Contributor
3639 Points
738 Posts
Re: declaring Protected Overrides Sub InitializeCulture() once for all
Mar 08, 2006 11:00 AM|LINK
Create a new class and derive it from System.Web.UI.Page. In this class override InitializeCulture(). Then derive all your other Pages from this new class. For example:
public partial class localizedPage : System.Web.UI.Page
{
protected override void InitializeCulture()
{
....
}
.....
}
public partial class myPage : localizedPage
{
...
}
This only works if the page that sets the profile is dedicated. In order to set the profile you need some sort of an input control. This page will then be ALWAYS ONE CULTURE BEHIND, in the history of changing cultures. It is kind of ok, it is just one page exception to the rule. This one page can be done DIRECTLY, via the Request.Form collection as described in this video demo, just set the profile too.
It is not the form of persistence(you chose the profile, which is fine) but the fact that InitializeCulture() occurs early on when there are no controls yet, so whichever the persistence medium, it would be reading yesterdays news.
You can build the dropwdown into the OO hierarchy. Its(any control's) members are not available yet for the InitializeCultue method, so you need to use the Request.Form collection. This is a real coupled solution though and limited as to UI templating.
Putting the dropdown to be in the MasterPage. Then every page becomes the exception to the rule. See the links I pointed to for a solution.
Another way to solve this is a design with iframes(frown, frown). Then the parent page with the dropdwon set the cullture directly. The child page loads properly because that request occurs after the parent. The child page can use OO and of the ways of persistence. As a matter of fact I use complex navigational system and not to repost it, I keep it in the parent page. Frames are a huuuge hassle though, and far from best practices. MSDN uses frames because the navigational system requires it, but they are not for the faint at heart.
Bottom line what you are asking for is not simple but doable. Under all scenarios it is either coupled or brittle, which is why Microsoft didn't offer it as a canned feature. Microsoft pushes the "AUTO" feature which essentially persists in a browser setting which is always available early on, and if switched, it occurs BEFORE the request, as it is done in the browser GUI and not even the page itself. We all agree "auto" is the lamest of all, until they expose the browser setting to scripting, which will be NEVER, for security reasons.
Rob
rmprimo
Contributor
3639 Points
738 Posts
Re: declaring Protected Overrides Sub InitializeCulture() once for all
Mar 08, 2006 11:15 AM|LINK
Luca, it is not the cookies. You can use any of the persistence forms you like.
BTW, what do you think profile uses by default? Last I looked, http is still disconnected so you still need to identify yourself on every request.
Look for the stats for users that accept cookies? Virtually 100% :)
Rob
Flo1973
Member
55 Points
15 Posts
Re: declaring Protected Overrides Sub InitializeCulture() once for all
Mar 08, 2006 11:31 AM|LINK
Hi Rob.
I agree, and this is what I do. I have got a dropdown control for choosing the language in my masterpage. When the selection index is changed, then in set the new profile value and reload the page. Sorry, I forgot to mention this...
Regards, Florian