• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    NopCommerce架构分析之(八)多语言支持

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

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

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

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

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

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

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

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

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

    namespace Nop.Web.Framework.Localization 
    { 
      public delegate LocalizedString Localizer(string text, params object[] args); 
    }

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

    IHtmlString的定义为:

    // 摘要: 
    //   表示不应再次进行编码的 HTML 编码的字符串。 
    public interface IHtmlString 
    { 
      // 摘要: 
      //   返回 HTML 编码的字符串。 
      // 
      // 返回结果: 
      //   HTML 编码的字符串。 
      string ToHtmlString(); 
    } 

    二、通过扩展HtmlHelper

    类HtmlExtensions扩展了HtmlHelper类,

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

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

    public static HelperResult LocalizedEditorT, TLocalizedModelLocal>(this HtmlHelperT> helper, string name, 
      Funcint, HelperResult> localizedTemplate, 
      FuncT, HelperResult> standardTemplate) 
      where T : ILocalizedModelTLocalizedModelLocal> 
      where TLocalizedModelLocal : ILocalizedModelLocal 
    { 
      return new HelperResult(writer => 
      { 
        if (helper.ViewData.Model.Locales.Count > 1) 
        { 
          var tabStrip = helper.Telerik().TabStrip().Name(name).Items(x => 
          { 
            x.Add().Text("Standard").Content(standardTemplate(helper.ViewData.Model).ToHtmlString()).Selected(true); 
            for (int i = 0; i  helper.ViewData.Model.Locales.Count; i++) 
            { 
              var locale = helper.ViewData.Model.Locales[i]; 
              var language = EngineContext.Current.ResolveILanguageService>().GetLanguageById(locale.LanguageId); 
              x.Add().Text(language.Name) 
                .Content(localizedTemplate 
                  (i). 
                  ToHtmlString 
                  ()) 
                .ImageUrl("~/Content/images/flags/" + language.FlagImageFileName); 
            } 
          }).ToHtmlString(); 
          writer.Write(tabStrip); 
        } 
        else 
        { 
          standardTemplate(helper.ViewData.Model).WriteTo(writer); 
        } 
      }); 
    }

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

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

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

    namespace Nop.Web.Models.News 
    { 
      public partial class AddNewsCommentModel : BaseNopModel 
      { 
        [NopResourceDisplayName("News.Comments.CommentTitle")] 
        [AllowHtml] 
        public string CommentTitle { get; set; } 
     
        [NopResourceDisplayName("News.Comments.CommentText")] 
        [AllowHtml] 
        public string CommentText { get; set; } 
     
        public bool DisplayCaptcha { get; set; } 
      } 
    }

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

    public static MvcHtmlString NopLabelForTModel, TValue>(this HtmlHelperTModel> helper, ExpressionFuncTModel, TValue>> expression, bool displayHint = true) 
    { 
      var result = new StringBuilder(); 
      var metadata = ModelMetadata.FromLambdaExpression(expression, helper.ViewData); 
      var hintResource = string.Empty; 
      object value = null; 
      if (metadata.AdditionalValues.TryGetValue("NopResourceDisplayName", out value)) 
      { 
        var resourceDisplayName = value as NopResourceDisplayName; 
        if (resourceDisplayName != null  displayHint) 
        { 
          var langId = EngineContext.Current.ResolveIWorkContext>().WorkingLanguage.Id; 
          hintResource = 
            EngineContext.Current.ResolveILocalizationService>() 
            .GetResource(resourceDisplayName.ResourceKey + ".Hint", langId); 
    
          result.Append(helper.Hint(hintResource).ToHtmlString()); 
        } 
      } 
      result.Append(helper.LabelFor(expression, new { title = hintResource })); 
      return MvcHtmlString.Create(result.ToString()); 
    }

    您可能感兴趣的文章:
    • NopCommerce架构分析之(七)主题Theme皮肤管理器
    • NopCommerce架构分析之(六)自定义RazorViewEngine和WebViewPage
    • NopCommerce架构分析之(五)Model绑定Action参数
    • NopCommerce架构分析之(四)基于路由实现灵活的插件机制
    • NopCommerce架构分析之(三)EntityFramework数据库初试化及数据操作
    • NopCommerce架构分析(一)Autofac依赖注入类生成容器
    • 使用Nopcommerce为商城添加满XX减XX优惠券功能
    • 基于nopCommerce的开发框架 附源码
    上一篇:NopCommerce架构分析之(七)主题Theme皮肤管理器
    下一篇:ASP.NET中防止页面刷新造成表单重复提交执行两次操作
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯

    时间:9:00-21:00 (节假日不休)

    地址:江苏信息产业基地11号楼四层

    《增值电信业务经营许可证》 苏B2-20120278

    NopCommerce架构分析之(八)多语言支持 NopCommerce,架构,分析,之,