Webiant Logo Webiant Logo
  1. No results found.

    Try your search with a different keyword or use * as a wildcard.

LocalizedModelFactory.cs

using Nop.Services.Localization;
using Nop.Web.Framework.Models;

namespace Nop.Web.Framework.Factories;

/// 
/// Represents the base localized model factory implementation
/// 
public partial class LocalizedModelFactory : ILocalizedModelFactory
{
    #region Fields
        
    protected readonly ILanguageService _languageService;

    #endregion

    #region Ctor

    public LocalizedModelFactory(ILanguageService languageService)
    {
        _languageService = languageService;
    }

    #endregion

    #region Methods

    /// 
    /// Prepare localized model for localizable entities
    /// 
    /// Localized model type
    /// Model configuration action
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the list of localized model
    /// 
    public virtual async Task> PrepareLocalizedModelsAsync(Func configure = null) where T : ILocalizedLocaleModel
    {
        //get all available languages
        var availableLanguages = await _languageService.GetAllLanguagesAsync(true);

        //prepare models
        var localizedModels = await availableLanguages.SelectAwait(async language =>
        {
            //create localized model
            var localizedModel = Activator.CreateInstance();

            //set language
            localizedModel.LanguageId = language.Id;

            //invoke the model configuration action
            if (configure != null)
                await configure(localizedModel, localizedModel.LanguageId);

            return localizedModel;
        }).ToListAsync();

        return localizedModels;
    }

    #endregion
}