Localizer: Adding associative values to controls
This commit is contained in:
parent
5ee08a4d93
commit
85693cd130
|
@ -1,6 +1,9 @@
|
||||||
app {
|
# Main window
|
||||||
|
app {
|
||||||
title {
|
title {
|
||||||
empty PVB Emulator
|
default PVB Emulator
|
||||||
loaded {app.filename} - {app.title.empty}
|
mixed {ctrl.number} {ctrl.filename} - {app.title.default}
|
||||||
|
number {ctrl.number} {app.title.default}
|
||||||
|
rom {ctrl.filename} - {app.title.default}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,10 +9,11 @@ public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
var loc = new Localizer();
|
var loc = new Localizer();
|
||||||
loc.set(Util.textRead("locale/en_US.txt"));
|
loc.set(Util.textRead("locale/en_US.txt"));
|
||||||
loc.put("app.filename", "wario.vb");
|
|
||||||
var window = new JFrame();
|
var window = new JFrame();
|
||||||
loc.add(window, "app.title.loaded");
|
loc.add(window, "app.title.mixed");
|
||||||
window.setVisible(true);
|
loc.put(window, "ctrl.filename", "wario.vb");
|
||||||
|
loc.put(window, "ctrl.number", "1");
|
||||||
|
System.out.println(window.getTitle());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,7 @@ public class Localizer {
|
||||||
// Instance fields
|
// Instance fields
|
||||||
private HashMap<Object, Object> controls; // Control mapping
|
private HashMap<Object, Object> controls; // Control mapping
|
||||||
private HashMap<String, String> messages; // Message dictionary
|
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() {
|
public Localizer() {
|
||||||
controls = new HashMap<Object, Object>();
|
controls = new HashMap<Object, Object>();
|
||||||
messages = new HashMap<String, String>();
|
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
|
// Add the control to the collection
|
||||||
controls.put(control, key);
|
controls.put(control, key);
|
||||||
|
tags .put(control, new HashMap<String, String>());
|
||||||
update();
|
update();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -79,6 +82,20 @@ public class Localizer {
|
||||||
controls.clear();
|
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
|
// Configure a dictionary entry
|
||||||
public String put(String key, String value) {
|
public String put(String key, String value) {
|
||||||
String ret = value == null ?
|
String ret = value == null ?
|
||||||
|
@ -91,6 +108,7 @@ public class Localizer {
|
||||||
|
|
||||||
// Remove a control from the collection
|
// Remove a control from the collection
|
||||||
public boolean remove(Object control) {
|
public boolean remove(Object control) {
|
||||||
|
tags.remove(control);
|
||||||
return controls.remove(control) != null;
|
return controls.remove(control) != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -301,12 +319,12 @@ public class Localizer {
|
||||||
///////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
// Process substitutions and escapes on a message for a given key
|
// 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());
|
String ret = messages.get(key.toLowerCase());
|
||||||
|
|
||||||
// The topmost key does not exist in the dictionary
|
// The topmost key does not exist in the dictionary
|
||||||
if (ret == null)
|
if (ret == null)
|
||||||
return null;
|
return key;
|
||||||
|
|
||||||
// Perform all substitutions
|
// Perform all substitutions
|
||||||
outer: for (;;) {
|
outer: for (;;) {
|
||||||
|
@ -333,7 +351,10 @@ public class Localizer {
|
||||||
|
|
||||||
// Determine the substitution
|
// Determine the substitution
|
||||||
key = new String(chars, start, x - start);
|
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)
|
if (value == null)
|
||||||
value = "\\{" + key + "\\}";
|
value = "\\{" + key + "\\}";
|
||||||
|
|
||||||
|
@ -374,14 +395,14 @@ public class Localizer {
|
||||||
|
|
||||||
// One string
|
// One string
|
||||||
if (key instanceof String)
|
if (key instanceof String)
|
||||||
values = new String[] { evaluate((String) key) };
|
values = new String[] { evaluate(control, (String) key) };
|
||||||
|
|
||||||
// Multiple strings
|
// Multiple strings
|
||||||
else {
|
else {
|
||||||
String[] keys = (String[]) key;
|
String[] keys = (String[]) key;
|
||||||
values = new String[keys.length];
|
values = new String[keys.length];
|
||||||
for (int x = 0; x < keys.length; x++)
|
for (int x = 0; x < keys.length; x++)
|
||||||
values[x] = evaluate(keys[x]);
|
values[x] = evaluate(control, keys[x]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the control's text
|
// Update the control's text
|
||||||
|
|
Loading…
Reference in New Issue