Simplify control finding for translation

Figured I'd rewrite this to remove the gotos
This commit is contained in:
Kaphotics
2016-06-30 22:36:54 -07:00
parent 537246d4b9
commit 67e7d3edaf

View File

@@ -237,64 +237,74 @@ internal static void TranslateInterface(Control form, string lang)
rawlist = rawlist.Select(i => i.Trim()).ToArray(); // Remove trailing spaces
}
string[] stringdata = new string[rawlist.Length];
int itemsToRename = 0;
List<string> stringdata = new List<string>();
int start = -1;
for (int i = 0; i < rawlist.Length; i++)
{
// Find our starting point
if (!rawlist[i].Contains("! " + form.Name)) continue;
// Allow renaming of the Window Title
string[] WindowName = rawlist[i].Split(new[] {" = "}, StringSplitOptions.None);
if (WindowName.Length > 1) form.Text = WindowName[1];
// Copy our Control Names and Text to a new array for later processing.
for (int j = i + 1; j < rawlist.Length; j++)
{
if (rawlist[j].Length == 0) continue; // Skip Over Empty Lines, errhandled
if (rawlist[j][0] == '-') continue; // Keep translating if line is a comment line
if (rawlist[j][0] == '!') // Stop if we have reached the end of translation
goto rename;
stringdata[itemsToRename] = rawlist[j]; // Add the entry to process later.
itemsToRename++;
}
start = i;
break;
}
return; // Not Found
// Now that we have our items to rename in: Control = Text format, let's execute the changes!
rename:
for (int i = 0; i < itemsToRename; i++)
{
string[] SplitString = stringdata[i].Split(new[] {" = "}, StringSplitOptions.None);
if (SplitString.Length < 2)
continue; // Error in Input, errhandled
string ctrl = SplitString[0]; // Control to change the text of...
string text = SplitString[1]; // Text to set Control.Text to...
Control[] controllist = form.Controls.Find(ctrl, true);
if (controllist.Length != 0) // If Control is found
{ controllist[0].Text = text; goto next; }
if (start < 0)
return;
// Rename Window Title
string[] WindowName = rawlist[start].Split(new[] {" = "}, StringSplitOptions.None);
if (WindowName.Length > 1) form.Text = WindowName[1];
// Check MenuStrips
foreach (MenuStrip menu in form.Controls.OfType<MenuStrip>())
{
// Menu Items aren't in the Form's Control array. Find within the menu's Control array.
ToolStripItem[] TSI = menu.Items.Find(ctrl, true);
if (TSI.Length <= 0) continue;
TSI[0].Text = text; goto next;
}
// Check ContextMenuStrips
foreach (ContextMenuStrip cs in FindContextMenuStrips(form.Controls.OfType<Control>()).Distinct())
{
ToolStripItem[] TSI = cs.Items.Find(ctrl, true);
if (TSI.Length <= 0) continue;
// Fetch controls to rename
for (int i = start + 1; i < rawlist.Length; i++)
{
if (rawlist[i].Length == 0) continue; // Skip Over Empty Lines, errhandled
if (rawlist[i][0] == '-') continue; // Keep translating if line is a comment line
if (rawlist[i][0] == '!') // Stop if we have reached the end of translation
break;
stringdata.Add(rawlist[i]); // Add the entry to process later.
}
TSI[0].Text = text; goto next;
}
if (stringdata.Count == 0)
return;
next:;
// Find control then change display Text.
foreach (string str in stringdata)
{
string[] SplitString = str.Split(new[] {" = "}, StringSplitOptions.None);
if (SplitString.Length < 2)
continue;
object c = FindControl(SplitString[0], form.Controls); // Find control within Form's controls
if (c == null) // Not found
continue;
string text = SplitString[1]; // Text to set Control.Text to...
if (c is Control)
(c as Control).Text = text;
else if (c is ToolStripItem)
(c as ToolStripItem).Text = text;
}
}
internal static List<ContextMenuStrip> FindContextMenuStrips(IEnumerable<Control> c)
private static object FindControl(string name, Control.ControlCollection c)
{
Control control = c.Find(name, true).FirstOrDefault();
if (control != null)
return control;
foreach (MenuStrip menu in c.OfType<MenuStrip>())
{
var item = menu.Items.Find(name, true).FirstOrDefault();
if (item != null)
return item;
}
foreach (ContextMenuStrip strip in FindContextMenuStrips(c.OfType<Control>()))
{
var item = strip.Items.Find(name, true).FirstOrDefault();
if (item != null)
return item;
}
return null;
}
private static List<ContextMenuStrip> FindContextMenuStrips(IEnumerable<Control> c)
{
List<ContextMenuStrip> cs = new List<ContextMenuStrip>();
foreach (Control control in c)