of Eight Multilingual Support for NopCommerce Architecture Analysis

  • 2021-07-16 02:15:34
  • OfStack

The languages supported by the system are classed: Language representation;

The classes corresponding to multilingual resources are: LocalizedProperty;;

When selecting a language to store in the class: GenericAttribute;;

Multilingualism can be exported to XML files, and export is also supported.

IWorkContext and its entity class WebWorkContext are the current running context; The user's login information and a few context settings are saved in this class.

Specifically, it includes: current user information: CurrentCustomer;; Current user Cookie;; Money; Language; Types of taxes; Suppliers, etc.;

There are several ways to present multilingual resources:

1. In the custom class WebViewPage < TModel > The method: T () is placed in, by which the text of the corresponding language is obtained when the web page is displayed.

In fact, T is only one agent, and the agent is defined as:


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

This proxy return value type is LocalizedString, which inherits the interface IHtmlString to ensure that localized text resources are displayed correctly.

IHtmlString is defined as:


//  Summary : 
//    Object that should not be encoded again  HTML  The encoded string.  
public interface IHtmlString 
{ 
  //  Summary : 
  //    Return  HTML  The encoded string.  
  // 
  //  Return results : 
  //   HTML  The encoded string.  
  string ToHtmlString(); 
} 

2. By extending HtmlHelper

Class HtmlExtensions extends the HtmlHelper class,

It mainly encapsulates some controls and supports multiple languages.

Method LocalizedEditor < T, TLocalizedModelLocal > Is the encapsulation of the TabStrip control of Telerik (that is, the multi-tab control-Tab control). When the system supports multiple languages at the same time, it usually displays 1 tab for each language, of course, only when necessary. Interface ILocalizedModel and interface ILocalizedModelLocal are used here. Interface ILocalizedModel is used to indicate that a certain Model class supports this multilingual display, which includes a multi-language data list Locales, and the class implementing interface ILocalizedModelLocal is the data of a specific language. LocalizedEditor method is based on the cooperation of these interfaces to support multi-language tabs. The Admin project uses this method, but the Web project does not.


public static HelperResult LocalizedEditor<T, TLocalizedModelLocal>(this HtmlHelper<T> helper, string name, 
  Func<int, HelperResult> localizedTemplate, 
  Func<T, HelperResult> standardTemplate) 
  where T : ILocalizedModel<TLocalizedModelLocal> 
  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.Resolve<ILanguageService>().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); 
    } 
  }); 
}

Extension method NopLabelFor < TModel, TValue > Is another multilingual implementation.

This method mainly describes the attribute name according to the subclass NopResourceDisplayName of the attribute DisplayNameAttribute. This attribute is a modification of the Model property to specify the name of the property.

For example, the properties of the class AddNewsCommentModel are specified with the NopResourceDisplayName attribute:


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; } 
  } 
}

The implementation of NopLabelFor, an extension of HtmlHelper, is as follows:


public static MvcHtmlString NopLabelFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, 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.Resolve<IWorkContext>().WorkingLanguage.Id; 
      hintResource = 
        EngineContext.Current.Resolve<ILocalizationService>() 
        .GetResource(resourceDisplayName.ResourceKey + ".Hint", langId); 

      result.Append(helper.Hint(hintResource).ToHtmlString()); 
    } 
  } 
  result.Append(helper.LabelFor(expression, new { title = hintResource })); 
  return MvcHtmlString.Create(result.ToString()); 
}


Related articles: