如何在 ASP.NET 中本地化 App_GlobalResources?
我有一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
找到了答案。
ASP.NET 无法正确回退到父语言。如果它无法将该文件识别为“后备”区域设置,则无法解析该文件,并显示误导性错误。
例如
工作正常。但是:
当无法识别区域设置字符串的 "
en
" 部分时,将会失败。解决方法是将其更改为使用完整的区域设置名称,并避免出现有问题的后备代码。例如:应该变成:
换句话说:如果浏览器正在请求语言环境
qps-ploc
,理想情况下,您可以使用本地
qps-ploc
将回退到qps
。但由于 ASP.NET 中的错误,它无法正确回退。这意味着您不能让它回退;你必须实际处理所有可能的区域设置: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.
works fine. But:
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.:should become:
In other words: if a browser is requesting locale
qps-ploc
, ideally you could useand the local
qps-ploc
will fallback toqps
. 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: