受欢迎的博客标签

NopCommerce 4.x - @T

Published

why is @T used in NopCommerce and what are its benefits / usage?

It is used for localization. You can find same pattern in Orchard CMS as well.

NOP Commerce stores resources (key value pairs) used for localization in database. While accessing, it does cache them and uses their engine to access these resources.

@T is just a method to access to resources while using differnt langs in the app.

 

step 1:Localizer[""];

@using Microsoft.AspNetCore.Mvc.Localization

@inject IViewLocalizer Localizer

@{
    ViewData["Title"] = Localizer["About"];
}
<h2>@ViewData["Title"].</h2>
<h3>@ViewData["Message"]</h3>

<p>@Localizer["Use this area to provide additional information."]</p>

 

step 2: @T[""]

path:\src\Presentation\Nop.Web\Themes\RootTheme\Views\_ViewImports.cshtml

@using Microsoft.AspNetCore.Mvc.Localization
@inject IViewLocalizer T

 

  <label>@T["Blog.Tags"] :</label>

step 3:@T("")

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Localization;

namespace Nop.Web.Framework.Localization
{
    //see https://github.com/OrchardCMS/Orchard2/blob/master/src/Orchard.Abstractions/Localization/Localizer.cs
    /// <summary>
    /// Localizes some text based on the current Work Context culture
    /// </summary>
    /// <param name="text">The text format to localize</param>
    /// <param name="args">The arguments used in the text format. The arguments are HTML-encoded if they don't implement <see cref="System.Web.IHtmlString"/>.</param>
    /// <returns>An HTML-encoded localized string</returns>
    public delegate LocalizedString Localizer(string text, params object[] args);
}

 

D:\tmp\nop43\nopCommerce-release-4.30\src\Presentation\Nop.Web.Framework\Mvc\Razor\NopRazorPage.cs

using Nop.Core;
using Nop.Core.Infrastructure;
using Nop.Services.Localization;
using Nop.Services.Themes;
using Nop.Web.Framework.Localization;
using Nop.Web.Framework.Themes;

namespace Nop.Web.Framework.Mvc.Razor
{
    /// <summary>
    /// Web view page
    /// </summary>
    /// <typeparam name="TModel">Model</typeparam>
    public abstract class NopRazorPage<TModel> : Microsoft.AspNetCore.Mvc.Razor.RazorPage<TModel>
    {
        private ILocalizationService _localizationService;
        private Localizer _localizer;

        /// <summary>
        /// Get a localized resources
        /// </summary>
        public Localizer T
        {
            get
            {
                if (_localizationService == null)
                    _localizationService = EngineContext.Current.Resolve<ILocalizationService>();

                if (_localizer == null)
                {
                    _localizer = (format, args) =>
                    {
                        var resFormat = _localizationService.GetResource(format);
                        if (string.IsNullOrEmpty(resFormat))
                        {
                            return new LocalizedString(format);
                        }
                        return new LocalizedString((args == null || args.Length == 0)
                            ? resFormat
                            : string.Format(resFormat, args));
                    };
                }
                return _localizer;
            }
        }

        /// <summary>
        /// Return a value indicating whether the working language and theme support RTL (right-to-left)
        /// </summary>
        /// <returns></returns>
        public bool ShouldUseRtlTheme()
        {
            var workContext = EngineContext.Current.Resolve<IWorkContext>();
            var supportRtl = workContext.WorkingLanguage.Rtl;
            if (supportRtl)
            {
                //ensure that the active theme also supports it
                var themeProvider = EngineContext.Current.Resolve<IThemeProvider>();
                var themeContext = EngineContext.Current.Resolve<IThemeContext>();
                supportRtl = themeProvider.GetThemeBySystemName(themeContext.WorkingThemeName)?.SupportRtl ?? false;
            }
            return supportRtl;
        }
    }

    /// <summary>
    /// Web view page
    /// </summary>
    public abstract class NopRazorPage : NopRazorPage<dynamic>
    {
    }
}

D:\tmp\nop43\nopCommerce-release-4.30\src\Presentation\Nop.Web\Views\_ViewImports.cshtml

@inherits Nop.Web.Framework.Mvc.Razor.NopRazorPage<TModel>
  <label>@T("Blog.Tags"):</label>

call

AjaxCartFailure: "@T("AjaxCart.Failure")"