Question

I've created two .resx files - WebResources.resx and WebResources.fr-FR.resx.

Both contain ExceptionMessages_SignupWithCard.

To test I did:

Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
Response.Write("Current Culture is " + CultureInfo.CurrentCulture.EnglishName);
Response.Write("<br>Message is: " + Resources.WebResources.ExceptionMessages_SignupWithCard);

The problem is that I still get the ExceptionMessages_SignupWithCard message from WebResources.resx and not the one from WebResources.fr-FR.resx .

Can someone please assist?

Thanks.

Was it helpful?

Solution

ASP.Net (specifically, the ResourceManager) uses CurrentUICulture to retrieve resources.

Try setting the Thread's CurrentUICulture as well.

CultureInfo french = new CultureInfo("fr-FR");
Thread.CurrentThread.CurrentCulture = french;
Thread.CurrentThread.CurrentUICulture = french;
Response.Write("Current Culture is " + CultureInfo.CurrentCulture.EnglishName);
Response.Write("<br>Message is: " +  
          Resources.WebResources.ExceptionMessages_SignupWithCard);

OTHER TIPS

Try setting the Page.Culture and Page.UICulture instead of the Thread Culture directly.

Since it is a string property, it appears that it wouldn't be very good. BUT, there is a lot of code going on to change not only the Threads Cultures, but the ResourceManagers too.

This seems to be the standard approach and not assigning the Thread's culture.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="LanugageChanger._Default"  meta:resourcekey="PageResource1"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div style="height: 481px">

        <asp:Label ID="Label1" runat="server" Text="Hello" 
            meta:resourcekey="Label1Resource1"></asp:Label>

        <asp:TextBox ID="TextBox1" runat="server" meta:resourcekey="TextBox1Resource1"></asp:TextBox>
        <br />
        <asp:Label ID="Label2" runat="server" Text="Goodbye" 
            meta:resourcekey="Label2Resource1"></asp:Label>

        <asp:TextBox ID="TextBox2" runat="server" meta:resourcekey="TextBox2Resource1"></asp:TextBox>
        <br />
        <asp:Label ID="Label3" runat="server" Text="Welcome" 
            meta:resourcekey="Label3Resource1"></asp:Label>

        <asp:TextBox ID="TextBox3" runat="server" meta:resourcekey="TextBox3Resource1"></asp:TextBox>
        <br />
        <asp:Label ID="Label4" runat="server" Text="Terminate" 
            meta:resourcekey="Label4Resource1"></asp:Label>

        <asp:TextBox ID="TextBox4" runat="server" meta:resourcekey="TextBox4Resource1"></asp:TextBox>
        <br />
        <asp:Label ID="Label5" runat="server" Text="End" 
            meta:resourcekey="Label5Resource1"></asp:Label>

        <asp:TextBox ID="TextBox5" runat="server" meta:resourcekey="TextBox5Resource1"></asp:TextBox>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Add new values" 
            meta:resourcekey="Button1Resource1" onclick="Button1_Click1" />

        <br />
        <asp:Button ID="Button2" runat="server" Text="Change language" 
            meta:resourcekey="Button2Resource1" onclick="Button2_Click" />

        <br />
        <br />
        <asp:Label ID="Label6" runat="server" meta:resourcekey="Label6Resource1" 
            Text="English 1"></asp:Label>
        <br />
        <asp:Label ID="Label7" runat="server" meta:resourcekey="Label7Resource1" 
            Text="English 2"></asp:Label>
        <br />
        <asp:Label ID="Label8" runat="server" meta:resourcekey="Label8Resource1" 
            Text="English 3"></asp:Label>
        <br />
        <asp:Label ID="Label9" runat="server" meta:resourcekey="Label9Resource1" 
            Text="English 4"></asp:Label>
        <br />
        <asp:Label ID="Label10" runat="server" meta:resourcekey="Label10Resource1" 
            Text="Default" />></asp:Label>

        <br />
        <asp:TextBox ID="TextBox6" runat="server" Height="153px" Width="543px"></asp:TextBox>

    </div>
    </form>
</body>
</html>

There are two methods to set the UICulture of an asp.net web page.

Option #1 is that the web browser can adjust the UI Culture (auto), by setting the current Language in the web browser. In FireFox you go the Content tab in Options, and click Languages, then add the French language and move it to the top of the list. IE is similar, it has a Languages button in the Options pages, and you add French and move it to the top of the order. For your web page to support this style of automatic adjustment, you have to set 'UICulture=auto' in web page pragma like so: <%@ Page Language="C#" AutoEventWireup="true" UICulture="auto" ... ... ... %> If you use this method, it will display your Default language for any web browser languages that it does not understand. You only have to define a culture-neutral resource to cover all versions of a language, or you can define the specific languages (fr-CA, es-mx, etc) and it will fallback to the neutral language file (fr-fr) if the specific language is not defined. MSDN has more info on the auto culture and how to add more web browser languages to IE: http://msdn.microsoft.com/en-us/library/fw69ke6f.aspx

Option #2 is to set it in the web page pragma to a specific language, like UICulture="fr-FR", or in the web.config for the whole site.

MSDN explains these settings in the link below, and how you can set it via web.config for all pages on your site. http://msdn.microsoft.com/en-us/library/bz9tc508.aspx

The programmatic method that you are using works, but as you noticed it only works temporarily. I've used the programmatic method for reading the language specified via the querystring and changing cultures that way. You can implement an HttpModule to check the querystring and set the culture for the current page request this way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top