diff options
| author | Mürsel Türk | 2020-07-07 12:31:53 +0200 |
|---|---|---|
| committer | Mürsel Türk | 2020-07-07 12:31:53 +0200 |
| commit | af5e6f587ccfb68ad5189641d474d3df98a7f7f6 (patch) | |
| tree | f59802866f1c3505f975df41a4a30fb0981b4458 | |
| parent | [client] Add resource bundle files for wizard classes. Update the wizard clas... (diff) | |
| download | tutor-module-af5e6f587ccfb68ad5189641d474d3df98a7f7f6.tar.gz tutor-module-af5e6f587ccfb68ad5189641d474d3df98a7f7f6.tar.xz tutor-module-af5e6f587ccfb68ad5189641d474d3df98a7f7f6.zip | |
[client] Add helper classes for i18n.
| -rw-r--r-- | dozentenmodul/src/main/java/org/openslx/dozmod/gui/helper/I18n.java | 50 | ||||
| -rw-r--r-- | dozentenmodul/src/main/java/org/openslx/dozmod/gui/helper/UTF8Control.java | 54 |
2 files changed, 104 insertions, 0 deletions
diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/helper/I18n.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/helper/I18n.java new file mode 100644 index 00000000..fe976b59 --- /dev/null +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/helper/I18n.java @@ -0,0 +1,50 @@ +package org.openslx.dozmod.gui.helper; + +import org.apache.log4j.Logger; + +import java.text.MessageFormat; +import java.util.ResourceBundle; + +/** + * Helper enum for loading resource bundle files and getting values from them. + */ +public enum I18n { + ACTIVITY("activity"), + CONFIGURATOR("configurator"), + CONTROL("control"), + HELPER("helper"), + PAGE("page"), + PAGE_LAYOUT("page_layout"), + WINDOW("window"), + WINDOW_LAYOUT("window_layout"), + WIZARD("wizard"); + + /** + * Logger for this class + */ + private final static Logger LOGGER = Logger.getLogger(I18n.class); + + private final ResourceBundle resourceBundle; + + I18n(String bundleFilename) { + String baseName = "i18n." + bundleFilename; + resourceBundle = ResourceBundle.getBundle(baseName, new UTF8Control()); + } + + /** + * Returns i18n value for a given key and format the output at the appropriate places for (a) given parameter(s). + * If no key is found, return the key. + * @param key to get value + * @param params to get formatted output + * @return value represented by key or key on error + */ + public String getString(String key, Object... params) { + try { + String value = resourceBundle.getString(key); + return MessageFormat.format(value, params); + } catch (Exception e) { + LOGGER.error("Could not find a value for the given key: " + key); + return key; + } + } +}
\ No newline at end of file diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/helper/UTF8Control.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/helper/UTF8Control.java new file mode 100644 index 00000000..2f759323 --- /dev/null +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/helper/UTF8Control.java @@ -0,0 +1,54 @@ +package org.openslx.dozmod.gui.helper; + +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.URL; +import java.net.URLConnection; +import java.nio.charset.StandardCharsets; +import java.util.Locale; +import java.util.PropertyResourceBundle; +import java.util.ResourceBundle; + +/** + * Custom ResourceBundle.Control class to read properties files as UTF-8, where usually ISO-8859-1 is default. + */ +public class UTF8Control extends ResourceBundle.Control { + + @Override + public ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader, boolean reload) + throws IllegalAccessException, InstantiationException, IOException { + if (!format.equals("java.properties")) { + return super.newBundle(baseName, locale, format, loader, reload); + } + + String bundleName = toBundleName(baseName, locale); + String resourceName = toResourceName(bundleName, "properties"); + + ResourceBundle bundle = null; + InputStream stream = null; + + if (reload) { + URL url = loader.getResource(resourceName); + if (url != null) { + URLConnection connection = url.openConnection(); + if (connection != null) { + connection.setUseCaches(false); + stream = connection.getInputStream(); + } + } + } else { + stream = loader.getResourceAsStream(resourceName); + } + + if (stream != null) { + try { + bundle = new PropertyResourceBundle(new InputStreamReader(stream, StandardCharsets.UTF_8)); + } finally { + stream.close(); + } + } + + return bundle; + } +}
\ No newline at end of file |
