views:

1424

answers:

3

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.

+7  A: 

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);
womp
Great, that worked, thanks, but when I place it in a button's onclick event, it doesn't maintain for the reload (displaying the .resx content in the on_load event).I guess I need to know how to change it for the session so it maintains over all my pages.Thanks.
ElHaix
+1  A: 

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.

Dominic Zukiewicz
A: 
<%@ 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"&gt;

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