我使用this blog by Alex Adamyan上的代码创建了一个本地化的MVC网站。
如果我使用现有的文化,这是很有效的。但是,我正在尝试本地化Tagalog(tl或tl-PH)。 Windows没有内置这种文化所以我根据下面的代码创建了一个(我已尝试过tl和tl-PH):
public static void CreateCustomCultures()
{
var cultureBuilder = new CultureAndRegionInfoBuilder(
"tl", CultureAndRegionModifiers.Neutral);
cultureBuilder.LoadDataFromCultureInfo(new CultureInfo("en-US"));
cultureBuilder.LoadDataFromRegionInfo(new RegionInfo("US"));
cultureBuilder.IsMetric = true;
cultureBuilder.CultureEnglishName = "Tagalog";
cultureBuilder.CultureNativeName = "Tagalog";
cultureBuilder.RegionEnglishName = "Tagalog";
cultureBuilder.RegionNativeName = "Tagalog";
cultureBuilder.TwoLetterISOLanguageName = "tl";
cultureBuilder.ThreeLetterISORegionName = "PH";
cultureBuilder.Register();
var cultureBuilder2 = new CultureAndRegionInfoBuilder(
"tl-PH", CultureAndRegionModifiers.None);
cultureBuilder2.LoadDataFromCultureInfo(new CultureInfo("en-US"));
cultureBuilder2.LoadDataFromRegionInfo(new RegionInfo("US"));
cultureBuilder2.IsMetric = true;
cultureBuilder2.CultureEnglishName = "Tagalog";
cultureBuilder2.CultureNativeName = "Tagalog";
cultureBuilder2.RegionEnglishName = "Tagalog";
cultureBuilder2.RegionNativeName = "Tagalog";
cultureBuilder2.TwoLetterISOLanguageName = "tl";
cultureBuilder2.ThreeLetterISORegionName = "PH";
cultureBuilder2.Register();
}
我的测试网站上还有四个资源文件位于〜/ Views / Home / Resources:
当我构建时,我在bin目录下得到三个适当命名的目录,每个目录都有一个适当命名的dll。
所以,当我访问我的网站主页http://localhost:1907时,我会收到默认(英语)语言字符串。
当我去德国(德)主页http://localhost:1907/de时,我得到了网站的德语版本。
当我转到tagalog版本http://localhost:1907/tl或http://localhost:1907/tl-PH时,我会获得英文版而不是Tagalog版。
我在资源获取代码中放置了断点,并确认当前线程的文化和UI文化已正确设置为Tagalog文化,而Tagalog是传递给resourceManager.GetString(key,culture)的文化。
有什么想法?我没有正确地创造我的文化吗?
答案 0 :(得分:1)
我认为你的文化永远不会被注册,至少其中一种文化不是。
以下代码将引发异常,因为您无法为中性文化指定区域值。就像你不能为中性文化创建一个DateTimeFormatInfo对象。
var cultureBuilder = new CultureAndRegionInfoBuilder(
"tl", CultureAndRegionModifiers.Neutral);
cultureBuilder.LoadDataFromCultureInfo(new CultureInfo("en-US"));
cultureBuilder.LoadDataFromRegionInfo(new RegionInfo("US"));
cultureBuilder.IsMetric = true;
cultureBuilder.CultureEnglishName = "Tagalog";
cultureBuilder.CultureNativeName = "Tagalog";
cultureBuilder.RegionEnglishName = "Tagalog";
cultureBuilder.RegionNativeName = "Tagalog";
cultureBuilder.TwoLetterISOLanguageName = "tl";
cultureBuilder.ThreeLetterISORegionName = "PH";
cultureBuilder.Register();
它应该是这样的
var cultureBuilder = new CultureAndRegionInfoBuilder(
"tl", CultureAndRegionModifiers.Neutral);
cultureBuilder.LoadDataFromCultureInfo(new CultureInfo("en-US"));
cultureBuilder.CultureEnglishName = "Tagalog";
cultureBuilder.CultureNativeName = "Tagalog";
cultureBuilder.TwoLetterISOLanguageName = "tl";
cultureBuilder.Register();
第二种特定文化似乎没问题。