Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 45 additions & 10 deletions app/src/processing/app/Language.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@
*/
public class Language {
static private final String FILE = "processing.app.languages.PDE";
//static private final String LISTING = "processing/app/languages/languages.txt";
static private final String DEFAULT_DOMAIN = "processing.app";

// Store the language information in a file separate from the preferences,
// because preferences need the language on load time.
static protected final String PREF_FILE = "language.txt";
static protected final File prefFile = Base.getSettingsFile(PREF_FILE);


/** Single instance of this Language class */
static private volatile Language instance;
Expand All @@ -51,7 +52,7 @@ public class Language {
/** Available languages */
private HashMap<String, String> languages;

private ResourceBundle bundle;
private final Map<String, ResourceBundle> bundles = new HashMap<String, ResourceBundle>();


private Language() {
Expand Down Expand Up @@ -80,8 +81,9 @@ private Language() {
saveLanguage(language);
}

// Get bundle with translations (processing.app.language.PDE)
bundle = ResourceBundle.getBundle(Language.FILE, new Locale(this.language), new UTF8Control());
Locale.setDefault(Locale.forLanguageTag(language));

registerBundle(DEFAULT_DOMAIN, Language.FILE, this.getClass());
}


Expand Down Expand Up @@ -162,26 +164,59 @@ static public Language init() {
return instance;
}

/**
* Register a contribution in the internationalization system.
*/
static public void registerContribution(String domain, Object o) {
Class klass = o.getClass();
String bundleBase = klass.getCanonicalName().replace("." + klass.getSimpleName(), "");
String bundleName = bundleBase + ".languages." + klass.getSimpleName();
init().registerBundle(domain, bundleName, klass);
}

private void registerBundle(String domain, String bundleName, Class klass) {
Locale locale = new Locale(language);
ResourceBundle bundle = ResourceBundle.getBundle(bundleName, locale, klass.getClassLoader(), new UTF8Control());
bundles.put(domain, bundle);
}

private ResourceBundle getBundle(String domain) {
return bundles.get(domain);
}

/** Get translation from bundles. */
static public String text(String text) {
ResourceBundle bundle = init().bundle;
return text(text, DEFAULT_DOMAIN);
}

static public String text(String text, String domain) {
try {
ResourceBundle bundle = init().getBundle(domain);
return bundle.getString(text);
} catch (MissingResourceException e) {
return text;
}
}


static public String interpolate(String text, Object... arguments) {
return String.format(init().bundle.getString(text), arguments);
return interpolate(text, arguments);
}

static public String interpolate(String text, String domain, Object... arguments) {
return String.format(text(text, domain), arguments);
}


static public String pluralize(String text, int count) {
ResourceBundle bundle = init().bundle;
return pluralize(text, DEFAULT_DOMAIN, count);
}

static public String pluralize(String text, String domain, int count) {
ResourceBundle bundle;
try {
bundle = init().getBundle(domain);
} catch (MissingResourceException e) {
return text;
}

String fmt = text + ".%s";

Expand Down Expand Up @@ -221,7 +256,7 @@ static public String getLanguage() {
* Custom 'Control' class for consistent encoding.
* http://stackoverflow.com/questions/4659929/how-to-use-utf-8-in-resource-properties-with-resourcebundle
*/
class UTF8Control extends ResourceBundle.Control {
static class UTF8Control extends ResourceBundle.Control {
public ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader, boolean reload) throws IllegalAccessException, InstantiationException,IOException {
// The below is a copy of the default implementation.
String bundleName = toBundleName(baseName, locale);
Expand Down