动态加载RESX文件不起作用

发布于 2025-01-04 07:49:59 字数 534 浏览 3 评论 0原文

我有一个网页,必须根据用户选择以多种不同的语言显示。为此,我对每个 asp.net 网页使用 RESX 文件。我不想在浏览器中使用自动检测语言,但我想根据用户选择再次设置语言。为了实现这一目标,我正在执行以下操作:

Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("es-MX", false);
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("es-MX", false);

或者

Page.Culture = "es-MX";
Page.UICulture = "es-MX";

但是这些都没有按预期工作!我正在页面的 Init 方法中初始化文化,但它将始终显示默认语言。我正在检查这些属性的值,并且这些属性的区域性正确,但仍然没有使用 RESX 文件进行渲染。有什么想法吗?建议?

谢谢

I have a webpage that has to be displayed in several different languages based on user selection. For that, I'm using RESX files for each of the asp.net webpages. I don't want to used the automatic detection of the language in the browser, but I want to set the language, again, based in the user selection. In order to accomplish this I'm doing the following:

Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("es-MX", false);
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("es-MX", false);

OR

Page.Culture = "es-MX";
Page.UICulture = "es-MX";

But neither of those are working as expected! I'm initializing the Culture in the Init method of the page but it will always display the default language. I'm inspecting the values of those properties and those have the culture correctly, but still is not being rendered using the RESX file. Any ideas? Suggestions?

Thanks

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

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

发布评论

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

评论(2

等你爱我 2025-01-11 07:49:59

如果有人在使用显式本地化时遇到此问题,则必须执行以下操作:

protected override void InitializeCulture()
{
    Page.Culture = "en-US";
    Page.UICulture = "en-US";
}

来自 net-tutorials.com 网站:

由于 Page 指令只是 Page 类的快捷方式,因此也可以从 CodeBehind 完成此操作。但是,我们必须在渲染页面之前的某个时刻执行此操作,以确保其具有所需的效果。这就是 InitializeCulture() 方法发挥作用的地方,该方法由 ASP.NET 在页面生命周期的早期调用,您可以覆盖该方法。

In case someone runs into this issue when working with Explicit localization, here is what has to be done:

protected override void InitializeCulture()
{
    Page.Culture = "en-US";
    Page.UICulture = "en-US";
}

From the net-tutorials.com website:

Since the Page directive is just a shortcut to the Page class, this can be done from CodeBehind as well. However, we have to do it at a certain point, before the page is being rendered, to make sure that it has the desired effect. This is where the InitializeCulture() method comes into play, a method that is called by ASP.NET pretty early in the Page life cycle, which you can override.

天涯离梦残月幽梦 2025-01-11 07:49:59

试试这个
System.Resources.ResourceReader 资源读取器
= new System.Resources.ResourceReader("RES_PATH");

现在您可以使用它来加载用户语言,例如 es.resx

System.Resources.ResourceReader resourceReader 
    = new System.Resources.ResourceReader(HttpContext.Current.Request.UserLanguages[0] 
    + ".resource");

Try this
System.Resources.ResourceReader resourceReader
= new System.Resources.ResourceReader("RES_PATH");

Now you can use this to load users language like es.resx

System.Resources.ResourceReader resourceReader 
    = new System.Resources.ResourceReader(HttpContext.Current.Request.UserLanguages[0] 
    + ".resource");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文