Localizer: Adding associative values to controls

This commit is contained in:
Guy Perfect 2020-07-31 17:27:44 -05:00
parent 5ee08a4d93
commit 85693cd130
3 changed files with 36 additions and 11 deletions

View File

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

View File

@ -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());
}
}

View File

@ -12,6 +12,7 @@ public class Localizer {
// Instance fields
private HashMap<Object, Object> controls; // Control mapping
private HashMap<String, String> messages; // Message dictionary
private HashMap<Object, HashMap<String, String>> tags; // Control overrides
@ -32,6 +33,7 @@ public class Localizer {
public Localizer() {
controls = new HashMap<Object, Object>();
messages = new HashMap<String, String>();
tags = new HashMap<Object, HashMap<String, String>>();
}
@ -70,6 +72,7 @@ public class Localizer {
// Add the control to the collection
controls.put(control, key);
tags .put(control, new HashMap<String, String>());
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