zoukankan      html  css  js  c++  java
  • NopCommerce架构分析之八------多语言

    系统支持的语言是有类:Language表示;

    多语言资源对应的类为:LocalizedProperty;

    当先选择某种语言存储在类中:GenericAttribute;

    多语言可以导出为XML文件,当然也支持导出。

    IWorkContext及其实体类WebWorkContext为当前运行上下文;用户的登录信息以及一些上下文环境设置都保存在此类中。

    具体包括:当前用户信息:CurrentCustomer;当前用户Cookie;货币;语言;税的类型;供应商等;

    展现多语言资源的方式有几种:

    一、在自定义类WebViewPage<TModel>中放置了方法:T(),通过此方法,网页在展现时获取对应语言的文字。

    其实T只是一个代理,代理的定义为:

    [csharp] view plain copy
    1. namespace Nop.Web.Framework.Localization  
    2. {  
    3.     public delegate LocalizedString Localizer(string text, params object[] args);  
    4. }  


    此代理返回值类型为LocalizedString,此类继承接口IHtmlString,以保证能正确显示本地化的文字资源。

    IHtmlString的定义为:

    [csharp] view plain copy
    1. // 摘要:  
    2.     //     表示不应再次进行编码的 HTML 编码的字符串。  
    3.     public interface IHtmlString  
    4.     {  
    5.         // 摘要:  
    6.         //     返回 HTML 编码的字符串。  
    7.         //  
    8.         // 返回结果:  
    9.         //     HTML 编码的字符串。  
    10.         string ToHtmlString();  
    11.     }  


    二、通过扩展HtmlHelper

    类HtmlExtensions扩展了HtmlHelper类,

    主要是对一些控件的封装,并支持多语言。

    方法 LocalizedEditor<T, TLocalizedModelLocal>是对Telerik的TabStrip控件的封装(也就是多页签控件---Tab控件),的。系统同时支持有多种语言时,多为每种语言显示一个页签,当然仅当需要时才这么做。这里面用到了接口ILocalizedModel和接口ILocalizedModelLocal。接口ILocalizedModel用来标示某Model类支持这种多语言显示,其中里面包括多种语言数据列表Locales,实现接口ILocalizedModelLocal的类就是特定一种语言的数据。LocalizedEditor方法就是根据这些接口的配合实现了支持多种语言页签了。Admin项目使用此方法,Web项目没有使用。

    [csharp] view plain copy
    1. public static HelperResult LocalizedEditor<T, TLocalizedModelLocal>(this HtmlHelper<T> helper, string name,  
    2.             Func<int, HelperResult> localizedTemplate,  
    3.             Func<T, HelperResult> standardTemplate)  
    4.            where T : ILocalizedModel<TLocalizedModelLocal>  
    5.            where TLocalizedModelLocal : ILocalizedModelLocal  
    6.        {  
    7.            return new HelperResult(writer =>  
    8.            {  
    9.                if (helper.ViewData.Model.Locales.Count > 1)  
    10.                {  
    11.                    var tabStrip = helper.Telerik().TabStrip().Name(name).Items(x =>  
    12.                    {  
    13.                        x.Add().Text("Standard").Content(standardTemplate(helper.ViewData.Model).ToHtmlString()).Selected(true);  
    14.                        for (int i = 0; i < helper.ViewData.Model.Locales.Count; i++)  
    15.                        {  
    16.                            var locale = helper.ViewData.Model.Locales[i];  
    17.                            var language = EngineContext.Current.Resolve<ILanguageService>().GetLanguageById(locale.LanguageId);  
    18.                            x.Add().Text(language.Name)  
    19.                                .Content(localizedTemplate  
    20.                                    (i).  
    21.                                    ToHtmlString  
    22.                                    ())  
    23.                                .ImageUrl("~/Content/images/flags/" + language.FlagImageFileName);  
    24.                        }  
    25.                    }).ToHtmlString();  
    26.                    writer.Write(tabStrip);  
    27.                }  
    28.                else  
    29.                {  
    30.                    standardTemplate(helper.ViewData.Model).WriteTo(writer);  
    31.                }  
    32.            });  
    33.        }  


    扩展方法NopLabelFor<TModel, TValue>是另外一种多语言实现方式。

    此方法主要是根据特性DisplayNameAttribute的子类NopResourceDisplayName实现对属性名称的描述。此特性是对Model属性的修饰,以指定属性的名称。

    例如类AddNewsCommentModel的属性用NopResourceDisplayName特性指定:

    [csharp] view plain copy
    1. namespace Nop.Web.Models.News  
    2. {  
    3.     public partial class AddNewsCommentModel : BaseNopModel  
    4.     {  
    5.         [NopResourceDisplayName("News.Comments.CommentTitle")]  
    6.         [AllowHtml]  
    7.         public string CommentTitle { get; set; }  
    8.   
    9.         [NopResourceDisplayName("News.Comments.CommentText")]  
    10.         [AllowHtml]  
    11.         public string CommentText { get; set; }  
    12.   
    13.         public bool DisplayCaptcha { get; set; }  
    14.     }  
    15. }  


    HtmlHelper的扩展方法NopLabelFor的实现如下:

    [csharp] view plain copy
      1. public static MvcHtmlString NopLabelFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression, bool displayHint = true)  
      2.         {  
      3.             var result = new StringBuilder();  
      4.             var metadata = ModelMetadata.FromLambdaExpression(expression, helper.ViewData);  
      5.             var hintResource = string.Empty;  
      6.             object value = null;  
      7.             if (metadata.AdditionalValues.TryGetValue("NopResourceDisplayName", out value))  
      8.             {  
      9.                 var resourceDisplayName = value as NopResourceDisplayName;  
      10.                 if (resourceDisplayName != null && displayHint)  
      11.                 {  
      12.                     var langId = EngineContext.Current.Resolve<IWorkContext>().WorkingLanguage.Id;  
      13.                     hintResource =  
      14.                         EngineContext.Current.Resolve<ILocalizationService>()  
      15.                         .GetResource(resourceDisplayName.ResourceKey + ".Hint", langId);  
      16.   
      17.                     result.Append(helper.Hint(hintResource).ToHtmlString());  
      18.                 }  
      19.             }  
      20.             result.Append(helper.LabelFor(expression, new { title = hintResource }));  
      21.             return MvcHtmlString.Create(result.ToString());  
      22.         } 
  • 相关阅读:
    python基础:多进程讲解
    vue 导出数据
    vue 导入excel数据组件
    vue 分页组件
    vue 点击空白区域隐藏div
    vue 类似淘宝选择地址组件
    js基础练习经典题(数组,质数,数组的遍历等等)
    js去重复
    理解CSS3里的Flex布局用法
    书写HTML5/CSS3的命名规则
  • 原文地址:https://www.cnblogs.com/zhangruisoldier/p/8042328.html
Copyright © 2011-2022 走看看