1

I have a web application that simply displays a form User Control. I have generated my resx files associated with the User Control by going to the design of the User Control and then clicking Tools > Generate Local Resource

Doing this has created the necessary resx file:

App_LocalResources
  ContactUsForm.ascx.resx

I then simply copy and pasted the existing resx file and renamed it so I now have the following:

App_LocalResources
  ContactUsForm.ascx.fr-FR.resx
  ContactUsForm.ascx.resx

As a test I modified a couple of the dotnet controls text within the new fr-FR resource file just to test whether it was all working as expected and it is not.

I use the following code to set the specific culture information based on a querystring parameter:

protected void Page_Init(object sender, EventArgs e)
{
    string cultureName = Request.QueryString["lang"];
    if (!string.IsNullOrEmpty(cultureName))
    {
        Thread.CurrentThread.CurrentCulture = new CultureInfo(cultureName);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(cultureName);
    }
}

I can then simply navigate to the page housing the user control by doing the following:

http://localhost/Default.aspx?lang=fr-FR

I can confirm the culture information is being set by executing the following code:

CultureInfo currentCultureUI = Thread.CurrentThread.CurrentUICulture;
CultureInfo currentCulture = Thread.CurrentThread.CurrentUICulture;

I can confirm that the set culture is in fact fr-FR however the text is not being replaced as per the changes within the ContactUsForm.ascx.fr-FR.resx file.

I can't seem to get this working as expected. What am I doing wrong and is there any other way to achieve the desired result?

Thanks in advance.

UPDATE:

If I do the following in code it works just fine:

lHeadingResource1.Text = GetLocalResourceObject("lHeadingResource1.Text").ToString();

It's actually reading the correct string from the ContactUsForm.ascx.fr-FR.resx file.

I can also confirm that my controls have all the necessary meta:resourcekey references as well.

It's just strange that if I don't set it in code it defaults back to the default culture and doesn't use the fr-FR specified above.

2 Answers 2

1

You should set the culture by overriding the Page's InitializeCulture method.

protected override void InitializeCulture()
{
    if (Request.Form["DropDownList1"] != null)
    {
        //define the language
        UICulture = Request.Form["DropDownList1"];
        //define the formatting (requires a specific culture)
        Culture = Request.Form["DropDownList1"];
    }
    base.InitializeCulture();
}

The page here http://blogs.spanlogic.com/clyap/post/2009/05/29/Programmatically-change-Culture-of-User-Control-(ascx).aspx suggests you can use FrameworkInitialize inside a user control. I haven't tested this tho.

    protected override void FrameworkInitialize()
    {
        base.FrameworkInitialize();
    }
Sign up to request clarification or add additional context in comments.

2 Comments

I suspect any code in the user control will execute too late in the page life cycle to correctly load the resource file. InitializeCulture runs before Page_Init. Both of which are before any events are fired on the user control.
protected override void FrameworkInitialize() did the trick. Thank you.
1

Try this You have to implement your logic with InitializeCulture()

<%@ Page Language="C#" uiculture="auto" %>
<%@ Import Namespace="System.Threading" %>
<%@ Import Namespace="System.Globalization" %>
<script runat="server">

    protected override void InitializeCulture()
    {
        if (Request.Form["ListBox1"] != null)
        {
            String selectedLanguage = Request.Form["ListBox1"];
            UICulture = selectedLanguage ;
            Culture = selectedLanguage ;

            Thread.CurrentThread.CurrentCulture = 
                CultureInfo.CreateSpecificCulture(selectedLanguage);
            Thread.CurrentThread.CurrentUICulture = new 
                CultureInfo(selectedLanguage);
        }
        base.InitializeCulture();
    }
</script>

For More Info Refer Below Link http://msdn.microsoft.com/en-us/library/bz9tc508(v=vs.80).aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.