我正在尝试在项目中添加本地化功能
我有
<li>@Html.ActionLink(Resources.HomeTexts.Home, "Index", "Home")</li>
(用于菜单)。
我也有语言切换器
<li>@Html.ActionLink("French", ViewContext.RouteData.Values["action"].ToString(), ViewContext.RouteData.Values["controller"].ToString(), new { lang = "fr" }, null)</li>
我有资源文件文件GomeTexts.fr.resx 我将自定义工具设置为PublicResXFileCodeGenerator的位置 和自定义工具名称空间资源。
我有溃败配置
routes.MapRoute(
name: "DefaultLocalized",
url: "{lang}/{controller}/{action}/{id}",
constraints: new { lang = @"(\w{2})|(\w{2}-\w{2})" }, // en or en-US
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, lang="en" }
);
我有Global.asax
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies["lang"];
if(cookie!=null && cookie.Value != null)
{
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(cookie.Value);
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(cookie.Value);
}
else
{
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("fr");
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("fr");
}
}
我有控制器
public ActionResult Index(string lang)
{
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);
return View();
}
但是没有任何帮助,是的,当我在地址栏中按法语时,我可以看到fr,但是语言仍从默认资源栏中获取。我什至手动设置为“ fr”,但仍然没有。我在做什么可能错了?