Nopcommerce Architecture Analysis (eight) multi-language Support _ self-study process

Source: Internet
Author: User
Tags locale nopcommerce

The system supports languages that have classes: Language says;

The corresponding classes for multilingual resources are: localizedproperty;

Select a language to store in the class: Genericattribute;

Multiple languages can be exported as XML files and, of course, export supported.

The Iworkcontext and its entity classes are webworkcontext to the current running context, and the user's logon information and some context settings are saved in this class.

Details include: Current user information: Currentcustomer; current user cookie; currency; language; type of tax; suppliers;

There are several ways to display multilingual resources:

First, in the custom class Webviewpage<tmodel> placed the method: T (), through this method, the Web page in the display of the corresponding language text.

In fact, T is just a proxy, the proxy is defined as:

Namespace Nop.Web.Framework.Localization 
{public 
  delegate localizedstring localizer (string text, params Object[] args); 

This proxy returns a value type of localizedstring, which inherits the interface ihtmlstring to ensure that localized text resources are displayed correctly.

The definition of ihtmlstring is:

Summary: 
//   represents an HTML-encoded string that should not be encoded again. Public 
Interface Ihtmlstring 
{ 
  //Summary: 
  //   Returns an HTML-encoded string. //// 
  return Result: 
  //   HTML-encoded string. 
  string tohtmlstring (); 

Second, through the expansion of HtmlHelper

Class Htmlextensions extends the HtmlHelper class,

The main is the encapsulation of some controls, and support for multiple languages.

Method Localizededitor<t, Tlocalizedmodellocal> is the encapsulation of the TabStrip control of the Telerik (that is, the tab control---the Multiple-page check control). When multiple languages are supported at the same time, more than one page sign is displayed for each language, but only when needed. Interface Ilocalizedmodel and interface ilocalizedmodellocal are used in this area. Interface Ilocalizedmodel is used to indicate that a model class supports this multi-language display, which includes multiple language data list locales, and the class that implements the interface ilocalizedmodellocal is the data of a particular language. Localizededitor method is based on these interfaces to achieve the support of multiple language page signing. The admin project uses this method and the Web project is not used.

public static Helperresult Localizededitor<t, tlocalizedmodellocal> (This htmlhelper<t> helper, string name , Func<int, helperresult> localizedtemplate, func<t, helperresult> standardtemplate) where t:ilocal Izedmodel<tlocalizedmodellocal> where tlocalizedmodellocal:ilocalizedmodellocal {return new HelperResult (w Riter => {if helper. ViewData.Model.Locales.Count > 1) {var TabStrip = helper. Telerik (). TabStrip (). Name (name). Items (x => {x.add ()). Text ("Standard"). Content (helper, standardtemplate. 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 way to implement multiple languages.

This method mainly implements a description of the property name based on the subclass nopresourcedisplayname of the attribute Displaynameattribute. This attribute is a decoration of the model property to specify the name of the property.

For example, the properties of a 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 the HtmlHelper extension method noplabelfor 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 Nopresourc 
    Edisplayname; if (resourcedisplayname!= null && displayhint) {var langId = Enginecontext.current.resolve<iworkc Ontext> (). 
      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 ()); }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.