在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'!