简单。我想本地化我的应用程序。 我搜索了几天,并尝试了一百万种不同的方法来获取我的资源。 我成功的唯一方法是使用标准的asp.net文件夹“App_LocalResource”,公开资源文件并给它们一个Custom Tool Name
。在视图中,我可以导入Custom Tool Name
和@using
。MVC4中的ASP.NET资源处理机制
我的问题是,当我改变文化时,语言/资源项目不会改变。
这是我如何改变它在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
。
我该怎么做才能解决这个问题?
为什么在这里每个案例都没有提供完整的文化名称? – 2013-03-02 21:33:38
@ AdamTuliper-MSFT CurrentUICulture只接受两个字母的字符串。 – 2013-03-04 11:53:29
CurrentUICulture接受一个CultureInfo,它是一个全名(不是两个字符),比如新的CultureInfo(“en-US”),我也会在BeginRequest中设置它,它在页面生命周期的早期出现,通常是这个代码的地方发现自己。 – 2013-03-04 17:44:23