Ejemplo n.º 1
0
        public void TestConvertDocx2Json()
        {
            //make sure test files exist
            TestingTestDocxFile();

            //Valid Schema for Json Data to be exported exported
            //NOTE: I used https://jsonschema.net/ to help me build the schema
            JsonSchema schema = JsonSchema.Parse(ValueRepository.JsonSchema);

            //Test Service Methods
            DocxConversionData docxFileData = docxFileService.CreateCleanedCopy(FullDocxTestFilePath, false);
            JsonConversionData jsonFileData = docxJsonService.ExportStringsToJsonFile(docxFileData);

            Assert.AreEqual("Success", jsonFileData.Messages[0]);
            //Validate Json being exported has a valid schema
            Assert.IsTrue(jsonFileData.JsonData.IsValid(schema));
            //Validate that exported Json contains the same number of strings to be translated
            Assert.AreEqual(jsonFileData.TotalStrings2Translate, docxFileData.TotalStrings2Translate);

            //Clean Up file that was created
            File.Delete(docxFileData.FullPath);
        }
Ejemplo n.º 2
0
        public JsonConversionData ExtractStringsToJsonFile(string fullFilePath)
        {
            if (string.IsNullOrEmpty(fullFilePath))
            {
                throw new ArgumentOutOfRangeException("The file path cannot be null or empty.");
            }

            //Extract Docx data out information from Docx file
            DocxConversionData cleanedDocxFileData = docxFileService.CreateCleanedCopy(fullFilePath, true);

            //Handle Errors
            if (cleanedDocxFileData.Messages[0] != "Success")
            {
                var errorResult = new JsonConversionData(fullFilePath);
                errorResult.Messages.Add(cleanedDocxFileData.Messages[0]);
                return(errorResult);
            }

            // We don't need the "cleaned" copy of the docx any longer, so delete it
            if (File.Exists(cleanedDocxFileData.FullPath))
            {
                File.Delete(cleanedDocxFileData.FullPath);
            }

            // We want the json file we create to have the same name as the original file, so
            // restore the original path in our data object
            var origFileData = new DocxConversionData(fullFilePath)
            {
                TotalStrings2Translate = cleanedDocxFileData.TotalStrings2Translate,
                Strings2Translate      = cleanedDocxFileData.Strings2Translate
            };

            origFileData.Messages.AddRange(cleanedDocxFileData.Messages);

            //translate data to target language
            List <string> TranslatedStrings = new List <string>();

            foreach (string line in origFileData.Strings2Translate)
            {
                string TranslatedLine = TextTranslator.TranslateText(line, "es", "en");
                TranslatedStrings.Add(TranslatedLine);
            }
            origFileData.Strings2Translate = (IEnumerable <string>)TranslatedStrings;

            //Convert docx data to json data
            JsonConversionData jsonFileData = docxJSonService.ExportStringsToJsonFile(origFileData);

            //Handle Errors
            //if (jsonFileData.Messages[0] != "Success")
            //{
            //    return jsonFileData;
            //}

            //Delete any Json file that might have the same name as the new one we are creating.
            if (File.Exists(jsonFileData.FullPath))
            {
                try
                {
                    File.Delete(jsonFileData.FullPath);
                }
                catch (Exception e)
                {
                    var errorResult = new JsonConversionData(fullFilePath);
                    errorResult.Messages.Add(e.Message);
                    return(errorResult);
                }
            }

            //Export Json data to file
            var result = jsonIoService.SaveJson(jsonFileData);

            //Handle Errors
            if (result != "Success")
            {
                jsonFileData.Messages.Insert(0, "Failed");
            }

            return(jsonFileData);
        }