Link to home
Start Free TrialLog in
Avatar of jonathangodwin
jonathangodwin

asked on

Resx Language Problem

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
ASKER CERTIFIED SOLUTION
Avatar of Sunny_Kumar
Sunny_Kumar

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sunny_Kumar
Sunny_Kumar

Did the above help?
Avatar of jonathangodwin

ASKER

Thanks for the suggestion, I've not had time to try it yet but will today. WIll get back to you
Thumbs up
Great stuff! Best of luck with the project!
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
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)
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
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"
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!