我创建了一个类库项目,并添加了基于http://msdn.microsoft.com/en-us/library/ms172469(v=vs.90).aspx
的自定义文化类库项目由一个简单的asp.net webproject引用,我只是根据当前文化显示一个字符串消息。
除了messages.es-ES.resx之外,我还添加了一个资源文件'messages.x-en-US-sample.resx'。当我将我的ui文化和文化(currentThread)设置为x-en-US-sample时,资源字符串不会自动映射到此文件,就像我使用例如es-ES文化自动获取messages.es-ES.resx。
我在这里缺少什么?
答案 0 :(得分:2)
以下是我放在一起的简单测试。只要您的资源文件显示了扩展名,就可以使用。
// Resource file names:
// XXX.resx (default)
// XXX.de-DE.resx (German)
// XXX.en-Hobbit.resx (custom)
static void Main()
{
BuildAndRegisterHobbitCulture();
const string goodDay = "GoodDay";
// Default.
Console.WriteLine("{0}: {1}", Thread.CurrentThread.CurrentCulture.Name,
Resources.ResourceManager.GetString(goodDay));
// German.
SetCulture("de-DE");
Console.WriteLine("{0}: {1}", Thread.CurrentThread.CurrentCulture.Name,
Resources.ResourceManager.GetString(goodDay));
// Custom - Hobbit.
SetCulture("en-Hobbit");
Console.WriteLine("{0}: {1}", Thread.CurrentThread.CurrentCulture.Name,
Resources.ResourceManager.GetString(goodDay));
Console.ReadLine();
}
private static void SetCulture(string culture)
{
var cultureInfo = CultureInfo.GetCultureInfo(culture);
Thread.CurrentThread.CurrentCulture = cultureInfo;
Thread.CurrentThread.CurrentUICulture = cultureInfo;
}
private static void BuildAndRegisterHobbitCulture()
{
if (CultureInfo.GetCultures(CultureTypes.AllCultures).Any(ci => ci.Name == "en-Hobbit"))
return;
var cultureAndRegionInfoBuilder = new CultureAndRegionInfoBuilder("en-Hobbit", CultureAndRegionModifiers.None);
cultureAndRegionInfoBuilder.LoadDataFromCultureInfo(CultureInfo.CreateSpecificCulture("en-GB"));
cultureAndRegionInfoBuilder.LoadDataFromRegionInfo(new RegionInfo("en-GB"));
cultureAndRegionInfoBuilder.Register();
}
答案 1 :(得分:0)
为什么要设置x-en-US
文化?
设置为:
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-US");
使用资源文件:
MyResourceManager.yourcountry-en-US.resx
我认为x-en-US
不是有效的CultureInfo代码,应该是en-US
您在这里有一个微软示例,您可以在其中注册自定义文化并分配它们:
public static void Main()
{
// Create a custom culture for ru-US.
CultureAndRegionInfoBuilder car1 = new CultureAndRegionInfoBuilder("ru-US",
CultureAndRegionModifiers.None);
car1.LoadDataFromCultureInfo(CultureInfo.CreateSpecificCulture("ru-RU"));
car1.LoadDataFromRegionInfo(new RegionInfo("en-US"));
car1.CultureEnglishName = "Russian (United States)";
car1.CultureNativeName = "русский (США)";
car1.CurrencyNativeName = "Доллар (США)";
car1.RegionNativeName = "США";
// Register the culture.
try {
car1.Register();
}
catch (InvalidOperationException) {
// Swallow the exception: the culture already is registered.
}
// Use the custom culture.
CultureInfo ci = CultureInfo.CreateSpecificCulture("ru-US");
Thread.CurrentThread.CurrentCulture = ci;
Console.WriteLine("Current Culture: {0}",
Thread.CurrentThread.CurrentCulture.Name);
Console.WriteLine("Writing System: {0}",
Thread.CurrentThread.CurrentCulture.TextInfo);
}