load in resx file info for all known languages

This commit is contained in:
Tim Young 2017-10-31 08:56:57 -05:00
parent 93f0ee18d7
commit 737ee16718

View File

@ -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<TranslationResxFile> TranslationResxFiles = new List<TranslationResxFile>();
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<TranslationItem> Items = new List<TranslationItem>();
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
}