From 737ee16718019b0256296a465fca8c82fc3362f6 Mon Sep 17 00:00:00 2001 From: Tim Young Date: Tue, 31 Oct 2017 08:56:57 -0500 Subject: [PATCH] load in resx file info for all known languages --- EduNetworkBuilder/TranslationWindow.cs | 83 ++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/EduNetworkBuilder/TranslationWindow.cs b/EduNetworkBuilder/TranslationWindow.cs index d39e673..a0bdd96 100644 --- a/EduNetworkBuilder/TranslationWindow.cs +++ b/EduNetworkBuilder/TranslationWindow.cs @@ -8,14 +8,22 @@ using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; +using System.Text.RegularExpressions; +using System.Resources; +using System.Collections; +using System.ComponentModel.Design; + namespace EduNetworkBuilder { + + public partial class TranslationWindow : Form { Network myNet = null; string[,] LanguageChoices = { { NB.Translate("NB_NBEn"), "en" }, { NB.Translate("NB_NBFr"), "fr" } }; string FormChosenDir = ""; + List TranslationResxFiles = new List(); public TranslationWindow(Network ToEdit) { @@ -247,6 +255,7 @@ namespace EduNetworkBuilder if (toCheck == targetfile) { FormChosenDir = Path.GetDirectoryName(Choice); + LoadResxFiles(); panelTranslateFormItems.Enabled = true; lblFormDir.Text = "Dir: " + FormChosenDir; lblFormDir.AutoSize = true; @@ -270,6 +279,19 @@ namespace EduNetworkBuilder return false; } + void LoadResxFiles() + { + if (!Directory.Exists(FormChosenDir)) return; + var resxFiles = Directory.EnumerateFiles(FormChosenDir, "*.resx", SearchOption.AllDirectories); + TranslationResxFiles.Clear(); + + foreach (string currentFile in resxFiles) + { + TranslationResxFile TRF = new TranslationResxFile(currentFile); + TranslationResxFiles.Add(TRF); + } + } + private void btnChooseDir_Click(object sender, EventArgs e) { string startingDir = FormChosenDir; @@ -288,4 +310,65 @@ namespace EduNetworkBuilder } } } + + #region TranslationClasses + public class TranslationItem + { + public string Key = ""; + public string Value = ""; + public string Comment = ""; + public bool translated = false; + } + + public class TranslationResxFile + { + public string filename; + List Items = new List(); + + public TranslationResxFile(string File) + { + filename = File; + Load(); + } + + void Load() + { + Items.Clear(); + ResXResourceReader InResx = new ResXResourceReader(filename); + InResx.UseResXDataNodes = true; + System.Reflection.Assembly currentAssembly = System.Reflection.Assembly.GetExecutingAssembly(); + + foreach (DictionaryEntry d in InResx) + { + ResXDataNode rdn = (ResXDataNode)d.Value; + TranslationItem TI = new TranslationItem(); + TI.Comment = rdn.Comment; + TI.Key = rdn.Name; + TI.Value = (string)rdn.GetValue(new System.Reflection.AssemblyName[] { currentAssembly.GetName() }); + Items.Add(TI); + } + InResx.Close(); + } + + public void Save() + { + string tmpfile = Path.Combine(Path.GetDirectoryName(filename), Path.GetFileNameWithoutExtension(filename) + "-tmp" + Path.GetExtension(filename)); + ResXResourceWriter WriteResx = new ResXResourceWriter(tmpfile); + + foreach (TranslationItem d in Items) + { + ResXDataNode rdn = new ResXDataNode(d.Key, d.Value); + rdn.Comment = d.Comment; + WriteResx.AddResource(rdn); + //Console.WriteLine("Adding " + toWrite.Name); + } + WriteResx.Generate(); + WriteResx.Close(); + //If we have an error, we should have exited and gone to "catch." + //Proceed to put the new file + File.Delete(filename); + File.Move(tmpfile, filename); + } + } + #endregion }