last point in page pipeline to change culture? - asp.net

What is the last point in the asp.net page loading pipeline that I can change the culture of a page by doing the following?
Thread.CurrentThread.CurrentCulture = << new culture >>;
Thread.CurrentThread.CurrentUICulture = << new culture >>;
I am changing the culture in my code and want to know at what point is the last point in which I can change the pages culture so that the correct resource files etc are picked up.
Is the Page PreInit too late in the pipeline to change the culture? I am aware there is an InitialiseCulture method in the Page class but I am working outside of this.

As per MSDN documentation, correct way is to use InitialiseCulture - it gets called very early in page life-cycle before even controls are created. And that is even before PreInit event.
Said that, people have set the culture information as late as Page_Load event. For example, see this KB article or this code project article. So I guess that PreInit event should be ok.
There are two relevant properties - Culture and UICulture. AFAIK, UICulture is used for loading correct local (page specific)/global resources and that would be done at rendering stage - so should not be an issue. The culture info from thread get used by many frameworks methods and you need to be careful using any code that is dependent on the culture information before you sets the culture in page life cycle - example of such code can be formatting of data or parsing from request data etc.

See my comment to your original post too.
protected override void InitializeCulture()
{
UICulture = "en";
Culture = "en-US";
}
http://msdn.microsoft.com/en-us/library/bz9tc508.aspx

Related

Creating a multilingual site in asp.net

I am creating a website that will be multilingual in nature.
I am providing a functionality whereby the user can select desired language by selecting it from the drop down.
Now my problem starts here (when a language is selected from the drop down). I am currently implementing 2 languages English and Arabic.
Problem is when the user select Arabic from the drop down on the login page the page is refreshed and the browser loads all the content in Arabic.
But...
When i select English back again, the page refreshes but the language content does not change !!
i have check the code and the values (culture name value) are being applied correctly!!
Any clues as to what is wrong and where...
Here is my code...
protected override void InitializeCulture()
{
String selectedLanguage = string.Empty;
if (Request.Form["ddlLanguage"] != null)
{
selectedLanguage = Request.Form["ddlLanguage"];
CultureInfo ci = new CultureInfo(selectedLanguage);
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;
}
base.InitializeCulture();
}
Any help will be great !!
Thanks
The Culture settings must set on each request. It's not enough to set the Thread cultures once when the selection changes.
In order to set the culture according to the user selection on each request, there are several possible ways.
Set the culture in (for instance) the Page_Init event of the master page.
Create custom base page (maybe MyBasePage) for the content pages and override the InitializeCulture method of that class. Then make all the content pages derive from the class instead of directly from Page.
Add code for setting the culture in a suitable event in Global.asax.
Probably there are several other possible ways to handle this as well.
Regardless of which method you will use, the culture that the user has chosen must be available to the code that is going to set the culture on the thread. So when the user changes his/her selection, you must save that selection in a place where you can access it in the coming requests. This can also be solved in several possible ways. Here are some options:
If you are using a ProfileProvider, you could save the selection to the user's profile.
Alternatively you can store it in a cookie.
You could possibly store it in the Session although that would mean that the user must re-select it whenever the Session has been reset.
You could use some other custom way to store the selection (database, some file on disk etc).
For a more detailed exmple of how this could be done using Global.asax and a cookie, have a look over here.
Check this article - How to create ASP.NET MVC multilingual web application ?
We will see mainly two approaches-
Approach 1: Using Static Pages
Approach 2: Using Dynamic page with localized data at runtime

Setting CurrentCulture from DropnDown in MasterPage

What is the best practice to set the CurrentThread.CurrentUICulture from a DrownDownList in a MasterPage?
I don't want to override InitializeCulture() in every page.
Could it be stored in a Session variable and set in a HttpModule or HttpHandler?
An additional problem i ran into is that if there are databound language-dependant controls on the page they will need to be databound after the language has changed.
Thanks.
I have most often chosen to create a pase page class and override InitializeCulture() there. All pages inherit from the base page so all logic is encapsulated in the base page. This works for me because I use the querystring to pass language preference changes.
InitializeCulture() has logic to check for querystring argument, existing session value, cookie preference, defaulting to the browser's current culture.
I suppose you could do a redirect in the masterpage dropdown change event.
I store the culture in a cookie and then set it in the Application_BeginRequest of the global.asax. When you first set it though you have to Server.Transfer(Request.Url.LocalPath) because the selectedindex changed event fires too late in the lifecycle to take affect. Not sure if its the best but it works for me.

How do I set a custom culture before the page is rendered?

I have created a custom culture for client-specific language, i.e. I have resx files for that culture.
The issue I have is that I can't seem to set the culture early enough in the page cycle. By the time I call Thread.CurrentThread.CurrentUICulture = culture and Thread.CurrentThread.CurrentCulture = culture the page seems to already have picked the language from the base resx file :(
So I'm having to set the culture and then redirect back to the same page?
I've tried setting it in the Page_PreInit and its still not early enough? Is there an earlier event I can hook into?
You need to override the page's InitializeCulture() method, look here for details.

ASP.NET Localizations and User definded language

I'm working on a asp.net web application and one of the requirements is that the user has to be able to select the language they want. I"m using Resx files to store the locals. What my question is do I need to change the CurrentCulture of the thread every time a page is loaded or is there a way to have it handled automatically when a logged in user moves from one page to the next.
Yes, I believe you need to set it every time. To make it even worse, you have to do it by overriding the InitializeCulture method of the Page class. I created a SitePage that all pages in my project inherit from instead of Page to do this.
public class SitePage : Page
{
protected override void InitializeCulture()
{
base.InitializeCulture();
// Set both the CurrentCulture (for currency, date, etc) conversion, and the CurrentUICulture for resource file lookup.
Thread.CurrentThread.CurrentCulture = whatever;
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
}
}
Further reading: http://msdn.microsoft.com/en-us/library/bz9tc508.aspx

Best way to implement languages menu in an ASP.NET application

I trying to implement a typical languages menu where users can select the language they want to view the site in through a menu that appears throughout all pages in the site.
The menu will appear on multiple master pages (currently one for pages where users are logged in and one for pages where users are not).
My current implementation is having a single master page base class (let's call it MasterBase). MasterBase has an event
public event LanguageChangedEventHandler LanguageChanged;
where LanguagedChangedEventHandler is simply defined as
public delegate void LanguageChangedEventHandler(string NewCulture);
MasterBase also has an overridable method
protected virtual void OnLanguageChanged(string NewCulture)
which just basically fires the event.
Each master page that inherits MasterBase overrides OnLanguageChanged and does the usual stuff like set the Thread's CurrentUICulture and the language cookie then does a
Server.Transfer(this.Page.AppRelativeVirtualPath, true);
to get the page to reload with localized values for the new culture. On the master page for logged in users it also updates the user's language pref in the db.
Each language option is currently a LinkButton on a master page that inherits from MasterBase. When the link is clicked it calls the base OnLanguagedChanged method passing in the correct culture info. E.g.
protected void btnEnglish_Click(object sender, EventArgs e) {
this.OnLanguageChanged("en-US");
}
Each page that needs to handle a language change then has some code in the page load that looks like...
((MasterBase)this.Master).LanguageChanged += this.Page_OnLanguageChanged;
// Where Page_OnLanguageChanged has the signature of LanguageChangedEventHandler
// and has stuff like reloading options in a drop down using the new language.
Quite a convoluted 'framework' =)
Firstly it's hard for new developers to know they have to hook up a method to the MasterBase's LanguageChanged event to handle language changes. Yes, we do document it. But still it's not something straightforward and obvious.
Secondly, all language changes are post backs. This is problematic especially when you want to navigate back with the browser Back button.
I'm looking for a more elegant solution. One that doesn't have both the problems above and also handles my current requirements.
Greatly appreciate any suggestions. Thanks.
It seems to me that it would be better to implement this in a control that sets an application variable that all pages could use. That way you could just implement the code in one place and have it always available on each page that displays the control (could be in your master's so all pages that inherit get it automatically). I think in the control you would have a handler that sets the global language setting and then reloads the page. Each page would check the language setting during page_load or prerender and load the proper localized strings accordingly.
I would just use the PreInit event on base page to set the current ui culture. I am not clear on why you would need each page to know when language is changed.

Resources