问题描述
我想在 App_GlobalResources 文件夹中创建一个名为shopping.en-sg.resx"的新加坡英语 (en-sg) 资源文件.
I want to create a resource file for Singaporean English (en-sg) named "shopping.en-sg.resx" in App_GlobalResources folder.
编译时出错.
错误 1 命名空间资源"已经包含一个定义'购物' c:WINDOWSMicrosoft.NETFrameworkv2.0.50727TemporaryASP.NET文件web2cd6afe9737b0a13App_GlobalResources.vomuzavz.1.cs 26
Error 1 The namespace 'Resources' already contains a definition for 'shopping' c:WINDOWSMicrosoft.NETFrameworkv2.0.50727Temporary ASP.NET Filesweb2cd6afe9737b0a13App_GlobalResources.vomuzavz.1.cs 26
在谷歌搜索后,我发现en-sg"不是默认文化,我必须为其创建自定义文化.我不知道这个的详细步骤.
After searching Google, I discover that "en-sg" is not a default culture and I have to create custom culture for it. I don't know the detailed steps of this.
我应该怎么做才能创建文化并消除编译错误?
What should I do to create the culture and remove the compilation error?
我按照 MSDN 中的示例,创建一个名为shopping.x-en-US-sample.resx"的文件,并将以下代码放入 BasePage 的函数中(protected override void InitializeCulture()):
I follow the example in MSDN, create a file called "shopping.x-en-US-sample.resx" and put the following code into BasePage's function (protected override void InitializeCulture()):
CultureAndRegionInfoBuilder cib = null;
cib = new CultureAndRegionInfoBuilder(
"x-en-US-sample", CultureAndRegionModifiers.None);
CultureInfo ci = new CultureInfo("en-US");
cib.LoadDataFromCultureInfo(ci);
RegionInfo ri = new RegionInfo("US");
cib.LoadDataFromRegionInfo(ri);
cib.Register();
ci = new CultureInfo("x-en-US-sample");
但是,编译错误仍然存在.
However, compilation error is still exist.
更新:
您可以通过在 app_globalresources 文件夹中创建一个空网站和两个文件shopping.en-sg.resx"和shopping.resx"来轻松重现该问题.
推荐答案
您可以在现有文化的基础上创建新文化:
You can create a new culture based on an existing culture:
string culture = "en-sg";
string name = "Singaporean English";
CultureInfo cultureInfo = new CultureInfo("en-GB");
RegionInfo regionInfo = new RegionInfo(cultureInfo.Name);
CultureAndRegionInfoBuilder cultureAndRegionInfoBuilder = new CultureAndRegionInfoBuilder(culture, CultureAndRegionModifiers.None);
cultureAndRegionInfoBuilder.LoadDataFromCultureInfo(cultureInfo);
cultureAndRegionInfoBuilder.LoadDataFromRegionInfo(regionInfo);
// Custom Changes
cultureAndRegionInfoBuilder.CultureEnglishName = name;
cultureAndRegionInfoBuilder.CultureNativeName = name;
cultureAndRegionInfoBuilder.Register();
添加:刚刚检查了参考资料:我有:
Added: Just checked the references: I have :
using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
已添加(根据评论更新):
关于错误信息:
您看到的错误是某些资源命名冲突的结果.检查资源名称,这些会被编译成 dll,您需要检查命名空间名称是否冲突.您可以使用反射器工具进行检查:http://www.red-gate.com/products/反射器/
The error you're seeing is the result of some resource naming conflict. Check the resource names, these get compiled into dlls to you need to check that the namespace names dont conflict. You can check this using the reflector tool: http://www.red-gate.com/products/reflector/
这篇关于在 ASP.NET 中创建自定义文化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!