Cannot localize using resource file in asp.net mvc
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
<globalization enableClientBasedCulture="true" culture="auto" uiCulture="auto"></globalization>
@{
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");
}
}
链接地址: http://www.djcxy.com/p/66648.html
上一篇: 如何设置运行JDK Netbeans?