Solved

Resx Language Problem

Posted on 2010-08-24
10
744 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
Comment Utility
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
Comment Utility
Did the above help?
0
 
LVL 1

Author Comment

by:jonathangodwin
Comment Utility
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
Comment Utility
Thumbs up
0
 
LVL 1

Expert Comment

by:Sunny_Kumar
Comment Utility
Great stuff! Best of luck with the project!
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 1

Author Comment

by:jonathangodwin
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

763 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

11 Experts available now in Live!

Get 1:1 Help Now