Override culture when using GetGlobalResourceObject or GetLocalResourceObject in ASP.NET - asp.net

I've implemented localization in this ASP.NET project using resource files and that's working as expected.
But there is one situation where I have to display the whole page in one language and then a section of it in some other language based on the selection from a dropdown list.
From what I can see, the GetGlobalResourceObject() and GetLocalResourceObject() methods work from the page culture, but I need to adjust the language programmatically. Is there a way to override those methods' behaviour so that I can specify the language?

I believe you can access a specific resx file by using this method:
value = HttpContext.GetGlobalResourceObject(classKey, resourceKey, culture)
You can initialize your culture like this:
Culture culture = CultureInfo.CreateSpecificCulture("pt-PT");
Just pick up your value from the dropdown and switch the culture in the CreateSpecificCulture method.
I havent tested it, let me know if that doesnt work.
EDIT: its the same for GetLocalResourceObject

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.

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

How to achieve multilingual support in ASP.NET

I want to internationalize my asp.net application. How to do this? What steps exactly do I have to follow?
1.) If you use database, then you must modify your tables. At least with adding the LCID column.
2.) Set default culture and UI culture in web.config
<system.web>
<globalization culture="cs-CZ" uiCulture="cs-CZ"/>
</system.web>
3.) Then you can set actual thread culture either in global.asax in e.g. BeginRequest event, or in base class of your page classes in InitializeCulture method
protected override void InitializeCulture()
{
string language = Request["lang"];
if (!string.IsNullOrEmpty(language))
{
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(language);
System.Threading.Thread.CurrentThread.CurrentUICulture = System.Threading.Thread.CurrentThread.CurrentCulture;
}
else
{
base.InitializeCulture();
}
}
For static texts you can use Resources. E.g. you create Mytexts.resx where you write texts for default laguage (en-us) and then you create Mytexts.en-UK.resx for uk english and overwrite text that are different from default laguage. Then you can insert this strings in your page :
<asp:Label runat="server" Text='<%$ Resources: Mytests,WelcomeMessage %>' />
This are only briefly steps for beginning with localization, but for small pages / apllications is it sufficient.
Simply make a basepage class that will inherited from Page class, put this method in basepage class and inherit basepage class in your every aspx.cs page to acheive globalization.
protected override void InitializeCulture()
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
base.InitializeCulture();
}
set in this method whatever culuture you want, like ar-sa for arabic....
You have to explore the topics of using resource files in your web application. If you need database based localization support you may try the excellent free product from westwind
Localization and Globalization topics in MSDN are your best bet for this.
It's a pretty big question to be able to give you the exact steps, and there are several different approaches.
The approach we took on my most recent project (simplified) was:
Set up a domain for each country
Create a resource file for all the
hard-coded strings (form labels etc)
for each culture (en-US, de-DE,
fr-FR)
Change the Thread.CurrentCulture based on the domain the site is
being accessed from - this means
that all your number formats, date
formats will be correct and use the
correct localised resource file
Hope this helps!
See here for the Microsoft white papers on Internationalization.
This is a complex topic and requires a lot of work to get right, through all layers of your system.
Start here.
ASP.NET resx files which will let you configure constant strings easily, but your DB will also need to support unicode, and you'll need to do different things depending on the languages you wish to support.
Good luck, and ask questions when you have specific problems.
See this link on creating localized resource files: http://msdn.microsoft.com/en-us/library/ms247246.aspx
Basically you create a new resource file each language/culture you want to support. Then you access the strings inside them by name in your markup pages and code behind files.
Additionally these resource files need to be in a specific folder in your project called: App_GlobalResources
Global resource files must be in the App_GlobalResources folder. If you
try to create a .resx file outside of
this folder, Visual Web Developer
prompts you to create it in the
folder.

ClientID invalid after moving code into DotNetNuke module

I am porting an existing application to a DotNetNuke module and discovered bizarre behavior. I use ClientID when creating javascript so that the code can identify the HTML elements. Normally that creates a value something like this:
"g_ctl00_ctl01_ctl00_ctl00_txtSearch"
We've all seen this a million times, right? Well, after porting this code to a DotNetNuke module and running it for the first time, the ClientID property is returning this:
"dnn_ctr397_GalleryServerPro.Web.Gallery, TechInfoSystems_ctl00_ctl01_ctl00_ctl00_txtSearch"
Notice the comma and the space. This is causing all kinds of javascript errors. For example, the ASP.NET Login control is now outputting invalid javascript:
var dnn_ctr397_GalleryServerPro.Web.Gallery, TechInfoSystems_ctl00_ctl01_ctl01_lv_ctl02_Login1_UserNameRequired = document.all ? document.all["dnn_ctr397_GalleryServerPro.Web.Gallery, TechInfoSystems_ctl00_ctl01_ctl01_lv_ctl02_Login1_UserNameRequired"] : document.getElementById("dnn_ctr397_GalleryServerPro.Web.Gallery, TechInfoSystems_ctl00_ctl01_ctl01_lv_ctl02_Login1_UserNameRequired");
My module has an assembly name of TechInfoSystems.GalleryServerPro.dll, the default namespace is GalleryServerPro.Web, and the user control is in a class named Gallery.cs, so that partially explains where some of that extra text is coming from, but why is it there? And what can I do to ensure that ClientID does not output commas, spaces, or other characters that can cause problems in javascript?
Thanks,
Roger
I (sort of) figured it out. I needed to add an ID to the user control that is being added to the DotNetNuke page. This is the main view user control - the one that inherits PortalModuleBase. The user control is contained entirely in a code-behind class (no .ascx), so adding this to the Init event fixed everything:
this.ID = "gsp";
My theory is that the issue happens when these are true:
The "view" user control uses only the code-behind file (no .ascx file)
The user control does not have an ID specified.
When registering a module without the .ascx, one must specify the class and assembly in the Module Definitions. In my case, it is "GalleryServerPro.Web.Gallery, TechInfoSystems.GalleryServerPro". That looks very close to the text DNN ends up inserting into the clientID string. DNN (or ASP.NET) must be taking that string and using it to build the clientID in the absence of an ID assigned in the user control.
Not sure if this is a DNN bug, but I am happy to figure it out and now can move on...
You could try referencing your control using a class instead?
There are plenty of good functions out there for doing such a thing. I would recommend jquery for a start, to make all things javascript a world easier.
I have NO idea why this is happening (but then I've never used DNN)
what you could do is take control of the ClientID yourself for the affected servercontrols, for instance, it looks like you're using a TextBox control with server ID = txtSearch, what you could do is make a class that inherits from TextBox and setting its ClientId to be equal to the Server ID (assuming there's only one control called txtSearch)
see here
It might be that DNN would override even that, but it might be worth a shot

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