TranslationPlugin.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using TMPro;
  8. using UnityEngine;
  9. using UnityEngine.SceneManagement;
  10. namespace BepInEx.Internal
  11. {
  12. public class TranslationPlugin : ITranslationPlugin, IUnityPlugin
  13. {
  14. Dictionary<string, string> translations = new Dictionary<string, string>();
  15. List<string> untranslated = new List<string>();
  16. public TranslationPlugin()
  17. {
  18. string[] translation = File.ReadAllLines(Utility.CombinePaths(Utility.ExecutingDirectory, "translation", "translation.txt"));
  19. for (int i = 0; i < translation.Length; i++)
  20. {
  21. string line = translation[i];
  22. if (!line.Contains('='))
  23. continue;
  24. string[] split = line.Split('=');
  25. translations[split[0]] = split[1];
  26. }
  27. }
  28. public void OnStart()
  29. {
  30. }
  31. public void OnLevelFinishedLoading(Scene scene, LoadSceneMode mode)
  32. {
  33. Translate();
  34. }
  35. public void OnFixedUpdate()
  36. {
  37. }
  38. public void OnLateUpdate()
  39. {
  40. }
  41. public void OnUpdate()
  42. {
  43. if (UnityEngine.Event.current.Equals(Event.KeyboardEvent("f9")))
  44. {
  45. Translate();
  46. }
  47. else if (UnityEngine.Event.current.Equals(Event.KeyboardEvent("f10")))
  48. {
  49. Dump();
  50. Console.WriteLine($"Text dumped to \"{Path.GetFullPath("dumped-tl.txt")}\"");
  51. }
  52. }
  53. void Translate()
  54. {
  55. foreach (TextMeshProUGUI gameObject in GameObject.FindObjectsOfType<TextMeshProUGUI>())
  56. {
  57. //gameObject.text = "Harsh is shit";
  58. if (translations.ContainsKey(gameObject.text))
  59. gameObject.text = translations[gameObject.text];
  60. else
  61. if (!untranslated.Contains(gameObject.text))
  62. untranslated.Add(gameObject.text);
  63. }
  64. }
  65. void Dump()
  66. {
  67. string output = "";
  68. foreach (var kv in translations)
  69. output += $"{kv.Key.Trim()}={kv.Value.Trim()}\r\n";
  70. foreach (var text in untranslated)
  71. if (!text.IsNullOrWhiteSpace()
  72. && !text.Contains("Reset")
  73. && !Regex.Replace(text, @"[\d-]", string.Empty).IsNullOrWhiteSpace()
  74. && !translations.ContainsValue(text.Trim()))
  75. output += $"{text.Trim()}=\r\n";
  76. File.WriteAllText("dumped-tl.txt", output);
  77. }
  78. public bool TryTranslate(string input, out string output)
  79. {
  80. if (translations.ContainsKey(input))
  81. {
  82. output = translations[input];
  83. return true;
  84. }
  85. output = null;
  86. if (!untranslated.Contains(input))
  87. untranslated.Add(input);
  88. return false;
  89. }
  90. }
  91. }