【发布时间】:2013-03-02 18:06:53
【问题描述】:
简单。我想本地化我的应用程序。
我已经用谷歌搜索了几天,并尝试了一百万种不同的方法来获取我的资源。
我成功的唯一方法是使用标准的 asp.net 文件夹“App_LocalResource”,公开资源文件并给它们一个Custom Tool Name。然后我可以在视图中导入Custom Tool Name 和@using。
我的问题是,当我改变文化时,语言/资源项并没有改变。
这是我在 global.asax 中更改它的方法:
protected void Application_AcquireRequestState(object sender, EventArgs e)
{
if (HttpContext.Current.Session != null)
{
CultureInfo ci = (CultureInfo)this.Session["Culture"];
if (ci == null)
{
string langName = "en";
string autoLang = "";
if (HttpContext.Current.Request.UserLanguages != null && HttpContext.Current.Request.UserLanguages.Length != 0)
{
autoLang = HttpContext.Current.Request.UserLanguages[0].Substring(0, 2);
}
if (autoLang == "da")
langName = autoLang;
ci = new CultureInfo(langName);
this.Session["Culture"] = ci;
}
Thread.CurrentThread.CurrentUICulture = ci;
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name);
}
}
所以文化是da 或en。但我注意到资源文件的名称必须具有特定的语法。必须有一个没有国家/文化代码的默认值(在本例中为英语),并且其他默认值已命名为 reFile.da-DK.resx。它必须同时具有语言和文化代码。
我担心资源处理程序可以识别我的文件,因为文化设置为“da”而不是“da-DK”。如果我将我的 da 资源文件命名为 resFile.da.resx 我无法导入我的资源文件 Custom Tool Name。
我该怎么做才能解决这个问题?
【问题讨论】:
-
为什么在每种情况下都没有完整的文化名称?
-
@AdamTuliper-MSFT CurrentUICulture 只接受两个字母字符串。
-
CurrentUICulture 接受一个 CultureInfo,它是一个全名(不是两个字符),例如 new CultureInfo("en-US"),我也会在 BeginRequest 中设置它,它发生在页面生命周期的早期并且通常是此代码所在的位置。
-
@AdamTuliper-MSFT 是的。我像你说的那样使用了全名 CultureInfo,并在只需要第一位时将字符串分成两部分。
-
好的,添加在下面作为答案。
标签: asp.net-mvc-3 asp.net-mvc-4 localization cultureinfo currentuiculture