fix: InputField占位符实时翻译 + 速限/数量/台标签翻译 - RefreshAll新增InputField遍历,用户输入内容不受影响
This commit is contained in:
parent
de46915c70
commit
e40683557f
@ -30,6 +30,13 @@ public class LocalizationManager : MonoBehaviour
|
||||
public bool fontApplied;
|
||||
}
|
||||
|
||||
private class InputFieldState
|
||||
{
|
||||
public string source;
|
||||
public string localized;
|
||||
public bool fontApplied;
|
||||
}
|
||||
|
||||
// Pre-built lookup dictionaries for O(1) translation
|
||||
private Dictionary<string, string> reverseTable;
|
||||
private Dictionary<string, string[]> normalizedTable;
|
||||
@ -41,6 +48,7 @@ public class LocalizationManager : MonoBehaviour
|
||||
private static LocalizationManager instance;
|
||||
private readonly Dictionary<int, TextState> textStates = new Dictionary<int, TextState>();
|
||||
private readonly Dictionary<int, DropdownState> dropdownStates = new Dictionary<int, DropdownState>();
|
||||
private readonly Dictionary<int, InputFieldState> inputFieldStates = new Dictionary<int, InputFieldState>();
|
||||
|
||||
private readonly Dictionary<string, string[]> table = new Dictionary<string, string[]>
|
||||
{
|
||||
@ -507,6 +515,15 @@ public class LocalizationManager : MonoBehaviour
|
||||
{ "触发物体", new[] { "触发物体", "Trigger Object", "Activer l'objet", "Активировать объект" } },
|
||||
{ "抓取物体", new[] { "抓取物体", "Grab Object", "Saisir l'objet", "Захватить объект" } },
|
||||
{ "传送", new[] { "传送", "Teleport", "Téléportation", "Телепорт" } },
|
||||
|
||||
// Equipment item labels (Image_equip1.prefab, Canvas.prefab)
|
||||
{ "速限:", new[] { "速限:", "Speed Limit:", "Limite de vitesse :", "Ограничение скорости:" } },
|
||||
{ "数量:", new[] { "数量:", "Quantity:", "Quantité :", "Количество:" } },
|
||||
{ "台", new[] { "台", "units", "unités", "ед." } },
|
||||
{ "速限: km/h", new[] { "速限: km/h", "Speed Limit: km/h", "Limite de vitesse : km/h", "Ограничение скорости: км/ч" } },
|
||||
{ "速限: 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", "Количество: ед." } },
|
||||
};
|
||||
|
||||
[SerializeField] private int language;
|
||||
@ -744,6 +761,9 @@ public class LocalizationManager : MonoBehaviour
|
||||
|
||||
Dropdown[] dropdowns = FindObjectsOfType<Dropdown>();
|
||||
for (int i = 0; i < dropdowns.Length; i++) Localize(dropdowns[i]);
|
||||
|
||||
InputField[] inputs = FindObjectsOfType<InputField>();
|
||||
for (int i = 0; i < inputs.Length; i++) Localize(inputs[i]);
|
||||
}
|
||||
|
||||
private string Translate(string source)
|
||||
@ -1080,4 +1100,41 @@ public class LocalizationManager : MonoBehaviour
|
||||
|
||||
dropdown.RefreshShownValue();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Localize the placeholder text of an InputField. User-entered text is never touched.
|
||||
/// </summary>
|
||||
private void Localize(InputField input)
|
||||
{
|
||||
if (input == null || !input.gameObject.scene.IsValid()) return;
|
||||
if (input.placeholder == null) return;
|
||||
|
||||
Text placeholder = input.placeholder as Text;
|
||||
if (placeholder == null) return;
|
||||
|
||||
int id = input.GetInstanceID();
|
||||
InputFieldState state;
|
||||
if (!inputFieldStates.TryGetValue(id, out state))
|
||||
{
|
||||
state = new InputFieldState { source = placeholder.text, localized = placeholder.text };
|
||||
inputFieldStates.Add(id, state);
|
||||
}
|
||||
else if (placeholder.text != state.localized)
|
||||
{
|
||||
state.source = GetSourceText(placeholder.text);
|
||||
}
|
||||
|
||||
// Apply font once
|
||||
if (font != null && !state.fontApplied)
|
||||
{
|
||||
Color savedColor = placeholder.color;
|
||||
placeholder.font = font;
|
||||
placeholder.color = savedColor;
|
||||
placeholder.verticalOverflow = VerticalWrapMode.Overflow;
|
||||
state.fontApplied = true;
|
||||
}
|
||||
|
||||
state.localized = Translate(state.source);
|
||||
placeholder.text = state.localized;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user