public static void TranslatePOwithAnotherPO(string BasePO, string TargetPo) { Po BPo = null, TPo = null; using (DataStream name = DataStreamFactory.FromFile(BasePO, FileOpenMode.Read)) using (BinaryFormat binaryname = new BinaryFormat(name)) { BPo = (Po)ConvertFormat.With <Po2Binary>(binaryname); } using (DataStream name = DataStreamFactory.FromFile(TargetPo, FileOpenMode.Read)) using (BinaryFormat binaryname = new BinaryFormat(name)) { TPo = (Po)ConvertFormat.With <Po2Binary>(binaryname); } foreach (PoEntry entryBPo in BPo.Entries) { foreach (PoEntry entryTPo in TPo.Entries) { if (entryBPo.Original == entryTPo.Original && (entryBPo.Translated != null && entryBPo.Translated != "")) { entryTPo.Translated = entryBPo.Translated; if (entryBPo.TranslatorComment != string.Empty && entryBPo.TranslatorComment != null && entryBPo.TranslatorComment.Trim() != "") { entryTPo.TranslatorComment = entryBPo.TranslatorComment; } } } } ConvertFormat.To <BinaryFormat>(TPo).Stream.WriteTo(TargetPo); }
public void ConvertNoHeaderEntries() { var testPo = new Po(); testPo.Add(new PoEntry { Original = "original" }); testPo.Add(new PoEntry { Original = "totranslate", Translated = "translated" }); string text = @" msgid ""original"" msgstr """" msgid ""totranslate"" msgstr ""translated"" "; text = text.Replace("\r\n", "\n"); var newPo = ConvertStringToPo(text); CompareText(ConvertFormat.To <BinaryFormat>(testPo), text); Assert.IsNull(newPo.Header); Assert.AreEqual(2, newPo.Entries.Count); }
public void ConvertBasicHeaderNoEntries() { var po = new Po(new PoHeader("myId", "yo", "es") { CreationDate = "today" }); string text = @"msgid """" msgstr """" ""Project-Id-Version: myId\n"" ""Report-Msgid-Bugs-To: yo\n"" ""POT-Creation-Date: today\n"" ""PO-Revision-Date: \n"" ""Last-Translator: \n"" ""Language-Team: \n"" ""Language: es\n"" ""MIME-Version: 1.0\n"" ""Content-Type: text/plain; charset=UTF-8\n"" ""Content-Transfer-Encoding: 8bit\n"" "; text = text.Replace("\r\n", "\n"); var newPo = ConvertStringToPo(text); CompareText(ConvertFormat.To <BinaryFormat>(po), text); Assert.AreEqual(po.Header.ProjectIdVersion, newPo.Header.ProjectIdVersion); Assert.AreEqual(po.Header.ReportMsgidBugsTo, newPo.Header.ReportMsgidBugsTo); Assert.AreEqual(po.Header.CreationDate, newPo.Header.CreationDate); Assert.AreEqual(po.Header.Language, newPo.Header.Language); Assert.IsEmpty(newPo.Header.LastTranslator); Assert.IsEmpty(newPo.Entries); }
static Po ConvertStringToPo(string binary) { using BinaryFormat textFormat = new BinaryFormat(); new TextWriter(textFormat.Stream).Write(binary); textFormat.Stream.Position = 0; return(ConvertFormat.To <Po>(textFormat)); }
public void ConvertToWithTypeThrowsIfTypeIsNull() { Type dstType = null; Assert.That( () => ConvertFormat.To(dstType, "3"), Throws.ArgumentNullException); }
public void ConvertToThrowsIfSrcIsNull() { Assert.That( () => ConvertFormat.To <int>(null), Throws.ArgumentNullException); Assert.That( () => ConvertFormat.To(typeof(int), null), Throws.ArgumentNullException); }
public void ConvertFromDerivedWithBaseConverter() { var format = new Derived { Y = 11, X = 10 }; int conv = 0; Assert.DoesNotThrow(() => conv = ConvertFormat.To <int>(format)); Assert.AreEqual(15, conv); }
public void ConvertFromBaseWithDerivedConverterThrows() { // We cannot do the inverse, from base type use the derived converter Base val = new Base { X = 3 }; Assert.Throws <InvalidOperationException>( () => ConvertFormat.To <ushort>(val)); }
public void ConvertToThrowsIfNoConverter() { var ex = Assert.Throws <InvalidOperationException>(() => ConvertFormat.To(typeof(short), (short)3)); Assert.AreEqual( "Cannot find converter for: " + $"{typeof(short).FullName} -> {typeof(short).FullName}", ex.Message); }
public void ConvertToThrowsIfThereAreTwoEqualConverters() { var test = new StringFormatTest("3"); var ex = Assert.Throws <InvalidOperationException>(() => ConvertFormat.To(typeof(short), test)); Assert.AreEqual( "Multiple converters for: " + $"{typeof(StringFormatTest).FullName} -> {typeof(short).FullName}", ex.Message); }
public void ConvertFromImplementationWithInterfaceConverter() { var format = new InterfaceImpl { Z = 14 }; int conv = 0; Assert.DoesNotThrow(() => conv = (int)ConvertFormat.With <ConverterInterface>(format)); Assert.AreEqual(14, conv); Assert.DoesNotThrow(() => conv = ConvertFormat.To <int>(format)); Assert.AreEqual(14, conv); }
/// <summary> /// Reads a format from this stream. /// </summary> /// <returns>The format read.</returns> /// <typeparam name="T">The type of the format to read.</typeparam> public T ReadFormat <T>() { if (Disposed) { throw new ObjectDisposedException(nameof(DataStream)); } T format; using (var binary = new BinaryFormat(this)) format = ConvertFormat.To <T>(binary); return(format); }
public void ConvertToDerivedWithDerivedConverter() { // Just to validate converter, derived with derived converter Derived derived = null; Assert.DoesNotThrow(() => derived = ConvertFormat.To <Derived>((ushort)4)); Assert.AreEqual(5, derived.Y); Assert.AreEqual(4, derived.X); ushort conv = 0; Assert.DoesNotThrow(() => conv = ConvertFormat.To <ushort>(derived)); Assert.AreEqual(5, conv); }
public void ConvertFullHeaderNoEntries() { var header = new PoHeader("myId", "yo", "SC") { CreationDate = "today", RevisionDate = "tomorrow", LastTranslator = "she", LanguageTeam = "bestteam", PluralForms = "pl", }; header.Extensions["Generator"] = "yarhl"; header.Extensions["Hey"] = "hoy"; var testPo = new Po(header); string text = @"msgid """" msgstr """" ""Project-Id-Version: myId\n"" ""Report-Msgid-Bugs-To: yo\n"" ""POT-Creation-Date: today\n"" ""PO-Revision-Date: tomorrow\n"" ""Last-Translator: she\n"" ""Language-Team: bestteam\n"" ""Language: SC\n"" ""MIME-Version: 1.0\n"" ""Content-Type: text/plain; charset=UTF-8\n"" ""Content-Transfer-Encoding: 8bit\n"" ""Plural-Forms: pl\n"" ""X-Generator: yarhl\n"" ""X-Hey: hoy\n"" "; text = text.Replace("\r\n", "\n"); var newPo = ConvertStringToPo(text); var newHeader = newPo.Header; CompareText(ConvertFormat.To <BinaryFormat>(testPo), text); Assert.AreEqual(header.ProjectIdVersion, newHeader.ProjectIdVersion); Assert.AreEqual(header.ReportMsgidBugsTo, newHeader.ReportMsgidBugsTo); Assert.AreEqual(header.CreationDate, newHeader.CreationDate); Assert.AreEqual(header.RevisionDate, newHeader.RevisionDate); Assert.AreEqual(header.LastTranslator, newHeader.LastTranslator); Assert.AreEqual(header.LanguageTeam, newHeader.LanguageTeam); Assert.AreEqual(header.Language, newHeader.Language); Assert.AreEqual(header.PluralForms, newHeader.PluralForms); Assert.That(header.Extensions["Generator"], Is.EqualTo(newHeader.Extensions["Generator"])); Assert.That(header.Extensions["Hey"], Is.EqualTo(newHeader.Extensions["Hey"])); Assert.IsEmpty(newPo.Entries); }
public void ConvertToBaseWithDerivedConverter() { // It should use the converter: ushort -> Derived // The converter will generate a derived type and will cast-down // to base. Base val = null; Assert.DoesNotThrow(() => val = ConvertFormat.To <Base>((ushort)3)); Assert.IsInstanceOf <Derived>(val); Assert.AreEqual(3, val.X); Assert.DoesNotThrow(() => val = ConvertFormat.To <Base>((int)3)); Assert.IsInstanceOf <Base>(val); Assert.AreEqual(5, val.X); }
/// <summary> /// Transforms the node format to the specified format. /// </summary> /// <typeparam name="TDst">Format to convert.</typeparam> /// <returns>This node.</returns> public Node TransformTo <TDst>() where TDst : IFormat { if (Disposed) { throw new ObjectDisposedException(nameof(Node)); } if (Format == null) { throw new InvalidOperationException( "Cannot transform a node without format"); } ChangeFormat(ConvertFormat.To <TDst>(Format)); return(this); }
public void ConvertToThrowsIfConstructorFails() { using var test = new StringFormatTest { Value = "3" }; var ex = Assert.Throws <Exception>(() => ConvertFormat.To(typeof(ushort), test)); Assert.AreEqual( "Exception of type 'System.Exception' was thrown.", ex.Message); // Just for coverage var converter = new FormatTestBadConstructor("2"); Assert.That( converter.Convert(new StringFormatTest("3")), Is.EqualTo(3)); }
public void ConvertFullEntry() { var testPo = new Po(); testPo.Add(new PoEntry { Original = "original", Translated = "translated", TranslatorComment = "a comment", ExtractedComments = "hehe", Reference = "ref1", Flags = "flag1,flag2", PreviousContext = "prev ctx", PreviousOriginal = "prev org", Context = "a ctx", }); string text = @" # a comment #. hehe #: ref1 #, flag1,flag2 #| msgctxt prev ctx #| msgid prev org msgctxt ""a ctx"" msgid ""original"" msgstr ""translated"" "; text = text.Replace("\r\n", "\n"); var newPo = ConvertStringToPo(text); CompareText(ConvertFormat.To <BinaryFormat>(testPo), text); Assert.AreEqual(1, newPo.Entries.Count); Assert.AreEqual(testPo.Entries[0].Original, newPo.Entries[0].Original); Assert.AreEqual(testPo.Entries[0].Translated, newPo.Entries[0].Translated); Assert.AreEqual(testPo.Entries[0].TranslatorComment, newPo.Entries[0].TranslatorComment); Assert.AreEqual(testPo.Entries[0].ExtractedComments, newPo.Entries[0].ExtractedComments); Assert.AreEqual(testPo.Entries[0].Reference, newPo.Entries[0].Reference); Assert.AreEqual(testPo.Entries[0].Flags, newPo.Entries[0].Flags); Assert.AreEqual(testPo.Entries[0].PreviousContext, newPo.Entries[0].PreviousContext); Assert.AreEqual(testPo.Entries[0].PreviousOriginal, newPo.Entries[0].PreviousOriginal); Assert.AreEqual(testPo.Entries[0].Context, newPo.Entries[0].Context); }
public void ConvertEntryAndHeader() { var testPo = new Po(new PoHeader("myId", "yo", "es") { CreationDate = "today" }); testPo.Add(new PoEntry { Original = "original", Translated = "translated" }); string text = @"msgid """" msgstr """" ""Project-Id-Version: myId\n"" ""Report-Msgid-Bugs-To: yo\n"" ""POT-Creation-Date: today\n"" ""PO-Revision-Date: \n"" ""Last-Translator: \n"" ""Language-Team: \n"" ""Language: es\n"" ""MIME-Version: 1.0\n"" ""Content-Type: text/plain; charset=UTF-8\n"" ""Content-Transfer-Encoding: 8bit\n"" msgid ""original"" msgstr ""translated"" "; text = text.Replace("\r\n", "\n"); var newPo = ConvertStringToPo(text); CompareText(ConvertFormat.To <BinaryFormat>(testPo), text); Assert.AreEqual(testPo.Header.ProjectIdVersion, newPo.Header.ProjectIdVersion); Assert.AreEqual(testPo.Header.ReportMsgidBugsTo, newPo.Header.ReportMsgidBugsTo); Assert.AreEqual(testPo.Header.Language, newPo.Header.Language); Assert.AreEqual(testPo.Header.CreationDate, newPo.Header.CreationDate); Assert.AreEqual(1, newPo.Entries.Count); Assert.AreEqual(testPo.Entries[0].Original, newPo.Entries[0].Original); Assert.AreEqual(testPo.Entries[0].Translated, newPo.Entries[0].Translated); }
public void ConvertNeedsToBeHiddenIfConstructorsHaveArgs() { // With MEF we can't have an extension without a default constructor // because it will throw an exception in every general request. // So we need to hide those extensions. using var test = new StringFormatTest("3"); var ex = Assert.Throws <InvalidOperationException>(() => ConvertFormat.To(typeof(long), test)); Assert.AreEqual( "Cannot find converter for: " + $"{typeof(StringFormatTest).FullName} -> {typeof(long).FullName}", ex.Message); // But we can use the With() var converter = new FormatTestNoConstructor("3"); Assert.AreEqual( 0, ConvertFormat.With(converter, new StringFormatTest("1"))); }
public void ConvertNeedsToBeHiddenIfNoPublicConstructor() { // With MEF we can't have an extension without a default constructor // because it will throw an exception in every general request. // So we need to hide those extensions. var test = new StringFormatTest("3"); var ex = Assert.Throws <InvalidOperationException>(() => ConvertFormat.To(typeof(ulong), test)); Assert.AreEqual( "Cannot find converter for: " + $"{typeof(StringFormatTest).FullName} -> {typeof(ulong).FullName}", ex.Message); // But we can use the With() of classes with Factory pattern. var converter = FormatTestPrivateConstructor.Create(); Assert.AreEqual( ConvertFormat.With(converter, new StringFormatTest("1")), 0); }
/// <summary> /// Transforms the node format to the specified format. /// </summary> /// <returns>This node.</returns> /// <param name="dst">Format to convert. It must implement IFormat.</param> public Node TransformTo(Type dst) { if (Disposed) { throw new ObjectDisposedException(nameof(Node)); } if (dst == null) { throw new ArgumentNullException(nameof(dst)); } if (Format == null) { throw new InvalidOperationException( "Cannot transform a node without format"); } ChangeFormat((IFormat)ConvertFormat.To(dst, Format)); return(this); }
public void ConvertSpecialEntry() { var testPo = new Po(); testPo.Add(new PoEntry { Original = "original", Translated = "trans\nl\"a\"ted", }); string text = @" msgid ""original"" msgstr """" ""trans\n"" ""l\""a\""ted"" "; text = text.Replace("\r\n", "\n"); var newPo = ConvertStringToPo(text); CompareText(ConvertFormat.To <BinaryFormat>(testPo), text); Assert.AreEqual(1, newPo.Entries.Count); Assert.AreEqual(testPo.Entries[0].Original, newPo.Entries[0].Original); Assert.AreEqual(testPo.Entries[0].Translated, newPo.Entries[0].Translated); }
// Extract the text from "FIleName" and the Speaker from "Bytcode" and save it in ".po". public static void MakePO(List <string> ExtractedText, string Bytecode, string DestinationDir) { if (!Directory.Exists(DestinationDir)) { Directory.CreateDirectory(DestinationDir); } List <string> SpeakersExtracted = null; if (Bytecode != null) { SpeakersExtracted = SpeakerExtractor(Bytecode); } List <string> JAPText = null; // If the user has checked "Add japanase texts", then extract the japanese text. if (DRAT.aDDJAPANESETEXTToolStripMenuItem.Checked == true) { JAPText = ReadJAPText(Path.GetFileNameWithoutExtension(DestinationDir)); } //List<string> TRText = ExtractedText; //ExtractedText = ReadTranslatedRawText(Path.GetFileNameWithoutExtension(DestinationDir)); //Read the language used by the user' OS, this way the editor can spellcheck the translation. System.Globalization.CultureInfo currentCulture = System.Threading.Thread.CurrentThread.CurrentCulture; Po po = new Po { Header = new PoHeader(DRAT.tabControl1.SelectedTab.Text, "your_email", currentCulture.Name) }; for (int i = 0; i < ExtractedText.Count; i++) { PoEntry entry = new PoEntry(); // Print the "Speaker". if (DRAT.hIDESPEAKERSToolStripMenuItem.Checked == false && SpeakersExtracted != null) { if (i < SpeakersExtracted.Count) { entry.Context = $"{(i + 1).ToString("D4")} | {SpeakersExtracted[i]}"; } else { entry.Context = $"{(i + 1).ToString("D4")} | {"ERROR"}"; } } else { entry.Context = $"{i + 1:D4}"; // If there isn't a Speaker, then just print the sentence number } // Print the original sentence. if (ExtractedText[i] == string.Empty || ExtractedText[i] == null) { entry.Original = "[EMPTY_LINE]"; entry.Translated = "[EMPTY_LINE]"; } else if (ExtractedText[i].Length == 1 || ExtractedText[i] == " \n" || ExtractedText[i] == "\n" || ExtractedText[i] == "..." || ExtractedText[i] == "…" || ExtractedText[i] == "...\n" || ExtractedText[i] == "…\n" || ExtractedText[i] == "\"...\"" || ExtractedText[i] == "\"…\"" || ExtractedText[i] == "\"...\n\"" || ExtractedText[i] == "\"…\n\"") { entry.Original = ExtractedText[i]; entry.Translated = ExtractedText[i]; } else { entry.Original = ExtractedText[i]; //if (TRText[i] != "" || TRText[i] != null) // entry.Translated = TRText[i]; } // Print the japanese sentence. if (DRAT.aDDJAPANESETEXTToolStripMenuItem.Checked == true && JAPText != null) { if (i < JAPText.Count && JAPText != null && JAPText[i] != null && JAPText[i] != string.Empty) { entry.ExtractedComments = JAPText[i].Replace("\r\n", "\n#. ").Replace("\n\r", "\n#. ").Replace("\n", "\n#. ").Replace("\r", string.Empty); // The repalce is needed, otherwise PoEditor is not going to load correctly the jp text and the Repack is gonna crash. } else { entry.ExtractedComments = "JAPANESE LINE NOT FOUND"; } } po.Add(entry); } string NewPOAddress = Path.Combine(DestinationDir, Path.GetFileNameWithoutExtension(DestinationDir) + ".po"); ConvertFormat.To <BinaryFormat>(po).Stream.WriteTo(NewPOAddress); }
/* I'M LEAVING THE XML STUFF FOR COMPATIBILITY WITH OLDER VERSIONS OF DRAT */ public static void ConvertXmlToPo(string SingleXML) { List <string> XMLOriginalSentences = null, XMLTranslatedSentences = null, XMLSpeakers = null, XMLComments = null, JAPText = null; using (FileStream TRANSLATEDXML = new FileStream(SingleXML, FileMode.Open, FileAccess.Read)) using (StreamReader XMLStreamReader = new StreamReader(TRANSLATEDXML, Encoding.Unicode)) { string XmlText = XMLStreamReader.ReadToEnd(); XMLOriginalSentences = CommonTextStuff.ExtractTextFromXML(XmlText, "<ORIGINAL N°", "</ORIGINAL N°", SingleXML); XMLTranslatedSentences = CommonTextStuff.ExtractTextFromXML(XmlText, "<TRANSLATED N°", "</TRANSLATED N°", SingleXML); XMLSpeakers = CommonTextStuff.ExtractTextFromXML(XmlText, "<SPEAKER N°", "</SPEAKER N°", SingleXML); XMLComments = CommonTextStuff.ExtractTextFromXML(XmlText, "<COMMENT N°", "</COMMENT N°", SingleXML); } // If the user has checked "Add japanase texts", then extract the japanese text. if (DRAT.aDDJAPANESETEXTToolStripMenuItem.Checked == true) { JAPText = ReadJAPText(SingleXML); } //Read the language used by the user' OS, this way the editor can spellcheck the translation. System.Globalization.CultureInfo currentCulture = System.Threading.Thread.CurrentThread.CurrentCulture; Po po = new Po { Header = new PoHeader(DRAT.tabControl1.SelectedTab.Text, "your_email", currentCulture.Name) }; try { for (int i = 0; i < XMLOriginalSentences.Count; i++) { PoEntry entry = new PoEntry(); // "Speaker" if (DRAT.hIDESPEAKERSToolStripMenuItem.Checked == false && XMLSpeakers.Count > 0 && XMLSpeakers[i] != null && XMLSpeakers[i] != "") { entry.Context = $"{i + 1:D4} | {XMLSpeakers[i]}"; } else { entry.Context = $"{i + 1:D4}"; } // "Original" if (XMLOriginalSentences.Count > 0 && XMLOriginalSentences[i] != null && XMLOriginalSentences[i] != "") { entry.Original = XMLOriginalSentences[i]; } else { entry.Original = "[EMPTY_LINE]"; entry.Translated = "[EMPTY_LINE]"; } // "Translated" if (XMLTranslatedSentences.Count > 0 && XMLTranslatedSentences[i] != null && XMLTranslatedSentences[i] != "") { if (XMLOriginalSentences[i].Length == 1 || XMLOriginalSentences[i] == " \n" || XMLOriginalSentences[i] == "\n" || XMLOriginalSentences[i] == "..." || XMLOriginalSentences[i] == "…" || XMLOriginalSentences[i] == "...\n" || XMLOriginalSentences[i] == "…\n" || XMLOriginalSentences[i] == "\"...\"" || XMLOriginalSentences[i] == "\"…\"" || XMLOriginalSentences[i] == "\"...\n\"" || XMLOriginalSentences[i] == "\"…\n\"") { entry.Translated = XMLOriginalSentences[i]; } else if (XMLTranslatedSentences[i] != XMLOriginalSentences[i]) { entry.Translated = XMLTranslatedSentences[i]; } } // "Comments" if (XMLComments.Count > 0 && XMLComments[i] != null && XMLComments[i] != "") { entry.TranslatorComment = XMLComments[i].Replace("\n", "\n# "); //The repalce is needed, otherwise PoEditor is not going to load correctly the jp text and the Repack is gonna crash.; } // "Japanese" if (DRAT.aDDJAPANESETEXTToolStripMenuItem.Checked == true && JAPText != null && JAPText.Count > 0 && i < JAPText.Count) { if (JAPText[i] != "" && JAPText[i] != null) { entry.ExtractedComments = JAPText[i].Replace("\n", "\n#. "); //The repalce is needed, otherwise PoEditor is not going to load correctly the jp text and the Repack is gonna crash. } else { entry.ExtractedComments = "[EMPTY_LINE]"; } } else { entry.ExtractedComments = "JAPANESE LINE NOT FOUND"; } po.Add(entry); } string NewPoAddress = Path.Combine(Path.GetDirectoryName(SingleXML), Path.GetFileNameWithoutExtension(SingleXML) + ".po"); ConvertFormat.To <BinaryFormat>(po).Stream.WriteTo(NewPoAddress); } catch { MessageBox.Show("\"" + SingleXML + "\"'s structure appears to be corrupted. Please check the code tags' integrity.\n\nYou can use an older version of DRAT to extract again the XML from the original English or Japanese file and compare it with yours.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public void ConvertToWithType() { Assert.AreEqual(ConvertFormat.To(typeof(int), "3"), 3); Assert.AreEqual(ConvertFormat.To(typeof(string), 3), "3"); }
public void ConvertToGeneric() { Assert.AreEqual(ConvertFormat.To <int>("3"), 3); }
public void ConvertEmptyPo() { var po = new Po(); CompareText(ConvertFormat.To <BinaryFormat>(po), string.Empty); }
public void ConvertToDerivedWithBaseConverterThrows() { Assert.Throws <InvalidOperationException>( () => ConvertFormat.To <Derived>(5)); }