Solved

Resx Language Problem

Posted on 2010-08-24
10
749 Views
Last Modified: 2012-05-10
Hi all,

I have my web app set up with asp text labels being fed from .resx files (multiple languages). I can go to browser settings and change language - this works fine. But I'm looking to programmatically set the language from radio buttons. I've read a lot on the subject and am aware that CurrentUICulture can be used, but apparently not for me. I've tried several variations of commands from system.threading and system.globalisation but to no avail.

I'm using VS 2010 and .NET 4.0 - I haven't tried much with the resources manager but would welcome suggestions,

any help appreciated,

Jonathan
0
Comment
Question by:jonathangodwin
  • 6
  • 4
10 Comments
 
LVL 1

Accepted Solution

by:
Sunny_Kumar earned 500 total points
ID: 33511438
This should help you: Its some code that I use

[code]
protected override void InitializeCulture()
        {

        if ((string)Request.Form["__EVENTTARGET"] == "WUC_user_preferences_wuc$Button_Update_Preferences")
            {
                string selectedLanguage;
                selectedLanguage = Request.Form["WUC_user_preferences_wuc$DropDownList_User_Language"];
                UICulture = selectedLanguage;
                Culture = selectedLanguage;

                Session["User_Default_Language_Culture"] = selectedLanguage;
            }

            if (Session["User_Default_Language_Culture"] != null)
            {
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture((string)Session["User_Default_Language_Culture"]);
                Thread.CurrentThread.CurrentUICulture = new CultureInfo((string)Session["User_Default_Language_Culture"]);
            }
           
            base.InitializeCulture();
        }
[/code]

I listen out for a postback caused by button "Button_Update_Preferences" which is inside my user control "WUC_user_preferences_wuc" - hence the "WUC_user_preferences_wuc$Button_Update_Preferences"

Then I get the language selected by the user from the dropdownlist "WUC_user_preferences_wuc$DropDownList_User_Language"

The sessions are there because I store the selected language in a session for each user.

Hope this helps!!
0
 
LVL 1

Expert Comment

by:Sunny_Kumar
ID: 33529074
Did the above help?
0
 
LVL 1

Author Comment

by:jonathangodwin
ID: 33529453
Thanks for the suggestion, I've not had time to try it yet but will today. WIll get back to you
0
 
LVL 1

Author Closing Comment

by:jonathangodwin
ID: 33529908
Thumbs up
0
 
LVL 1

Expert Comment

by:Sunny_Kumar
ID: 33530127
Great stuff! Best of luck with the project!
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:jonathangodwin
ID: 33531058
If I strip the code down to just setting the current UI culture it works fine, but I change it to the following:

        Dim selectedLang As String
        If lstLang.SelectedIndex = 0 Then
            selectedLang = "fr"
        ElseIf lstLang.SelectedIndex = 1 Then
            selectedLang = "ar"
        Else : selectedLang = "en"
        End If

        Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(selectedLang)
        Thread.CurrentThread.CurrentUICulture = New CultureInfo(selectedLang)
        MyBase.InitializeCulture()

where lstLand is a listbox with language options in, I get the error message: Object reference not set to an instance of an object. -- referring to the line: If lstLang.SelectedIndex = 0 Then

Any ideas?

Thanks
0
 
LVL 1

Expert Comment

by:Sunny_Kumar
ID: 33531204
I don't think you're using the correct language codes:

For example, I'm using french and english. I have two resource files:

Default.aspx.fr.resx
Default.aspx.resx

In my dropdown list I have two options: French and English. The corresponding values are:

For Selection "French" - value = "fr-FR"
For Selection "English" - value = "en-IE"

"fr-FR" = Language French, Country France (Remember you have canadian french also, hence the country is needed"
"en-IE" = Language English, Country Ireland (You could have us, uk etc).

I think you need to update

selectedLang = "fr"
to
selectedLang = "fr-FR"

and make sure you have the correct resource files as above.

Generally if ASP.net cannot find the corresponding culture resource file, it just loads the default one (the one without any language postfix in the filename e.g. - Default.aspx.resx)
0
 
LVL 1

Expert Comment

by:Sunny_Kumar
ID: 33531277
The above is worth taking note of but I mis-read your previous post.

I think the problem may be that you are setting the language every time the page loads. Which is correct in theory. However, that means that every time the page loads, you are looking for a value from your dropdownlist lstLang. Unless you're posting this value every single time, you're not going to have an object to refer to.

Try surrounding your code with an If statement?

if (lstLang != null).....
//Change language based on selection
End if
0
 
LVL 1

Author Comment

by:jonathangodwin
ID: 33531572
Oh, so Protected Overrides Sub InitializeCulture() is on page load?

Is it possible to have overriding properties, say, inside a normal Public Sub, or is the above the only way?

What I have is some flags which on their click event, triggers a language change. Is this possible  to do inside the overriding procedure?

For some reason Chrome only accepts "it" whereas IE demands "it-IT"
0
 
LVL 1

Expert Comment

by:Sunny_Kumar
ID: 33531742
With every load of an asp page, whether it be a postback, first page load or an ajax request, the  InitializeCulture() method is called automatically. You do not call this anywhere. All the method does is set the culture.

See the page lifecycle here and the 4th one down in the cycle:
http://3.bp.blogspot.com/_Li2NlT9UdAY/TB8AmvnH2II/AAAAAAAAAEI/ldghqAYPu5Y/s1600/aspnet_page-control-life-cycle.jpg

However, we want to override the standard  InitializeCulture() method, so we code a Protected Overrides Sub InitializeCulture() method. Whatever we place in here sets the culture on every page load.

"What I have is some flags which on their click event, triggers a language change. Is this possible  to do inside the overriding procedure?" - I'm not sure but I think the easiest way to do it is the way I've coded above.

Store the user language in a session (or a cookie, or viewstate). That way you can load the preferred user language from the database when a user signs in (if you have users).

But you need to also listen out from events fired that trigger a language change from the dropdownlist. Like I mentioned above:

 if ((string)Request.Form["__EVENTTARGET"] == "WUC_user_preferences_wuc$Button_Update_Preferences")
            {
.... then load selected value into session

.........

if (Session["User_Default_Language_Culture"] != null)
            {
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture((string)Session["User_Default_Language_Culture"]);
                Thread.CurrentThread.CurrentUICulture = new CultureInfo((string)Session["User_Default_Language_Culture"]);
            }
           
            base.InitializeCulture();
        }

Hope you understand!


0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now