From 85693cd130d3ca66d44352cc4764203d933f0a00 Mon Sep 17 00:00:00 2001 From: Guy Perfect Date: Fri, 31 Jul 2020 17:27:44 -0500 Subject: [PATCH] Localizer: Adding associative values to controls --- locale/en_US.txt | 9 ++++++--- src/desktop/Main.java | 7 ++++--- src/desktop/util/Localizer.java | 31 ++++++++++++++++++++++++++----- 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/locale/en_US.txt b/locale/en_US.txt index 33db39c..85c6689 100644 --- a/locale/en_US.txt +++ b/locale/en_US.txt @@ -1,6 +1,9 @@ -app { +# Main window +app { title { - empty PVB Emulator - loaded {app.filename} - {app.title.empty} + default PVB Emulator + mixed {ctrl.number} {ctrl.filename} - {app.title.default} + number {ctrl.number} {app.title.default} + rom {ctrl.filename} - {app.title.default} } } diff --git a/src/desktop/Main.java b/src/desktop/Main.java index a84902f..1ea43b8 100644 --- a/src/desktop/Main.java +++ b/src/desktop/Main.java @@ -9,10 +9,11 @@ public class Main { public static void main(String[] args) { var loc = new Localizer(); loc.set(Util.textRead("locale/en_US.txt")); - loc.put("app.filename", "wario.vb"); var window = new JFrame(); - loc.add(window, "app.title.loaded"); - window.setVisible(true); + loc.add(window, "app.title.mixed"); + loc.put(window, "ctrl.filename", "wario.vb"); + loc.put(window, "ctrl.number", "1"); + System.out.println(window.getTitle()); } } diff --git a/src/desktop/util/Localizer.java b/src/desktop/util/Localizer.java index 4903944..bbdf995 100644 --- a/src/desktop/util/Localizer.java +++ b/src/desktop/util/Localizer.java @@ -12,6 +12,7 @@ public class Localizer { // Instance fields private HashMap controls; // Control mapping private HashMap messages; // Message dictionary + private HashMap> tags; // Control overrides @@ -32,6 +33,7 @@ public class Localizer { public Localizer() { controls = new HashMap(); messages = new HashMap(); + tags = new HashMap>(); } @@ -70,6 +72,7 @@ public class Localizer { // Add the control to the collection controls.put(control, key); + tags .put(control, new HashMap()); update(); return true; } @@ -79,6 +82,20 @@ public class Localizer { controls.clear(); } + // Configure a control tag + public String put(Object control, String key, String value) { + if (controls.get(control) == null || key == null) + return null; + var tags = this.tags.get(control); + key = key.toLowerCase(); + String ret = value == null ? + tags.remove(key) : + tags.put(key, value) + ; + update(); + return ret; + } + // Configure a dictionary entry public String put(String key, String value) { String ret = value == null ? @@ -91,6 +108,7 @@ public class Localizer { // Remove a control from the collection public boolean remove(Object control) { + tags.remove(control); return controls.remove(control) != null; } @@ -301,12 +319,12 @@ public class Localizer { /////////////////////////////////////////////////////////////////////////// // Process substitutions and escapes on a message for a given key - private String evaluate(String key) { + private String evaluate(Object control, String key) { String ret = messages.get(key.toLowerCase()); // The topmost key does not exist in the dictionary if (ret == null) - return null; + return key; // Perform all substitutions outer: for (;;) { @@ -333,7 +351,10 @@ public class Localizer { // Determine the substitution key = new String(chars, start, x - start); - String value = messages.get(key.toLowerCase()); + String lkey = key.toLowerCase(); + String value = tags.get(control).get(lkey); + if (value == null) + value = messages.get(lkey); if (value == null) value = "\\{" + key + "\\}"; @@ -374,14 +395,14 @@ public class Localizer { // One string if (key instanceof String) - values = new String[] { evaluate((String) key) }; + values = new String[] { evaluate(control, (String) key) }; // Multiple strings else { String[] keys = (String[]) key; values = new String[keys.length]; for (int x = 0; x < keys.length; x++) - values[x] = evaluate(keys[x]); + values[x] = evaluate(control, keys[x]); } // Update the control's text