Webiant Logo Webiant Logo
  1. No results found.

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

MigrationExtension.cs

using System.Globalization;
using FluentMigrator;
using Nop.Core.Domain.Localization;
using Nop.Core.Infrastructure;
using Nop.Services.Common;
using Nop.Services.Localization;

namespace Nop.Web.Framework.Extensions;

public static partial class MigrationExtension
{
    /// 
    /// Get language data
    /// 
    /// Default language identifier and all languages
    public static (int? defaultLanguageId, IList allLanguages) GetLanguageData(this IMigration _, ILanguageService languageService = null)
    {
        languageService ??= EngineContext.Current.Resolve();

        var languages = languageService.GetAllLanguages(true);
        var languageId = languages
            .FirstOrDefault(lang => lang.UniqueSeoCode == new CultureInfo(NopCommonDefaults.DefaultLanguageCulture).TwoLetterISOLanguageName)?.Id;

        return (languageId, languages);
    }

    /// 
    /// Rename locales
    /// 
    /// Migration
    /// Locales to rename. Key - old name, Value - new name
    /// All languages
    /// Localization service
    public static void RenameLocales(this IMigration _, Dictionary localesToRename, IList allLanguages, ILocalizationService localizationService)
    {
        foreach (var lang in allLanguages)
        foreach (var locale in localesToRename)
        {
            var lsr = localizationService.GetLocaleStringResourceByName(locale.Key, lang.Id, false);
            
            if (lsr == null) 
                continue;

            var exist = localizationService.GetLocaleStringResourceByName(locale.Value, lang.Id, false);

            if (exist != null)
                localizationService.DeleteLocaleStringResource(lsr);
            else
            {
                lsr.ResourceName = locale.Value.ToLowerInvariant();
                localizationService.UpdateLocaleStringResource(lsr);
            }
        }
    }
}