From a132b2421eb524950fd41fa3ce7fffc305c36268 Mon Sep 17 00:00:00 2001 From: Graham Triggs Date: Tue, 29 Aug 2017 22:56:15 +0100 Subject: [PATCH] [VIVO-1294] Allow applications built on Vitro to have application-level messages distinct from themes --- .../webapp/i18n/VitroResourceBundle.java | 96 ++++++++++++------- 1 file changed, 61 insertions(+), 35 deletions(-) diff --git a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/i18n/VitroResourceBundle.java b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/i18n/VitroResourceBundle.java index 6bfc75434..5fa5e217d 100644 --- a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/i18n/VitroResourceBundle.java +++ b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/i18n/VitroResourceBundle.java @@ -9,7 +9,9 @@ import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; import java.text.MessageFormat; +import java.util.ArrayList; import java.util.Enumeration; +import java.util.List; import java.util.Properties; import java.util.ResourceBundle; @@ -49,6 +51,22 @@ public class VitroResourceBundle extends ResourceBundle { private static final String FILE_FLAG = "@@file "; private static final String MESSAGE_FILE_NOT_FOUND = "File {1} not found for property {0}."; + private static final List appPrefixes = new ArrayList<>(); + + static { + addAppPrefix("vitro"); + } + + public static void addAppPrefix(String prefix) { + if (!prefix.endsWith("-") && !prefix.endsWith("_")) { + prefix = prefix + "_"; + } + + if (!appPrefixes.contains(prefix)) { + appPrefixes.add(prefix); + } + } + // ---------------------------------------------------------------------- // Factory method // ---------------------------------------------------------------------- @@ -94,7 +112,6 @@ public class VitroResourceBundle extends ResourceBundle { private final String appI18nPath; private final String themeI18nPath; private final Control control; - private final Properties defaults; private final Properties properties; private VitroResourceBundle(String bundleName, ServletContext ctx, @@ -105,49 +122,58 @@ public class VitroResourceBundle extends ResourceBundle { this.appI18nPath = appI18nPath; this.themeI18nPath = themeI18nPath; this.control = control; - - this.defaults = new Properties(); - this.properties = new Properties(this.defaults); - - loadProperties(); + this.properties = loadProperties(); loadReferencedFiles(); } - private void loadProperties() throws IOException { + private Properties loadProperties() throws IOException { String resourceName = control.toResourceName(bundleName, "properties"); + Properties props = null; - String defaultsPath = joinPath(appI18nPath, resourceName); - String propertiesPath = joinPath(themeI18nPath, resourceName); - File defaultsFile = locateFile(defaultsPath); - File propertiesFile = locateFile(propertiesPath); + File defaultsPath = locateFile(joinPath(appI18nPath, resourceName)); + File propertiesPath = locateFile(joinPath(themeI18nPath, resourceName)); - if ((defaultsFile == null) && (propertiesFile == null)) { - throw new FileNotFoundException("Property file not found at '" - + defaultsPath + "' or '" + propertiesPath + "'"); - } - - if (defaultsFile != null) { - log.debug("Loading bundle '" + bundleName + "' defaults from '" - + defaultsPath + "'"); - FileInputStream stream = new FileInputStream(defaultsFile); - Reader reader = new InputStreamReader(stream, "UTF-8"); - try { - this.defaults.load(reader); - } finally { - reader.close(); + props = loadProperties(props, defaultsPath); + if (appPrefixes != null && appPrefixes.size() > 0) { + for (String appPrefix : appPrefixes) { + props = loadProperties(props, locateFile(joinPath(appI18nPath, (appPrefix + resourceName)))); } } - if (propertiesFile != null) { - log.debug("Loading bundle '" + bundleName + "' overrides from '" - + propertiesPath + "'"); - FileInputStream stream = new FileInputStream(propertiesFile); - Reader reader = new InputStreamReader(stream, "UTF-8"); - try { - this.properties.load(reader); - } finally { - reader.close(); - } + props = loadProperties(props, propertiesPath); + if (props == null) { + throw new FileNotFoundException("Property file not found at '" + defaultsPath + "' or '" + propertiesPath + "'"); } + props = loadProperties(props, locateFile(joinPath(appI18nPath, "local_" + resourceName))); + + return props; + } + + private Properties loadProperties(Properties defProps, File file) throws IOException { + if (file == null || !file.isFile()) { + return defProps; + } + + Properties props = null; + if (defProps != null) { + props = new Properties(defProps); + } else { + props = new Properties(); + } + + log.debug("Loading bundle '" + bundleName + "' defaults from '" + file + "'"); + FileInputStream stream = new FileInputStream(file); + Reader reader = new InputStreamReader(stream, "UTF-8"); + try { + props.load(reader); + } finally { + reader.close(); + } + + if (props.size() > 0) { + return props; + } + + return defProps; } private void loadReferencedFiles() throws IOException {