Creating a multilingual site in asp.net - 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

Related

How to Override CurrentThread.CurrentCulture for an application

I have sharepoint application, that needs to be made support Globalization(multi language support).
I am planning have a drop down box with list of languages.
Problem: By Default the CurrentCulture is en-US as expected.Lets say the if user choose some other language(chinese) from the dropbox then I need to set CurrentCulture to chinese so that it can access corresponding resource xml file.
I tried overriding the currentculture based on user selection.But it is not getting effected for all the threads.
I tried setting in web.config even that it don't work.
Please suggest how to change the CurrentCulture and CurrentUICulture for entire application(threads) based on user selected language.
Regards,
Archu
Have you tried doing it the Sharepoint way:
http://www.denisstadler.com/sharepoint-2010/sharepoint-2010-publishing-feature/set-up-multilingual-support-in-sharepoint-2010/
If you really need to have control on the culture of the thread you can set them like this:
System.Threading.Thread.CurrentThread.CurrentUICulture =
New CultureInfo("de")
System.Threading.Thread.CurrentThread.CurrentCulture =
New CultureInfo("de-DE")
But only of you are spawning the threads yourself.

The best solution to customize page controls based on some roles and settings

I have several pages in asp.net each with lots of controls. I Also have some roles in my application that each has some setting options. Now I want to prepare my page based on these settings. Maybe it’s not too clear, so please take a look at my example.
Example: There are some buttons, some textboxes, some datetime picker, and a chart in a page, now what I want is when a user sees this page, the controls appear and disappear based on the users role. An important thing is that I don’t want to have only visible and invisible controls, in some scenarios I need to show controls with some customizations. For example change chart data source, limit selecting date time and so on.
The first solution that I can think of, is saving the settings in database and after visiting the page by user, the settings fetch from database and based on those, I can customize the controls with conditional phrases (if and else). But I suppose it is not a good approach and my page will get very messy.
Please help me with any better solutions and if you know good references about it, please let me know.
Please see this link...use of ControlAdapters may help you...
Role-based enabling/disabling of controls in asp.net
You must use Thread.CurrentPrincipal.
A. When user login to your application, you attach his identity to thread, for example
string[] rolesArray = .....; //Get roles from dataBase by identity.
Thread.CurrentPrincipal = new YourCustomPrincipal(new YourCustomIdentity("YouName", "..."), rolesArray);
B. And when you navige about your application you test Thread.CurrentPrincipal
IPrincipal threadPrincipal = Thread.CurrentPrincipal;
if(threadPrincipal.Roles.Contains("roleTest"))
{
//Adjust your control
}

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.

Calling a ASP Page thru it Class

Like in Windows Forms:
Dim myForm as New AForm(Constr-arg1, Constr-arg2)
myForm.Show
... is there a similar way to Load a Page in ASP.Net. I would like to overload the Page Constructor and instantiate the correct Page Contructor depending on the situation.
Can you just link to the page passing parameters in the QueryString (after the ? in the URL) and then use them in the constructor (more likely PageLoad)
I think the best approach here for ASP.NET is to write User Control (*.ascx file) that represents page content, and load different controls based on current situation using Page.LoadControl() method. This solution is flexible enough, because only reference to control is its name. And this approach is much more useful than page constructor overloading as soos as you're not related on strong types, only on controls' names.
This isn't really the "correct" way to redirect to a page in .Net web programming.
Instead, you should call either Request.Redirect("~/newpage.aspx") or Server.Transfer("~/newpage.aspx"). You should then handle the request in the new page's Page_Load handler.
You can pass state between the pages by adding to the query string of the redirected URL (i.e. ~/newpage.aspx?q1=test), or by assiging values to the Session store (i.e Session["q1"] = value).

Resources