除了 terjetyl 提到的,为了能够改变文化,您需要向控制器添加额外的功能。
首先,您需要创建以下类(您可以将其放在 Controllers 文件夹中):
public class BaseController : Controller
{
protected override void ExecuteCore()
{
string cultureName = null;
// Attempt to read the culture cookie from Request
HttpCookie cultureCookie = Request.Cookies["_culture"];
// If there is a cookie already with the language, use the value for the translation, else uses the default language configured.
if (cultureCookie != null)
cultureName = cultureCookie.Value;
else
{
cultureName = ConfigurationManager.AppSettings["DefaultCultureName"];
cultureCookie = new HttpCookie("_culture");
cultureCookie.HttpOnly = false; // Not accessible by JS.
cultureCookie.Expires = DateTime.Now.AddYears(1);
}
// Validates the culture name.
cultureName = CultureHelper.GetImplementedCulture(cultureName);
// Sets the new language to the cookie.
cultureCookie.Value = cultureName;
// Sets the cookie on the response.
Response.Cookies.Add(cultureCookie);
// Modify current thread's cultures
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(cultureName);
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
base.ExecuteCore();
}
}
然后,您需要使 MVC 项目中的每个控制器都继承自创建的类。
在此之后,您需要在 Views 文件夹的 Web.config 上的命名空间标签上添加以下代码。
<add namespace="complete assembly name of the resources project"/>
最后,您需要在更改语言的按钮上添加将“_culture”cookie 设置为正确语言代码的说明。
如果您有任何问题,请告诉我。