Solved

Resx Language Problem

Posted on 2010-08-24
10
755 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

728 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