2018-03-21 05:34:44 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
|
|
|
|
using PKHeX.Core;
|
|
|
|
|
|
|
|
|
|
namespace PKHeX.WinForms
|
|
|
|
|
{
|
|
|
|
|
public static class WinFormsTranslator
|
|
|
|
|
{
|
2020-12-22 07:37:07 +00:00
|
|
|
|
private static readonly Dictionary<string, TranslationContext> Context = new();
|
2018-03-21 05:34:44 +00:00
|
|
|
|
internal static void TranslateInterface(this Control form, string lang) => TranslateForm(form, GetContext(lang));
|
|
|
|
|
|
|
|
|
|
private static string GetTranslationFileNameInternal(string lang) => $"lang_{lang}";
|
|
|
|
|
private static string GetTranslationFileNameExternal(string lang) => $"lang_{lang}.txt";
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2021-04-12 01:09:54 +00:00
|
|
|
|
public static IReadOnlyDictionary<string, string> GetDictionary(string lang) => GetContext(lang).Lookup;
|
|
|
|
|
|
2018-03-21 05:34:44 +00:00
|
|
|
|
private static TranslationContext GetContext(string lang)
|
|
|
|
|
{
|
|
|
|
|
if (Context.TryGetValue(lang, out var context))
|
|
|
|
|
return context;
|
|
|
|
|
|
|
|
|
|
var lines = GetTranslationFile(lang);
|
|
|
|
|
Context.Add(lang, context = new TranslationContext(lines));
|
|
|
|
|
return context;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void TranslateForm(Control form, TranslationContext context)
|
|
|
|
|
{
|
|
|
|
|
form.SuspendLayout();
|
2022-02-27 05:45:31 +00:00
|
|
|
|
|
2018-03-21 05:34:44 +00:00
|
|
|
|
// Translate Title
|
2022-02-27 05:45:31 +00:00
|
|
|
|
var formName = form.Name;
|
|
|
|
|
form.Text = context.GetTranslatedText(formName, form.Text);
|
|
|
|
|
|
|
|
|
|
// Translate Controls
|
2018-03-21 05:34:44 +00:00
|
|
|
|
var translatable = GetTranslatableControls(form);
|
|
|
|
|
foreach (var c in translatable)
|
2022-02-27 05:45:31 +00:00
|
|
|
|
TranslateControl(c, context, formName);
|
|
|
|
|
|
|
|
|
|
form.ResumeLayout();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void TranslateControl(object c, TranslationContext context, string formname)
|
|
|
|
|
{
|
|
|
|
|
if (c is Control r)
|
2018-03-21 05:34:44 +00:00
|
|
|
|
{
|
2022-02-27 05:45:31 +00:00
|
|
|
|
var current = r.Text;
|
|
|
|
|
var updated = context.GetTranslatedText($"{formname}.{r.Name}", current);
|
|
|
|
|
if (!ReferenceEquals(current, updated))
|
|
|
|
|
r.Text = updated;
|
|
|
|
|
}
|
|
|
|
|
else if (c is ToolStripItem t)
|
|
|
|
|
{
|
|
|
|
|
var current = t.Text;
|
|
|
|
|
var updated = context.GetTranslatedText($"{formname}.{t.Name}", current);
|
|
|
|
|
if (!ReferenceEquals(current, updated))
|
|
|
|
|
t.Text = updated;
|
2018-03-21 05:34:44 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static IEnumerable<string> GetTranslationFile(string lang)
|
|
|
|
|
{
|
|
|
|
|
var file = GetTranslationFileNameInternal(lang);
|
|
|
|
|
// Check to see if a the translation file exists in the same folder as the executable
|
|
|
|
|
string externalLangPath = GetTranslationFileNameExternal(file);
|
|
|
|
|
if (File.Exists(externalLangPath))
|
|
|
|
|
{
|
|
|
|
|
try { return File.ReadAllLines(externalLangPath); }
|
|
|
|
|
catch { /* In use? Just return the internal resource. */ }
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-03 23:53:48 +00:00
|
|
|
|
if (Util.IsStringListCached(file, out var result))
|
|
|
|
|
return result;
|
2020-10-18 18:02:39 +00:00
|
|
|
|
var txt = (string?)Properties.Resources.ResourceManager.GetObject(file);
|
2020-01-03 23:53:48 +00:00
|
|
|
|
return Util.LoadStringList(file, txt);
|
2018-03-21 05:34:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static IEnumerable<object> GetTranslatableControls(Control f)
|
|
|
|
|
{
|
|
|
|
|
foreach (var z in f.GetChildrenOfType<Control>())
|
|
|
|
|
{
|
|
|
|
|
switch (z)
|
|
|
|
|
{
|
|
|
|
|
case ToolStrip menu:
|
|
|
|
|
foreach (var obj in GetToolStripMenuItems(menu))
|
|
|
|
|
yield return obj;
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
if (string.IsNullOrWhiteSpace(z.Name))
|
|
|
|
|
break;
|
|
|
|
|
|
2020-06-22 00:46:06 +00:00
|
|
|
|
if (z.ContextMenuStrip != null) // control has attached MenuStrip
|
2018-07-27 02:34:27 +00:00
|
|
|
|
{
|
2018-03-21 05:34:44 +00:00
|
|
|
|
foreach (var obj in GetToolStripMenuItems(z.ContextMenuStrip))
|
|
|
|
|
yield return obj;
|
2018-07-27 02:34:27 +00:00
|
|
|
|
}
|
2018-03-21 05:34:44 +00:00
|
|
|
|
|
2021-04-12 01:09:54 +00:00
|
|
|
|
if (z is ListControl or TextBoxBase or LinkLabel or NumericUpDown or ContainerControl)
|
2018-03-21 05:34:44 +00:00
|
|
|
|
break; // undesirable to modify, ignore
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(z.Text))
|
|
|
|
|
yield return z;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2018-03-21 05:34:44 +00:00
|
|
|
|
private static IEnumerable<T> GetChildrenOfType<T>(this Control control) where T : class
|
|
|
|
|
{
|
2020-10-18 18:02:39 +00:00
|
|
|
|
foreach (var child in control.Controls.OfType<Control>())
|
2018-03-21 05:34:44 +00:00
|
|
|
|
{
|
2019-05-13 22:53:27 +00:00
|
|
|
|
if (child is T childOfT)
|
2018-03-21 05:34:44 +00:00
|
|
|
|
yield return childOfT;
|
|
|
|
|
|
|
|
|
|
if (!child.HasChildren) continue;
|
|
|
|
|
foreach (var descendant in GetChildrenOfType<T>(child))
|
|
|
|
|
yield return descendant;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2018-03-21 05:34:44 +00:00
|
|
|
|
private static IEnumerable<object> GetToolStripMenuItems(ToolStrip menu)
|
|
|
|
|
{
|
|
|
|
|
foreach (var i in menu.Items.OfType<ToolStripMenuItem>())
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(i.Text))
|
|
|
|
|
yield return i;
|
|
|
|
|
foreach (var sub in GetToolsStripDropDownItems(i).Where(z => !string.IsNullOrWhiteSpace(z.Text)))
|
|
|
|
|
yield return sub;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2018-03-21 05:34:44 +00:00
|
|
|
|
private static IEnumerable<ToolStripMenuItem> GetToolsStripDropDownItems(ToolStripDropDownItem item)
|
|
|
|
|
{
|
|
|
|
|
foreach (var dropDownItem in item.DropDownItems.OfType<ToolStripMenuItem>())
|
|
|
|
|
{
|
|
|
|
|
yield return dropDownItem;
|
2022-02-27 05:45:31 +00:00
|
|
|
|
if (!dropDownItem.HasDropDownItems)
|
|
|
|
|
continue;
|
2018-03-21 05:34:44 +00:00
|
|
|
|
foreach (ToolStripMenuItem subItem in GetToolsStripDropDownItems(dropDownItem))
|
|
|
|
|
yield return subItem;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-27 05:45:31 +00:00
|
|
|
|
#if DEBUG
|
2018-03-21 05:34:44 +00:00
|
|
|
|
public static void UpdateAll(string baseLanguage, IEnumerable<string> others)
|
|
|
|
|
{
|
2020-06-22 00:46:06 +00:00
|
|
|
|
var baseContext = GetContext(baseLanguage);
|
2018-03-21 05:34:44 +00:00
|
|
|
|
foreach (var lang in others)
|
|
|
|
|
{
|
|
|
|
|
var c = GetContext(lang);
|
2020-06-22 00:46:06 +00:00
|
|
|
|
c.UpdateFrom(baseContext);
|
2018-03-21 05:34:44 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-04 01:46:32 +00:00
|
|
|
|
public static void DumpAll(params string[] banlist)
|
2018-03-21 05:34:44 +00:00
|
|
|
|
{
|
|
|
|
|
var results = Context.Select(z => new {Lang = z.Key, Lines = z.Value.Write()});
|
|
|
|
|
foreach (var c in results)
|
2018-04-04 01:46:32 +00:00
|
|
|
|
{
|
|
|
|
|
var lang = c.Lang;
|
|
|
|
|
var fn = GetTranslationFileNameExternal(lang);
|
|
|
|
|
var lines = c.Lines;
|
|
|
|
|
var result = lines.Where(z => !banlist.Any(z.Contains));
|
|
|
|
|
File.WriteAllLines(fn, result);
|
|
|
|
|
}
|
2018-03-21 05:34:44 +00:00
|
|
|
|
}
|
2018-04-03 03:55:10 +00:00
|
|
|
|
|
2022-02-27 05:45:31 +00:00
|
|
|
|
public static void LoadAllForms(IEnumerable<System.Type> types, params string[] banlist)
|
2018-04-03 03:55:10 +00:00
|
|
|
|
{
|
2022-02-27 05:45:31 +00:00
|
|
|
|
types = types.Where(t => t.BaseType == typeof(Form) && !banlist.Contains(t.Name));
|
|
|
|
|
foreach (var t in types)
|
2018-04-03 03:55:10 +00:00
|
|
|
|
{
|
|
|
|
|
var constructors = t.GetConstructors();
|
|
|
|
|
if (constructors.Length == 0)
|
2022-02-27 05:45:31 +00:00
|
|
|
|
{ System.Diagnostics.Debug.WriteLine($"No constructors: {t.Name}"); continue; }
|
2018-05-13 01:11:47 +00:00
|
|
|
|
var argCount = constructors[0].GetParameters().Length;
|
2018-04-03 03:55:10 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2020-10-18 18:02:39 +00:00
|
|
|
|
var _ = (Form?)System.Activator.CreateInstance(t, new object[argCount]);
|
2018-04-03 03:55:10 +00:00
|
|
|
|
}
|
2020-09-19 05:11:13 +00:00
|
|
|
|
// This is a debug utility method, will always be logging. Shouldn't ever fail.
|
2020-06-28 04:36:53 +00:00
|
|
|
|
catch
|
|
|
|
|
{
|
2022-02-27 05:45:31 +00:00
|
|
|
|
System.Diagnostics.Debug.Write($"Failed to create a new form {t}");
|
2020-06-28 04:36:53 +00:00
|
|
|
|
}
|
2018-04-03 03:55:10 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-04 01:46:32 +00:00
|
|
|
|
|
|
|
|
|
public static void SetRemovalMode(bool status = true)
|
|
|
|
|
{
|
|
|
|
|
foreach (TranslationContext c in Context.Values)
|
|
|
|
|
{
|
|
|
|
|
c.RemoveUsedKeys = status;
|
|
|
|
|
c.AddNew = !status;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void RemoveAll(string defaultLanguage, params string[] banlist)
|
|
|
|
|
{
|
|
|
|
|
var badKeys = Context[defaultLanguage];
|
|
|
|
|
var split = badKeys.Write().Select(z => z.Split(TranslationContext.Separator)[0])
|
|
|
|
|
.Where(l => !banlist.Any(l.StartsWith)).ToArray();
|
|
|
|
|
foreach (var c in Context)
|
|
|
|
|
{
|
|
|
|
|
var lang = c.Key;
|
|
|
|
|
var fn = GetTranslationFileNameExternal(lang);
|
|
|
|
|
var lines = File.ReadAllLines(fn);
|
|
|
|
|
var result = lines.Where(l => !split.Any(s => l.StartsWith(s + TranslationContext.Separator)));
|
|
|
|
|
File.WriteAllLines(fn, result);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-12 01:09:54 +00:00
|
|
|
|
|
|
|
|
|
public static void LoadSettings<T>(string defaultLanguage, bool add = true)
|
|
|
|
|
{
|
|
|
|
|
var context = (Dictionary<string, string>)Context[defaultLanguage].Lookup;
|
|
|
|
|
var props = typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
|
|
|
|
|
foreach (var prop in props)
|
|
|
|
|
{
|
|
|
|
|
var p = prop.PropertyType.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
|
|
|
|
|
foreach (var x in p)
|
|
|
|
|
{
|
|
|
|
|
var individual = (LocalizedDescriptionAttribute[])x.GetCustomAttributes(typeof(LocalizedDescriptionAttribute), false);
|
|
|
|
|
foreach (var v in individual)
|
|
|
|
|
{
|
|
|
|
|
var hasKey = context.ContainsKey(v.Key);
|
|
|
|
|
if (add)
|
|
|
|
|
{
|
|
|
|
|
if (!hasKey)
|
|
|
|
|
context.Add(v.Key, v.Fallback);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (hasKey)
|
|
|
|
|
context.Remove(v.Key);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-02-27 05:45:31 +00:00
|
|
|
|
#endif
|
2018-03-21 05:34:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-04 02:09:02 +00:00
|
|
|
|
public sealed class TranslationContext
|
2018-03-21 05:34:44 +00:00
|
|
|
|
{
|
2018-04-19 22:36:43 +00:00
|
|
|
|
public bool AddNew { private get; set; }
|
2018-04-04 01:46:32 +00:00
|
|
|
|
public bool RemoveUsedKeys { private get; set; }
|
|
|
|
|
public const char Separator = '=';
|
2020-12-22 07:37:07 +00:00
|
|
|
|
private readonly Dictionary<string, string> Translation = new();
|
2021-04-12 01:09:54 +00:00
|
|
|
|
public IReadOnlyDictionary<string, string> Lookup => Translation;
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2018-04-04 01:46:32 +00:00
|
|
|
|
public TranslationContext(IEnumerable<string> content, char separator = Separator)
|
2018-03-21 05:34:44 +00:00
|
|
|
|
{
|
|
|
|
|
var entries = content.Select(z => z.Split(separator)).Where(z => z.Length == 2);
|
2019-02-07 07:28:02 +00:00
|
|
|
|
foreach (var kvp in entries.Where(z => !Translation.ContainsKey(z[0])))
|
|
|
|
|
Translation.Add(kvp[0], kvp[1]);
|
2018-03-21 05:34:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-18 18:02:39 +00:00
|
|
|
|
public string? GetTranslatedText(string val, string? fallback)
|
2018-03-21 05:34:44 +00:00
|
|
|
|
{
|
|
|
|
|
if (RemoveUsedKeys)
|
|
|
|
|
Translation.Remove(val);
|
|
|
|
|
|
|
|
|
|
if (Translation.TryGetValue(val, out var translated))
|
|
|
|
|
return translated;
|
|
|
|
|
|
|
|
|
|
if (fallback != null && AddNew)
|
|
|
|
|
Translation.Add(val, fallback);
|
|
|
|
|
return fallback;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-04 01:46:32 +00:00
|
|
|
|
public IEnumerable<string> Write(char separator = Separator)
|
2018-03-21 05:34:44 +00:00
|
|
|
|
{
|
|
|
|
|
return Translation.Select(z => $"{z.Key}{separator}{z.Value}").OrderBy(z => z.Contains(".")).ThenBy(z => z);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UpdateFrom(TranslationContext other)
|
|
|
|
|
{
|
|
|
|
|
bool oldAdd = AddNew;
|
|
|
|
|
AddNew = true;
|
|
|
|
|
foreach (var kvp in other.Translation)
|
|
|
|
|
GetTranslatedText(kvp.Key, kvp.Value);
|
|
|
|
|
AddNew = oldAdd;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|