ABP Zero 本地化语言的初始化和扩展
在aspnetboilerplate.com生成后,在core下的本地化文件增加选项即可
初始化方法
解析:
var currentCultureName = Thread.CurrentThread.CurrentUICulture.Name;
解决:
Global.asax设置
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); } }
原方法重写
protected override void Application_BeginRequest(object sender, EventArgs e) { Thread.CurrentThread.CurrentCulture = new CultureInfo("zh-CN"); Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh-CN"); }
参考:http://www.ctosay.cn/content/50650577840524115454.html
WEB项目拥有自己的本地化
本地化过程
1.在Web项目下创建文件夹Localization,再创建子目录Source,在Source下创建相对的xml文件
2.在Web项目下自动生成的基类控制器TestControllerBase
protected TestControllerBase() { LocalizationSourceName = TestWebConsts.LocalizationSourceName; }
3. 在 ZeroWebModule下进行初始化
public override void PreInitialize() { //Enable database based localization Configuration.Modules.Zero().LanguageManagement.EnableDbLocalization(); Configuration.Localization.Languages.Add(new LanguageInfo("zh-CN", "简体中文", isDefault: true)); Configuration.Localization.Sources.Add( new DictionaryBasedLocalizationSource( TestWebConsts.LocalizationSourceName, new XmlEmbeddedFileLocalizationDictionaryProvider( Assembly.GetExecutingAssembly(), "Test.Web.Localization.Source" ) ) ); }
这步非常重要,TestWebConsts是自定义类
public class TestWebConsts { public const string LocalizationSourceName = "Test.Web"; }
这里的LocalizationSourceName不能和core项目下的一样,否则会出错
本地化资源文件的属性“生成操作”设置为“嵌入的资源”
"Test.Web.Localization.Source" //这里是本地化资源文件的目录 = 项目名+路径
常见问题
1.Logs.txt提示Can not find 'xx' in localization source 'xxxx.Web'!