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 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +