public IFile Merge(IFile main, IFile addition) { if (main == null || main.GetType() != addition.GetType() || main.GetType() != typeof(CustomModTranslationFile)) { return(null); } var mainFile = main as CustomModTranslationFile; var additionFile = addition as CustomModTranslationFile; var result = new CustomModTranslationFile(); result.Translations = new Dictionary <string, BaseEntry>(); foreach (var pair in additionFile.Translations) { if (mainFile.Translations.ContainsKey(pair.Key)) { result.Translations.Add(pair.Key, Merge(mainFile.Translations[pair.Key], pair.Value)); } else { result.Translations.Add(pair.Key, pair.Value); } } return(result); }
public void Export(IPackage package, IExportConfig config) { if (package?.Mod == null) { return; } var translations = Utils.GetModByName(package.ModName).Field("translations") as Dictionary <string, ModTranslation>; if (translations == null) { return; } var file = new CustomModTranslationFile { Translations = new Dictionary <string, BaseEntry>() }; foreach (var translation in translations) { file.Translations.Add(translation.Key, new BaseEntry { Origin = translation.Value.DefaultOrEmpty(), Translation = config.WithTranslation ? translation.Value?.GetTranslation(package.Language) : "" }); } package.AddFile(file); }
public void UpdateInternal(CustomModTranslationFile oldFile, CustomModTranslationFile newFile, IUpdateLogger logger) { var oldEntries = oldFile.Translations; var newEntries = newFile.Translations; foreach (var newEntryKey in newEntries.Keys) { if (oldEntries.Keys.Contains(newEntryKey)) { var o = oldEntries[newEntryKey]; var n = newEntries[newEntryKey]; if (o.Origin != n.Origin) { logger.Change($"{newEntryKey}\r\n[Old: \"{o.Origin}\"]\r\n => \r\n[New: \"{n.Origin}\"]\r\n"); o.Origin = n.Origin; o.Translation = n.Translation; } } else { logger.Add($"[{newEntryKey}]"); var entry = newEntries[newEntryKey]; oldEntries.Add(newEntryKey, entry); } } var removed = oldEntries.Keys.Where(k => !newEntries.ContainsKey(k)); foreach (var r in removed) { logger.Remove($"[{r}]"); } }
internal CustomModTranslationFile MergeInternal(CustomModTranslationFile main, CustomModTranslationFile addition) { var result = new CustomModTranslationFile { Translations = new Dictionary <string, BaseEntry>() }; foreach (var t in main.Translations) { result.Translations.Add(t.Key, t.Value.Clone() as BaseEntry); } foreach (var pair in addition.Translations) { if (result.Translations.ContainsKey(pair.Key)) { result.Translations[pair.Key] = Merge(main.Translations[pair.Key], pair.Value); } else { result.Translations.Add(pair.Key, pair.Value.Clone() as BaseEntry); } } return(result); }
public void Merge_Correct() { var importer = new CustomModTranslationImporter(); var main = new CustomModTranslationFile() { Translations = new Dictionary <string, BaseEntry>() { { "Key1", new BaseEntry() { Origin = "Origin1", Translation = "Translation1" } }, { "Key2", new BaseEntry() { Origin = "Origin2", Translation = "Translation2" } }, { "Key3", new BaseEntry() { Origin = "Origin3", Translation = "Translation3" } }, } }; var addition = new CustomModTranslationFile() { Translations = new Dictionary <string, BaseEntry>() { { "Key1", new BaseEntry() { Origin = "Origin1", Translation = "AnotherTranslation1" } }, { "Key4", new BaseEntry() { Origin = "Origin4", Translation = "Translation4" } }, } }; var result = importer.MergeInternal(main, addition); result.Translations.Count.Should().Be(4); result.Translations["Key1"].Translation.Should().Be("Translation1"); result.Translations["Key4"].Origin.Should().Be("Origin4"); result.Translations["Key4"].Translation.Should().Be("Translation4"); }
private void ImportInternal(CustomModTranslationFile file, IMod mod, CultureInfo culture) { var entryDict = file.Translations; var translations = Utils.GetModByName(mod.Name).ValueOf <IDictionary <string, ModTranslation> >("translations"); if (translations == null) { return; } foreach (var pair in entryDict) { if (!translations.ContainsKey(pair.Key)) { continue; } var translation = translations[pair.Key]; translation?.Import(pair.Value, culture); translations[pair.Key] = translation; } }
public void Update_Correct() { var service = new CustomModTranslationUpdater(); var logger = new UpdateLogger(); var oldFile = new CustomModTranslationFile() { Translations = new Dictionary <string, BaseEntry>() { { "Key1", new BaseEntry() { Origin = "Origin1", Translation = "Translation1" } }, { "Key2", new BaseEntry() { Origin = "Origin2", Translation = "Translation2" } }, { "Key3", new BaseEntry() { Origin = "Origin3", Translation = "Translation3" } }, } }; var newFile = new CustomModTranslationFile() { Translations = new Dictionary <string, BaseEntry>() { { "Key1", new BaseEntry() { Origin = "AnotherOrigin1", Translation = "AnotherTranslation1" } }, { "Key4", new BaseEntry() { Origin = "Origin4", Translation = "Translation4" } }, { "Key5", new BaseEntry() { Origin = "Origin5", Translation = "Translation5" } }, } }; service.Update(oldFile, newFile, logger); logger.Added.Count.Should().Be(2); logger.Removed.Count.Should().Be(2); logger.Changed.Count.Should().Be(1); oldFile.Translations.Count.Should().Be(5); oldFile.Translations["Key1"].Origin.Should().Be("AnotherOrigin1"); oldFile.Translations["Key1"].Translation.Should().Be("AnotherTranslation1"); oldFile.Translations["Key2"].Origin.Should().Be("Origin2"); oldFile.Translations["Key2"].Translation.Should().Be("Translation2"); oldFile.Translations["Key3"].Origin.Should().Be("Origin3"); oldFile.Translations["Key3"].Translation.Should().Be("Translation3"); oldFile.Translations["Key4"].Origin.Should().Be("Origin4"); oldFile.Translations["Key4"].Translation.Should().Be("Translation4"); oldFile.Translations["Key5"].Origin.Should().Be("Origin5"); oldFile.Translations["Key5"].Translation.Should().Be("Translation5"); }