如何在 ASP.NET 中本地化 App_GlobalResources?

发布于 2025-01-03 19:01:23 字数 1795 浏览 3 评论 0原文

我有一个 ASP.NET 网站,其中包含一些“全局”资源:

\App_GlobalResources
    ContosoFrobberResource.resx

这是与每个“aspx”文件关联的“本地”资源的补充:

\App_LocalResources
    Materials.aspx.resx
    Notes.aspx.resx
    OrderHistory.aspx.resx

现在我想将该网站本地化为另一个区域设置(例如 en)。我为每个“本地”资源创建一个 resx 文件:

\App_LocalResources
    Materials.aspx.en.resx
    Materials.aspx.en-us.resx
    Materials.aspx.en-uk.resx
    Materials.aspx.en-sg.resx
    Notes.aspx.en.resx
    Notes.aspx.en-us.resx
    Notes.aspx.en-uk.resx
    Notes.aspx.en-sg.resx
    OrderHistory.aspx.en.resx
    OrderHistory.aspx.en-us.resx
    OrderHistory.aspx.en-uk.resx
    OrderHistory.aspx.en-sg.resx

并且一切正常;该网站显示

  • 美国 (en-US)
  • 英国 (en-UK)
  • 新加坡 (en-SG)

的定制英语版本以及通用

  • 英语 (en)

的后备版本当我尝试将资源本地化时,问题就出现了App_GlobalResources

\App_GlobalResources
    ContosoFrobberResource.resx
    ContosoFrobberResource.en-us.resx

网站崩溃并出现以下错误:

CS0101:命名空间“Resources”已包含“ContosoFrobberResource”的定义

如何在 ASP.NET 网站中本地化 App_GlobalResources

奖金喋喋不休

ContosoFrobberResource.resx

 
 <根>
    <数据名称=“ProjectDueDate”xml:空间=“保留”>
       <值>提案截止日期
    
 

 

ContosoFrobberResource.qps.resx

 
 <根>
    <数据名称=“ProjectDueDate”xml:空间=“保留”>
       <值>Prşposẳl Duɇ Dãtē
    
 

i have an ASP.NET website with some "global" resources:

\App_GlobalResources
    ContosoFrobberResource.resx

This is in addition to the "local" resources associated with each "aspx" file:

\App_LocalResources
    Materials.aspx.resx
    Notes.aspx.resx
    OrderHistory.aspx.resx

Now i want to localize the site into another locale (e.g. en). i create a resx file for each "local" resource:

\App_LocalResources
    Materials.aspx.en.resx
    Materials.aspx.en-us.resx
    Materials.aspx.en-uk.resx
    Materials.aspx.en-sg.resx
    Notes.aspx.en.resx
    Notes.aspx.en-us.resx
    Notes.aspx.en-uk.resx
    Notes.aspx.en-sg.resx
    OrderHistory.aspx.en.resx
    OrderHistory.aspx.en-us.resx
    OrderHistory.aspx.en-uk.resx
    OrderHistory.aspx.en-sg.resx

And that all works fine; the site displays tailored versions of English for

  • United States (en-US)
  • United Kingdom (en-UK)
  • Singapore (en-SG)

as well as a fallback for generic

  • English (en)

The problem comes when i try to localize the resources in App_GlobalResources:

\App_GlobalResources
    ContosoFrobberResource.resx
    ContosoFrobberResource.en-us.resx

The web-site crashes with the error:

CS0101: The namespace 'Resources' already contains a definition for 'ContosoFrobberResource'

How do i localize App_GlobalResources in an ASP.NET website?

Bonus Chatter

ContosoFrobberResource.resx

 <?xml version="1.0" encoding="utf-8"?>
 <root>
    <data name="ProjectDueDate" xml:space="preserve">
       <value>Proposal Due Date</value>
    </data>
 </root>

 

ContosoFrobberResource.qps.resx

 <?xml version="1.0" encoding="utf-8"?>
 <root>
    <data name="ProjectDueDate" xml:space="preserve">
       <value>Prȍposẳl Duɇ Dãtē</value>
    </data>
 </root>

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

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

发布评论

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

评论(1

最舍不得你 2025-01-10 19:01:23

找到了答案。

ASP.NET 无法正确回退到父语言。如果它无法将该文件识别为“后备”区域设置,则无法解析该文件,并显示误导性错误。

例如

ContosoGrobber.resx
ContosoGrobber.en-us.resx

工作正常。但是:

ContosoGrobber.resx
ContosoGrobber.en.resx

当无法识别区域设置字符串的 "en" 部分时,将会失败。解决方法是将其更改为使用完整的区域设置名称,并避免出现有问题的后备代码。例如:

ContosoGrobber.qps.resx

应该变成:

ContosoGrobber.qps-ploc.resx

换句话说:如果浏览器正在请求语言环境 qps-ploc理想情况下,您可以使用

ContosoGrobber.qps.resx

本地 qps-ploc将回退到qps。但由于 ASP.NET 中的错误,它无法正确回退。这意味着您不能让它回退;你必须实际处理所有可能的区域设置:

ContosoGrobber.qps-ploc.resx
ContosoGrobber.qps-plocm.resx
ContosoGrobber.qps-ploca.resx

Found the answer.

ASP.NET doesn't fallback properly to the parent language. If it cannot recognize the file as a "fallback" locale then it fails to parse it, and shows the misleading error.

e.g.

ContosoGrobber.resx
ContosoGrobber.en-us.resx

works fine. But:

ContosoGrobber.resx
ContosoGrobber.en.resx

fails when the "en" portion of the locale string isn't recognized. Changing it to use the full locale name, and avoid the buggy fallback code is the fix. e.g.:

ContosoGrobber.qps.resx

should become:

ContosoGrobber.qps-ploc.resx

In other words: if a browser is requesting locale qps-ploc, ideally you could use

ContosoGrobber.qps.resx

and the local qps-ploc will fallback to qps. But because of the bug in ASP.NET it doesn't fallback correctly. This means you cannot let it fallback; you have to actually handle all possible locales:

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