.resx 本地化不适用于 ASP.Net - C#

发布于 2024-08-06 06:00:48 字数 542 浏览 4 评论 0原文

我创建了两个 .resx 文件 - WebResources.resx 和 WebResources.fr-FR.resx。

两者都包含 ExceptionMessages_SignupWithCard。

为了测试我所做的:

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

问题是我仍然从 WebResources.resx 收到 ExceptionMessages_SignupWithCard 消息,而不是从 WebResources.fr-FR.resx 收到消息。

有人可以帮忙吗?

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

金兰素衣 2024-08-13 06:00:48

ASP.Net(特别是 ResourceManager)使用 CurrentUICulture 来检索资源。

也尝试设置线程的 CurrentUICulture。

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

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);
玩世 2024-08-13 06:00:48

尝试设置 Page.Culture 和 Page.UICulture 而不是直接设置线程区域性。

由于它是一个字符串属性,因此看起来不太好。但是,有很多代码不仅会改变线程文化,还会改变资源管理器。

这似乎是标准方法,而不是分配线程的区域性。

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.

热鲨 2024-08-13 06:00:48
<%@ 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>
<%@ 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>
世态炎凉 2024-08-13 06:00:48

有两种方法可以设置 ASP.NET 网页的 UICulture。

选项 #1 是 Web 浏览器可以通过设置 Web 浏览器中的当前语言来调整 UI 文化(自动)。在 FireFox 中,转到“选项”中的“内容”选项卡,然后单击“语言”,然后添加法语并将其移动到列表顶部。 IE 类似,它在“选项”页面中有一个“语言”按钮,您可以添加法语并将其移至订单顶部。为了让您的网页支持这种自动调整方式,您必须在网页编译指示中设置“UICulture=auto”,如下所示:
<%@ 页面语言="C#" AutoEventWireup="true" UICulture="auto" ... ... ... %>
如果您使用此方法,它将显示它不理解的任何 Web 浏览器语言的默认语言。您只需定义一个文化中立的资源来覆盖一种语言的所有版本,或者您可以定义特定的语言(fr-CA、es-mx 等),它将回退到中性语言文件(fr-fr)如果未定义特定语言。 MSDN 提供了有关汽车文化以及如何向 IE 添加更多网络浏览器语言的更多信息:
http://msdn.microsoft.com/en-us/library/fw69ke6f.aspx

选项 #2 是在网页编译指示中将其设置为特定语言,例如 UICulture="fr-FR",或者在整个站点的 web.config 中将其设置。

MSDN 在下面的链接中解释了这些设置,以及如何通过 web.config 为站点上的所有页面进行设置。
http://msdn.microsoft.com/en-us/library/bz9tc508.aspx

您正在使用的编程方法是有效的,但正如您所注意到的,它只是暂时有效。我使用编程方法来读取通过查询字符串指定的语言并以这种方式更改文化。您可以实现一个 HttpModule 来检查查询字符串并以这种方式设置当前页面请求的区域性。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文