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
- Namespace Nop.Web.Framework.Localization
- {
- public Delegate localizedstring Localizer (string text, params object[] args);
- }
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
- Summary:
- //Represents an HTML-encoded string that should not be encoded again.
- public interface ihtmlstring
- {
- //Abstract:
- //Returns an HTML-encoded string.
- //
- //Return Result:
- //HTML-encoded string.
- string tohtmlstring ();
- }
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
- Public static Helperresult localizededitor<t, tlocalizedmodellocal> (thishtmlhelper<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 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
- 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:
[CSharp]View PlainCopy
- Public static mvchtmlstring Noplabelfor<tmodel, tvalue> (thishtmlhelper<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 ());
- }
Nopcommerce architecture Analysis of eight------multi-lingual