不能使用本地化的资源文件在asp.net mvc的 [英] Cannot localize using resource file in asp.net mvc
问题描述
我开发一个ASP.NET MVC应用程序。在我的应用程序,我想补充一个选项的下拉是供用户选择的语言。我发现使用ASP.NET MVC本地化的文章。
<$p$p><$c$c>http://www.c-sharpcorner.com/UploadFile/b8e86c/localization-of-a-site-in-mvc5-using-resource-file/http://www.mikesdotnetting.com/article/183/globalization-and-localization-with-razor-web-pages这一切都使用资源文件的本地化,并根据检索财产文化资源。例如,我们有如果我们使用英语和法国LangRes.resx和LangRes.fr-FR.resx的名字,使两个资源文件。所以,我测试如何使用资源文件如下图所示。但它不工作。
我创建了两个资源文件,命名为LangRes.resx和LangRes.fr-FR.resx
我修改设置为公开的这两个文件。
然后我说值,以资源文件
块引用>
<醇开始=4>
然后我说这个在Web.config中
块引用>&LT;全球化enableClientBasedCulture =真正的文化=自动的UICulture =自动&GT;&LT; /全球化&GT;
- 然后在视图文件打印我的消息很喜欢这个
块引用>@ {
文化= =的UICultureFR-FR
}
&LT; H2&GT; @ LangRes.Title&LT; / H&GT;实际上,它应该显示法国。对?因为我设置文化FR-FR,并把它映射到后缀LangRes.fr-FR.resx资源文件。但它始终显示英语。所以,请为什么呢?为什么不工作?我该如何解决?此外,什么是ASP.NET MVC的本地化的最佳方式吗?
解决方案您需要通过添加此功能设置在Global.asax中默认的语言:
保护无效的Application_BeginRequest(对象发件人,EventArgs的发送)
{
System.Threading.Thread.CurrentThread.CurrentCulture =新System.Globalization.CultureInfo(FR-FR);
System.Threading.Thread.CurrentThread.CurrentUICulture =新System.Globalization.CultureInfo(FR-FR);
}如果您要使用用户变化的语言选项和创建更改语言按钮(例如EN和FR按钮)。你有一个控制器来设置的文化价值观。例如:
查看:
@ Html.ActionLink(英语,SelectLanguage,家,新{SelectedLanguage =EN-US},NULL)
@ Html.ActionLink(法语,SelectLanguage,家,新{SelectedLanguage =FR-FR},NULL)控制器:
公众的ActionResult SelectLanguage(字符串SelectedLanguage)
{
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(SelectedLanguage);
Thread.CurrentThread.CurrentUICulture =新的CultureInfo(SelectedLanguage.ToLower()); 的HttpCookie LangCookie =新的HttpCookie(LangCookie);
LangCookie.Value = SelectedLanguage;
Response.Cookies.Add(LangCookie); 返回RedirectToAction(指数,家);
}和也,如果你想查询语言的cookie,你可以控制它在你的Global.asax中,像这样的:
保护无效的Application_BeginRequest(对象发件人,EventArgs的发送)
{
的HttpCookie LangCookie = Request.Cookies时[LangCookie];
如果(LangCookie = NULL&放大器;!&安培;!LangCookie.Value = NULL)
{
System.Threading.Thread.CurrentThread.CurrentCulture =新System.Globalization.CultureInfo(LangCookie.Value);
System.Threading.Thread.CurrentThread.CurrentUICulture =新System.Globalization.CultureInfo(LangCookie.Value);
}
其他
{
System.Threading.Thread.CurrentThread.CurrentCulture =新System.Globalization.CultureInfo(FR-FR);
System.Threading.Thread.CurrentThread.CurrentUICulture =新System.Globalization.CultureInfo(FR-FR);
}
}I am developing an ASP.NET MVC application. In my application, I want to add an option with drop down that is for users to select language. I have found the localization articles using ASP.NET MVC.
http://www.c-sharpcorner.com/UploadFile/b8e86c/localization-of-a-site-in-mvc5-using-resource-file/ http://www.mikesdotnetting.com/article/183/globalization-and-localization-with-razor-web-pages
It all using resource file for localization and it retrieve the resource according Culture property. For example, We have to make two resource files if we are using English and France with the name of LangRes.resx and LangRes.fr-FR.resx . So I tested how to use resource files like below. But it is not working.
I created two resource files, named LangRes.resx and LangRes.fr-FR.resx
I set the modifier to public for both files.
Then I added the values to resource files
- Then I added this in Web.config
<globalization enableClientBasedCulture="true" culture="auto" uiCulture="auto"></globalization>
- Then in the view file I printed the message liked this
@{ Culture = UICulture = "fr-FR"; } <h2>@LangRes.Title</h2>
Actually, it should show France. Right? Because I set the culture to "fr-FR" and it is mapped to the resource file with suffix LangRes.fr-FR.resx. But it is always showing "English". So please why? Why is it not working? How can I fix it? Besides, What is the best way to localize in ASP.NET MVC please?
解决方案You need to set default language in Global.asax by adding this function:
protected void Application_BeginRequest(object sender, EventArgs e) { System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("fr-FR"); System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("fr-FR"); }
If you want to use change language option for users and created change language buttons (for example EN and FR buttons). You have to set culture values in a controller. For example:
View:
@Html.ActionLink("English", "SelectLanguage", "Home", new { SelectedLanguage = "en-US" }, null) @Html.ActionLink("Français", "SelectLanguage", "Home", new { SelectedLanguage = "fr-FR" }, null)
Controller:
public ActionResult SelectLanguage(string SelectedLanguage) { Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(SelectedLanguage); Thread.CurrentThread.CurrentUICulture = new CultureInfo(SelectedLanguage.ToLower()); HttpCookie LangCookie = new HttpCookie("LangCookie"); LangCookie.Value = SelectedLanguage; Response.Cookies.Add(LangCookie); return RedirectToAction("Index", "Home"); }
And also if you want to check language cookie, you can control it in your Global.asax, like this:
protected void Application_BeginRequest(object sender, EventArgs e) { HttpCookie LangCookie = Request.Cookies["LangCookie"]; if (LangCookie != null && LangCookie.Value != null) { System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(LangCookie.Value); System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(LangCookie.Value); } else { System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("fr-FR"); System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("fr-FR"); } }
这篇关于不能使用本地化的资源文件在asp.net mvc的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!