114 lines
3.4 KiB
Java
114 lines
3.4 KiB
Java
|
package app;
|
||
|
|
||
|
// Java imports
|
||
|
import java.util.*;
|
||
|
|
||
|
// Project imports
|
||
|
import util.*;
|
||
|
|
||
|
// Top-level software state manager
|
||
|
public class App {
|
||
|
|
||
|
// Instance fields
|
||
|
private Localizer.Locale[] locales; // Language translations
|
||
|
private Localizer localizer; // UI localization manager
|
||
|
|
||
|
|
||
|
|
||
|
///////////////////////////////////////////////////////////////////////////
|
||
|
// Constructors //
|
||
|
///////////////////////////////////////////////////////////////////////////
|
||
|
|
||
|
// Default constructor
|
||
|
public App() {
|
||
|
|
||
|
// Instance fields
|
||
|
localizer = new Localizer();
|
||
|
|
||
|
// Additional processing
|
||
|
initLocales();
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
///////////////////////////////////////////////////////////////////////////
|
||
|
// Public Methods //
|
||
|
///////////////////////////////////////////////////////////////////////////
|
||
|
|
||
|
// Associate a control with the localizer
|
||
|
public boolean addControl(Object control, Object key) {
|
||
|
return localizer.add(control, key);
|
||
|
}
|
||
|
|
||
|
// Retrieve the currently active locale
|
||
|
public Localizer.Locale getLocale() {
|
||
|
return localizer.getLocale();
|
||
|
}
|
||
|
|
||
|
// Retrieve a list of registered locales
|
||
|
public Localizer.Locale[] listLocales() {
|
||
|
var ret = new Localizer.Locale[locales.length];
|
||
|
System.arraycopy(locales, 0, ret, 0, locales.length);
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
|
// Remove a control from the localizer
|
||
|
public boolean removeControl(Object control) {
|
||
|
return localizer.remove(control);
|
||
|
}
|
||
|
|
||
|
// Specify a new locale
|
||
|
public void setLocale(Localizer.Locale locale) {
|
||
|
localizer.setLocale(locale);
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
///////////////////////////////////////////////////////////////////////////
|
||
|
// Private Methods //
|
||
|
///////////////////////////////////////////////////////////////////////////
|
||
|
|
||
|
// Load and parse all locale translations
|
||
|
private void initLocales() {
|
||
|
|
||
|
// Process all locale files
|
||
|
var locales = new HashMap<String, Localizer.Locale>();
|
||
|
for (String file : Util.listFiles("locale")) {
|
||
|
|
||
|
// Process the file
|
||
|
try {
|
||
|
var locale = Localizer.parse(Util.textRead("locale/" + file));
|
||
|
String key = locale.id.toLowerCase();
|
||
|
|
||
|
// Register the locale
|
||
|
if (locales.containsKey(key)) throw new RuntimeException(
|
||
|
"Locale with ID '" + locale.id + "' already registered");
|
||
|
locales.put(key, locale);
|
||
|
|
||
|
// The locale matches the one in the config
|
||
|
if (key.equals("en-us"))
|
||
|
localizer.setLocale(locale);
|
||
|
}
|
||
|
|
||
|
// Could not process the file
|
||
|
catch (Exception e) {
|
||
|
System.err.println("Error parsing locale " +
|
||
|
file + ": " + e.getMessage());
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
// Produce the list of registered locales
|
||
|
this.locales = locales.values().toArray(
|
||
|
new Localizer.Locale[locales.size()]);
|
||
|
Arrays.sort(this.locales);
|
||
|
|
||
|
// Select a default locale
|
||
|
if (localizer.getLocale() != null || this.locales.length == 0)
|
||
|
return;
|
||
|
var locale = locales.get("en-us");
|
||
|
localizer.setLocale(locale != null ? locale : this.locales[0]);
|
||
|
}
|
||
|
|
||
|
}
|