我正在使用.Net Resource API为我的网站创建多种语言功能 我跟着this example
通过这种方式,我可以创建在运行时无法更改的嵌入式资源。当我将Build类型更改为“Content”时,可以在运行时更改资源文件,但我不知道如何使用此构建类型执行多种语言。请帮我!
答案 0 :(得分:0)
我在其他问题上为此写了一个微笑的解决方案: https://stackoverflow.com/a/37458723/3401842
您需要通过添加此函数在Global.asax中设置默认语言:
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");
}
如果要为用户使用更改语言选项并创建更改语言按钮(例如EN和FR按钮)。您必须在控制器中设置文化值。例如:
查看:
@Html.ActionLink("English", "SelectLanguage", "Home", new { SelectedLanguage = "en-US" }, null)
@Html.ActionLink("Français", "SelectLanguage", "Home", new { SelectedLanguage = "fr-FR" }, null)
控制器:
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");
}
如果你想检查语言cookie,你可以在Global.asax中控制它,如下所示:
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");
}
}
您也可以浏览guide。