Exemplo n.º 1
0
        private void PopulateLocaleResources()
        {
            // Default primary language
            var language = _ctx.Set <Language>().Single();

            var locPath = CommonHelper.MapPath("~/App_Data/Localization/App/" + language.LanguageCulture);

            if (!System.IO.Directory.Exists(locPath))
            {
                // Fallback to neutral language folder (de, en etc.)
                locPath = CommonHelper.MapPath("~/App_Data/Localization/App/" + language.UniqueSeoCode);
            }

            var localizationService = this.LocalizationService;

            // save resources
            foreach (var filePath in System.IO.Directory.EnumerateFiles(locPath, "*.smres.xml", SearchOption.TopDirectoryOnly))
            {
                var doc = new XmlDocument();
                doc.Load(filePath);

                doc = localizationService.FlattenResourceFile(doc);

                // now we have a parsed XML file (the same structure as exported language packs)
                // let's save resources
                localizationService.ImportResourcesFromXml(language, doc);

                // no need to call SaveChanges() here, as the above call makes it
                // already without AutoDetectChanges(), so it's fast.
            }

            MigratorUtils.ExecutePendingResourceMigrations(locPath, _ctx);
        }
Exemplo n.º 2
0
        private void ExecutePendingResourceMigrations(string resPath)
        {
            string headPath = Path.Combine(resPath, "head.txt");

            if (!File.Exists(headPath))
            {
                return;
            }

            string resHead = File.ReadAllText(headPath);

            if (!MigratorUtils.IsValidMigrationId(resHead))
            {
                return;
            }

            var migrator   = new DbMigrator(new MigrationsConfiguration());
            var migrations = GetPendingResourceMigrations(migrator, resHead);

            foreach (var id in migrations)
            {
                if (MigratorUtils.IsAutomaticMigration(id))
                {
                    continue;
                }

                if (!MigratorUtils.IsValidMigrationId(id))
                {
                    continue;
                }

                // Resolve and instantiate the DbMigration instance from the assembly
                var migration = MigratorUtils.CreateMigrationInstanceByMigrationId(id, migrator.Configuration);

                var provider = migration as ILocaleResourcesProvider;
                if (provider == null)
                {
                    continue;
                }

                var builder = new LocaleResourcesBuilder();
                provider.MigrateLocaleResources(builder);

                var resEntries  = builder.Build();
                var resMigrator = new LocaleResourcesMigrator(_ctx);
                resMigrator.Migrate(resEntries);
            }
        }