public override void OnFinish(object sender, EventArgs e)
        {
            var uploadedFile = GetBinding <UploadedFile>("UploadedFile");

            using (var sr = new StreamReader(uploadedFile.FileStream))
            {
                var txt = sr.ReadToEnd();

                var importModel = ImportExportModel.FromXml(txt);
                var importer    = new ResourceImporter(importModel);

                var result = importer.Import();

                var sb = new StringBuilder();

                sb.AppendLine("Languages: " + String.Join(", ", result.Languages.Select(l => l.Name)));
                sb.AppendLine("ResourceSets: " + String.Join(", ", result.ResourceSets.Select(s => s ?? "Website")));
                sb.AppendLine();
                sb.AppendLine("Keys added: " + result.KeysAdded);
                sb.AppendLine("Values added: " + result.ValuesAdded);
                sb.AppendLine("Values updated: " + result.ValuesUpdated + " (were the same: " + result.ValuesWereTheSame + ")");

                ShowMessage(DialogType.Message, "Import result", sb.ToString());
            }
        }
Пример #2
0
        public ViewResult ImportResources(bool?previewImport, HttpPostedFileBase importFile, bool?showMenu)
        {
            var model = new ImportResourcesViewModel {
                ShowMenu = showMenu ?? false
            };

            if (importFile == null || importFile.ContentLength == 0)
            {
                return(View("ImportResources", model));
            }

            var fileInfo = new FileInfo(importFile.FileName);

            if (fileInfo.Extension.ToLower() != ".json")
            {
                ModelState.AddModelError("file", "Uploaded file has different extension. Json file expected");
                return(View("ImportResources", model));
            }

            var importer     = new ResourceImporter();
            var serializer   = new JsonDataSerializer();
            var streamReader = new StreamReader(importFile.InputStream);
            var fileContent  = streamReader.ReadToEnd();

            try
            {
                var newResources = serializer.Deserialize <IEnumerable <LocalizationResource> >(fileContent);

                if (previewImport.HasValue && previewImport.Value)
                {
                    var changes = importer.DetectChanges(newResources, new GetAllResources.Query().Execute());

                    var availableLanguagesQuery = new AvailableLanguages.Query();
                    var languages = availableLanguagesQuery.Execute();

                    var previewModel = new PreviewImportResourcesViewModel(changes, showMenu ?? false, languages);

                    return(View("ImportPreview", previewModel));
                }

                var result = importer.Import(newResources, previewImport ?? true);

                ViewData["LocalizationProvider_ImportResult"] = result;
            }
            catch (Exception e)
            {
                ModelState.AddModelError("importFailed", $"Import failed! Reason: {e.Message}");
            }

            return(View("ImportResources", model));
        }
Пример #3
0
        public ViewResult ImportResources(bool?importOnlyNewContent, HttpPostedFileBase importFile, bool?showMenu)
        {
            var model = new ImportResourcesViewModel
            {
                ShowMenu = showMenu ?? false
            };

            if (importFile == null || importFile.ContentLength == 0)
            {
                return(View("ImportResources", model));
            }

            var fileInfo = new FileInfo(importFile.FileName);

            if (fileInfo.Extension.ToLower() != ".json")
            {
                ModelState.AddModelError("file", "Uploaded file has different extension. Json file expected");
                return(View("ImportResources", model));
            }

            var importer     = new ResourceImporter();
            var serializer   = new JsonDataSerializer();
            var streamReader = new StreamReader(importFile.InputStream);
            var fileContent  = streamReader.ReadToEnd();

            try
            {
                var newResources = serializer.Deserialize <IEnumerable <LocalizationResource> >(fileContent);
                var result       = importer.Import(newResources, importOnlyNewContent ?? true);

                ViewData["LocalizationProvider_ImportResult"] = result;
            }
            catch (Exception e)
            {
                ModelState.AddModelError("importFailed", $"Import failed! Reason: {e.Message}");
            }

            return(View("ImportResources", model));
        }