ABP的语言切换
在ABP官网http://www.aspnetboilerplate.com/创建一个Multi Page Web Application项目并打开,在Web项目下可以找到一个Controllers/LayoutController.cs,里面有代码如下:
[ChildActionOnly] public PartialViewResult LanguageSelection() { var model = new LanguageSelectionViewModel { CurrentLanguage = _localizationManager.CurrentLanguage, Languages = _localizationManager.GetAllLanguages() }; return PartialView("_LanguageSelection", model); }
对,这段控制器代码对应于视图Views/Layout/_LanguageSelection.cshtml,用于页面右上角的语言切换:
控制器传给视图一个LanguageSelectionViewModel对象,用于展示当前默认使用的语言和下拉菜单中支持的语言。
观察上面的代码,就知道LanguageSelectionViewModel对象里的信息其实都是从LocalizationManager传递过来的。查看ABP的源码,信息的源头又可以追溯到ILocalizationConfiguration,原来,语言实际配置的代码放在Web项目的App_Start/某某WebModule.cs的PreInitialize方法里:
public override void PreInitialize() { //Add/remove languages for your application Configuration.Localization.Languages.Add(new LanguageInfo("en", "English", "famfamfam-flag-england", true)); Configuration.Localization.Languages.Add(new LanguageInfo("tr", "Türkçe", "famfamfam-flag-tr")); Configuration.Localization.Languages.Add(new LanguageInfo("zh-CN", "简体中文", "famfamfam-flag-cn")); ... }
LanguageInfo构造函数的第三个参数为图标名,从_LanguageSelection视图可以看到:
<li><a href="/AbpLocalization/ChangeCulture?cultureName=@(language.Name)&returnUrl=@(Request.Url)"><i class="@language.Icon"></i> @language.DisplayName</a></li>
原来在这里使用了一组famfamfam国旗图标,官网:http://www.famfamfam.com/lab/icons/flags/
查看Web项目下的Content/flags/famfamfam-flags.css可以看到对这些国旗图标的定义:
[class^="famfamfam-flag"] { display: inline-block; width: 16px; height: 11px; line-height: 11px; /* vertical-align: text-top; */ background-image: url("famfamfam-flags.png"); background-position: 0 0; background-repeat: no-repeat; } .famfamfam-flag-zw { background-position: 0px 0px; width: 16px; height: 11px; } .famfamfam-flag-zm { background-position: -16px 0px; width: 16px; height: 11px; } .famfamfam-flag-za { background-position: 0px -11px; width: 16px; height: 11px; } .famfamfam-flag-yt { background-position: -16px -11px; width: 16px; height: 11px; } .famfamfam-flag-ye { background-position: -32px 0px; width: 16px; height: 11px; } .famfamfam-flag-ws { background-position: -32px -11px; width: 16px; height: 11px; } .famfamfam-flag-wf { background-position: 0px -22px; width: 16px; height: 11px; } ...
图标文件:
当我们切换语言时,提交的链接大概是这样的:
http://localhost:61754/AbpLocalization/ChangeCulture?cultureName=en&returnUrl=http://localhost:61754/
问题是,ChangeCulture控制器在哪?
查看ABP源码,可以找到Abp.Web.Mvc.Controllers.Localization.AbpLocalizationController类:
public class AbpLocalizationController : AbpController { [DisableAuditing] public virtual ActionResult ChangeCulture(string cultureName, string returnUrl = "") { if (!GlobalizationHelper.IsValidCultureCode(cultureName)) { throw new AbpException("Unknown language: " + cultureName + ". It must be a valid culture!"); } Response.Cookies.Add(new HttpCookie("Abp.Localization.CultureName", cultureName) { Expires = Clock.Now.AddYears(2) }); if (Request.IsAjaxRequest()) { return Json(new MvcAjaxResponse(), JsonRequestBehavior.AllowGet); } if (!string.IsNullOrWhiteSpace(returnUrl)) { return Redirect(returnUrl); } return Redirect(Request.ApplicationPath); } }
一目了然,ABP把“当前所使用的语言”记录在cookies里了。
再回头翻查LocalizationManager类里定义的GetCurrentLanguage方法:
private LanguageInfo GetCurrentLanguage() { if (_configuration.Languages.IsNullOrEmpty()) { throw new AbpException("No language defined in this application. Define languages on startup configuration."); } var currentCultureName = Thread.CurrentThread.CurrentUICulture.Name; //Try to find exact match var currentLanguage = _configuration.Languages.FirstOrDefault(l => l.Name == currentCultureName); if (currentLanguage != null) { return currentLanguage; } //Try to find best match currentLanguage = _configuration.Languages.FirstOrDefault(l => currentCultureName.StartsWith(l.Name)); if (currentLanguage != null) { return currentLanguage; } //Try to find default language currentLanguage = _configuration.Languages.FirstOrDefault(l => l.IsDefault); if (currentLanguage != null) { return currentLanguage; } //Get first one return _configuration.Languages[0]; }
上面代码并没发现读取cookies的操作,再翻到ABP源码的Abp.Web.AbpWebApplication类,原来放到了Application_BeginRequest方法里读取:
protected virtual void Application_BeginRequest(object sender, EventArgs e) { var langCookie = Request.Cookies["Abp.Localization.CultureName"]; if (langCookie != null && GlobalizationHelper.IsValidCultureCode(langCookie.Value)) { Thread.CurrentThread.CurrentCulture = new CultureInfo(langCookie.Value); Thread.CurrentThread.CurrentUICulture = new CultureInfo(langCookie.Value); } }