功能
为了实现动态语言切换,Demo做了三个可以切换的语言库,包括资源文件的定义、实体对象属性的设置、后台代码控制器的实现、iauthorization filter和html helper,易学易用。有了我之前发布的# MVC脚手架智能代码引擎的更新模板中生成多语言资源文件的新功能,我发现我的框架和现在流行的ABP框架差不多,有点像收费版的Asp.net Zero,但是我把它做得更轻,更方便,更快。不幸的是,Asp.net核心下的脚手架扩建并不像MVC5那么容易。这个还需要研究,下一步就是升级到ASP.NET核心。
Github下载演示
具体实现方法
定义实体类
名称资源类型是通过显示属性定义的,并且需要要读取的语言库资源文件
生成资源文件
mvc脚手架扩展工具会自动生成三个实体对象对应的资源文件,默认为中文、繁体和英文
繁体中文需要自己翻译,英文内容根据字段名定义后已经被分为大写字母。日期时间现实日期时间
页面功能按钮语言资源文件库
前端和后端代码实现语言切换功能
-选择切换语言
javascript,javascript
/*多种语言下拉列表*/$(' # dropdownlang-drop down-menu ')。on('click ',' a ',()= >{ const lang = this.textconst flag = this . FIRStElementChild . ClassName;const culture = this . FIRStElementChild . GetAttribute(" culture ");$('#dropdownlang ')。children()[0]。className =标志;$('#dropdownlang ')。children()[1]。innerHTML = langlocal STORAGE . SetItem(' lang-text ',lang);localStorage.setItem('lang-css ',标志);local storage . SetItem(' lang-culture ',区域性);$.get('/Account/SetCulture?lang=' +文化)。然后(res = >;{ if(RES . success){ location . reload();} });});$(()= >{ const lang = LocalStorage . GetItem(' lang-text ');const CSS = LocalStorage . GetItem(' lang-CSS ');const culture = LocalStorage . GetItem(' lang-culture ');//这段代码是多余的,完全没有效果//tag = document。("");//tag . type = " text/Java ";//tag . src = src;//document.body .(标记);//$ . parser . parse();};if(lang & amp;& ampcss和。& amp区域性){ $('#dropdownlang ')。children()[0]。className = css$('#dropdownlang ')。children()[1]。innerHTML = lang} });
后端代码
[http get]public action result set culture(string lang){//此处的CultureInfo集是冗余开关(lang . trim()){ case " en ":culture info。current culture = new culture info(" en-us ");CultureInfo。currentui culture = new culture info(" en-US ");打破;case "cn": CultureInfo。current culture = new culture info(" zh-CN ");CultureInfo。currentui culture = new culture info(" zh-CN ");打破;案例“tw”:CultureInfo。current culture = new culture info(" zh-TW ");CultureInfo。currentui culture = new culture info(" zh-TW ");打破;}//这里设置的CultureInfo是多余的var cookie = new http cookie(" culture ",lang) {expires = datetime。现在。add years(1)};回应。cookie . Add(cookie);返回Json(new { success = true },JsonRequestBehavior。allow get);}
这是关键。它不使用RouteData,并且有很多地方可以通过修改url来改变当前的语言,所以我使用Cookies来保存它
公共类CultureFilter:IAuthorizationFilter { private readonly string DefaultCulture;public CultureFilter(){ this . DefaultCulture = " cn ";} public void on authorization(AuthorizationContext filter context){ var culture = filter context。HttpContext . request . Cookies[" culture "];var lang = defaultCultureif(文化!= null & amp& amp文化。值!= null) { lang =区域性。价值;filterContext。HttpContext . response . Cookies . set(区域性);} switch (lang。Trim()) { case "en": CultureInfo。current culture = new culture info(" en-US ");CultureInfo。currentui culture = new culture info(" en-US ");//线程。current thread . currentui culture = new culture info(" en-US ");//线程。current thread . current culture = new culture info(" en-US ");打破;case "cn": CultureInfo。current culture = new culture info(" zh-CN ");CultureInfo。currentui culture = new culture info(" zh-CN ");//线程。current thread . currentui culture = new culture info(" zh-CN ");//线程。current thread . current culture = new culture info(" zh-CN ");打破;案例“tw”:CultureInfo。current culture = new culture info(" zh-TW ");CultureInfo。currentui culture = new culture info(" zh-TW ");//线程。current thread . currentui culture = new culture info(" zh-TW ");//线程。current thread . current culture = new culture info(" zh-TW ");打破;} } }
HtmlHelper代码实现语言的输出
/// <。摘要>。///多语言切换//。& lt/param>。/// <。param name="name " >& lt/param>。/// <。退货>。& lt/returns>。公共静态HtmlString L(此HtmlHelper助手,字符串名称){ var resource = new System。resources . resource manager(type of(WebApp . resource . global));var text =资源。GetString(名称);返回新的HtmlString(文本??姓名);} /// <。摘要>。///前端easyui或者其他js相关的信息,比如提示信息,也需要转换。这里必须加载不同的语言文件。///
原文:https://www.cnblogs.com/neozhu/p/10285874.html
1.《cultureinfo ASP.NET MVC5多语言切换快速实现方案》援引自互联网,旨在传递更多网络信息知识,仅代表作者本人观点,与本网站无关,侵删请联系页脚下方联系方式。
2.《cultureinfo ASP.NET MVC5多语言切换快速实现方案》仅供读者参考,本网站未对该内容进行证实,对其原创性、真实性、完整性、及时性不作任何保证。
3.文章转载时请保留本站内容来源地址,https://www.lu-xu.com/caijing/1041822.html