public void TestBuildDocxConversionData() { //Make sure Test File Exists TestingTestDocxFile(); //Test Service Method DocxConversionData docxFileData = docxFileService.CreateCleanedCopy(FullDocxTestFilePath, false); Assert.IsNotNull(docxFileData); Assert.AreEqual("Success", docxFileData.Messages[0]); Assert.IsTrue(docxFileData.TotalStrings2Translate > 0); Assert.IsTrue(File.Exists(docxFileData.FullPath)); //Clean Up file that was created File.Delete(docxFileData.FullPath); }
public Json2Docx(string fullJsonTestFilePath, string fullDocxTestFilePath, IDocxFileService docxFileService, IDocxJsonService jsonService, IMergeService mergeService) { JsonConversionData jsonFileData = jsonService.BuildJsonConversionData(fullJsonTestFilePath); //Handle Errors if (jsonFileData.Messages[0] != "Success") { Response = jsonFileData.Messages[0]; return; } DocxConversionData docxFileData = docxFileService.CreateCleanedCopy(fullDocxTestFilePath, false); //Handle Errors if (docxFileData.Messages[0] != "Success") { Response = docxFileData.Messages[0]; return; } Response = mergeService.MergeJson2Docx(jsonFileData, docxFileData); if (Response == "Success") { //rename DocxFile string docxConversionDataFullPath = docxFileData.FullPath; string mergedFileName = docxFileData.GetTranslatedFullPath(DateTime.Now); try { File.Move(docxConversionDataFullPath, mergedFileName); if (File.Exists(mergedFileName)) { FilePath = mergedFileName; } else { Response = "Error saving merged Docx file: " + mergedFileName; } } catch (Exception e) { Response = e.Message; } } }
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); }