我们首先需要为 make 添加新路由,例如默认情况下您的主要语言
context.MapRoute(
name: "lang",
url: "{lang}/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
constraints : new { lang = @"zh|en" },
namespaces: new[] { "NamespaceToControllers" }
);
context.MapRoute(
name : "default",
url : "{controller}/{action}/{id}",
defaults : new { controller = "Home", action = "Index", id = UrlParameter.Optional, lang = "zh" },
namespaces : new [] { "NamespaceToControllers" }
);
第二部分 - 我们必须修改 DefaultController 以更改cultureInfo 并在配置文件 BaseController 中将其删除
public class DefaultController : BaseController
{
public string CurrentLangCode { get; protected set; }
public Language CurrentLang { get; protected set; }
protected override void Initialize(System.Web.Routing.RequestContext requestContext)
{
if (requestContext.HttpContext.Request.Url != null)
{
HostName = requestContext.HttpContext.Request.Url.Authority;
}
if (requestContext.RouteData.Values["lang"] != null && requestContext.RouteData.Values["lang"] as string != "null")
{
CurrentLangCode = requestContext.RouteData.Values["lang"] as string;
var ci = new CultureInfo(CurrentLangCode);
Thread.CurrentThread.CurrentUICulture = ci;
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name);
}
base.Initialize(requestContext);
}
}
public class BaseController {
protected override void Initialize(System.Web.Routing.RequestContext requestContext)
{
if (requestContext.HttpContext.Request.Url != null)
{
HostName = requestContext.HttpContext.Request.Url.Authority;
}
base.Initialize(requestContext);
}
}
要检查这一点,只需在 main url /en
3th part之后写
我们必须更改按钮等静态内容,我们需要像这样添加新的 Asp.net 文件夹 - App.LocalResourses并在那里创建新文件 - GlobalRes.resx和GlobalRes.en.resx
在此文件中,通过按钮的当前语言或静态添加字符串
毕竟我们需要更改 .resx 文件的属性:
Build Action: Embedded Resource
Custom Tool: PublicResXFileCodeGenerator
我们必须向 web.config 添加新的命名空间
<system.web.webPages.razor>
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="urAppNamespace.Helper" />
<add namespace="urAppNamespace.Tools" />
<add namespace="urAppNamespace.App_LocalResources" />
</namespaces>
</pages>
</system.web.webPages.razor>
这是实现此逻辑的示例
<li><span class="login">@GlobalRes.Enter</span></li>
<li>@Html.ActionLink(GlobalRes.Register, "Register", "User")</li>