Nopcommerce architecture Analysis of eight------multi-lingual

Source: Internet
Author: User
Tags locale nopcommerce

The language supported by the system is a class: language expression;

The corresponding classes of multi-language resources are: Localizedproperty;

Yourselves select a language to store in the class: Genericattribute;

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

Iworkcontext and its entity classes are webworkcontext the current run context, and the user's login information and some contextual settings are saved in this class.

Details include: Current user information: Currentcustomer; current user cookie; currency; language; type of tax; supplier, etc.;

There are several ways to show multilingual resources:

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

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

[CSharp]View PlainCopy
    1. Namespace Nop.Web.Framework.Localization
    2. {
    3. public Delegate localizedstring Localizer (string text, params object[] args);
    4. }


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

The ihtmlstring is defined as:

[CSharp]View PlainCopy
  1. Summary:
  2. //Represents an HTML-encoded string that should not be encoded again.
  3. public interface ihtmlstring
  4. {
  5. //Abstract:
  6. //Returns an HTML-encoded string.
  7. //  
  8. //Return Result:
  9. //HTML-encoded string.
  10. string tohtmlstring ();
  11. }


Second, through the expansion of HtmlHelper

Class Htmlextensions extends the HtmlHelper class,

It is mainly for the encapsulation of some controls and supports multiple languages.

Method Localizededitor<t, Tlocalizedmodellocal> is the encapsulation of the Telerik TabStrip control (that is, the Multi-tab Control---tab control). When the system supports multiple languages, it displays a tab for each language, but only when needed. The interface Ilocalizedmodel and interface ilocalizedmodellocal are used in this area. The interface Ilocalizedmodel is used to indicate that a model class supports this multi-language display, which includes multiple language data lists locales, the class that implements the interface ilocalizedmodellocal is the data of a particular language. The Localizededitor method is to implement support for multiple language page signatures based on the interface's mates. The admin project uses this method, and the Web project is not used.

[CSharp]View PlainCopy
  1. Public static Helperresult localizededitor<t, tlocalizedmodellocal> (thishtmlhelper<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. }


Extension method Noplabelfor<tmodel, tvalue> is another way to implement multiple languages.

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

For example, the properties of a class Addnewscommentmodel are specified with the Nopresourcedisplayname attribute:

[CSharp]View PlainCopy
  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. [Nopresourcedisplayname ("News.Comments.CommentText")]
  9. [Allowhtml]
  10. public string Commenttext { get;  set;}
  11. public bool Displaycaptcha { get;  set;}
  12. }
  13. }


The implementation of the HtmlHelper extension method noplabelfor is as follows:

[CSharp]View PlainCopy
    1. Public static mvchtmlstring Noplabelfor<tmodel, tvalue> (thishtmlhelper<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. Result. Append (helper. Hint (Hintresource). Tohtmlstring ());
    17. }
    18. }
    19. Result. Append (helper.  Labelfor (expression, new {title = Hintresource});
    20. return mvchtmlstring.create (result.  ToString ());
    21. }

Nopcommerce architecture Analysis of eight------multi-lingual

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.