From 41369dd16dbf100c502b28c9de5567e34d08dfe2 Mon Sep 17 00:00:00 2001 From: ayuan9957 <107920784+ayuan9957@users.noreply.github.com> Date: Mon, 20 Jul 2026 09:57:29 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=BF=BB=E8=AF=91=E8=A1=A8=E5=A4=96?= =?UTF-8?q?=E9=83=A8=E5=8C=96=E4=B8=BAXML=E9=85=8D=E7=BD=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6=20-=20452=E6=9D=A1=E7=BF=BB=E8=AF=91=E4=BB=8EStreamin?= =?UTF-8?q?gAssets/Localization.xml=E5=8A=A0=E8=BD=BD,=20=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E7=BF=BB=E8=AF=91=E5=8F=AA=E9=9C=80=E7=BC=96=E8=BE=91?= =?UTF-8?q?XML=E6=97=A0=E9=9C=80=E6=94=B9=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Localization/LocalizationManager.cs | 64 ++- Assets/StreamingAssets/Localization.xml | 455 ++++++++++++++++++ 2 files changed, 517 insertions(+), 2 deletions(-) create mode 100644 Assets/StreamingAssets/Localization.xml diff --git a/Assets/Scripts/Localization/LocalizationManager.cs b/Assets/Scripts/Localization/LocalizationManager.cs index 9b2ef04..7416a3e 100644 --- a/Assets/Scripts/Localization/LocalizationManager.cs +++ b/Assets/Scripts/Localization/LocalizationManager.cs @@ -1,5 +1,7 @@ using System; using System.Collections.Generic; +using System.IO; +using System.Xml; using UnityEngine; using UnityEngine.UI; @@ -50,7 +52,13 @@ public class LocalizationManager : MonoBehaviour private readonly Dictionary dropdownStates = new Dictionary(); private readonly Dictionary inputFieldStates = new Dictionary(); - private readonly Dictionary table = new Dictionary + private Dictionary table; + + private const string XmlPath = "Localization.xml"; + + private static Dictionary BuildHardcodedTable() + { + return new Dictionary { { "装备抢修模拟训练系统", new[] { "装备抢修模拟训练系统", "Equipment Emergency Repair Simulation", "Simulation de réparation d'urgence", "Симулятор аварийного ремонта" } }, { "登陆界面", new[] { "登陆界面", "Login", "Connexion", "Вход" } }, @@ -524,7 +532,58 @@ public class LocalizationManager : MonoBehaviour { "速限: km/h", new[] { "速限: km/h", "Speed Limit: km/h", "Limite de vitesse : km/h", "Ограничение скорости: км/ч" } }, { "数量: 台", new[] { "数量: 台", "Quantity: units", "Quantité : unités", "Количество: ед." } }, { "数量: 台", new[] { "数量: 台", "Quantity: units", "Quantité : unités", "Количество: ед." } }, - }; + }; + } + + /// + /// Load translations from StreamingAssets/Localization.xml. + /// Falls back to hardcoded table if XML is missing or invalid. + /// + private void LoadTable() + { + table = BuildHardcodedTable(); + + string xmlPath = Path.Combine(Application.streamingAssetsPath, XmlPath); + if (!File.Exists(xmlPath)) + { + Debug.LogWarning($"Localization XML not found at {xmlPath}, using hardcoded table."); + return; + } + + try + { + var xmlTable = new Dictionary(); + using (XmlReader reader = XmlReader.Create(xmlPath)) + { + while (reader.Read()) + { + if (reader.NodeType == XmlNodeType.Element && reader.Name == "item") + { + string zh = reader.GetAttribute("zh"); + string en = reader.GetAttribute("en"); + string fr = reader.GetAttribute("fr"); + string ru = reader.GetAttribute("ru"); + if (string.IsNullOrEmpty(zh)) continue; + xmlTable[zh] = new[] { zh, en ?? zh, fr ?? zh, ru ?? zh }; + } + } + } + + if (xmlTable.Count > 0) + { + table = xmlTable; + Debug.Log($"Localization loaded from XML: {xmlTable.Count} entries."); + } + else + { + Debug.LogWarning("Localization XML is empty, using hardcoded table."); + } + } + catch (Exception e) + { + Debug.LogError($"Failed to load Localization XML: {e.Message}"); + } + } [SerializeField] private int language; private Font font; @@ -568,6 +627,7 @@ public class LocalizationManager : MonoBehaviour instance = this; language = Mathf.Clamp(PlayerPrefs.GetInt(PreferenceKey, 0), 0, 3); font = Resources.Load(FontResourcePath); + LoadTable(); BuildLookupTables(); } diff --git a/Assets/StreamingAssets/Localization.xml b/Assets/StreamingAssets/Localization.xml new file mode 100644 index 0000000..71cf2cf --- /dev/null +++ b/Assets/StreamingAssets/Localization.xml @@ -0,0 +1,455 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +