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
Solved

Resx Language Problem

Posted on 2010-08-24
10
753 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.NET 2008 - Windows Protected Your PC 7 41
vb.net winforms sizing/resolution? 4 42
Help with error in Query 2 31
How to filter by key press ? 6 19
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

856 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