From cc208930183f1f163e4a9884cc2fd38a48c003fc Mon Sep 17 00:00:00 2001 From: James Agnew Date: Mon, 8 Jul 2019 08:58:06 -0400 Subject: [PATCH] Add hardcoded keys to localizer --- .../java/ca/uhn/fhir/i18n/HapiLocalizer.java | 32 +++++++++++++------ .../ca/uhn/fhir/i18n/HapiLocalizerTest.java | 10 ++++-- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/i18n/HapiLocalizer.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/i18n/HapiLocalizer.java index 313c99b8006..1eb9a69c8aa 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/i18n/HapiLocalizer.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/i18n/HapiLocalizer.java @@ -2,13 +2,13 @@ package ca.uhn.fhir.i18n; import ca.uhn.fhir.context.ConfigurationException; import ca.uhn.fhir.util.UrlUtil; +import ca.uhn.fhir.util.VersionUtil; import java.text.MessageFormat; import java.util.*; import java.util.concurrent.ConcurrentHashMap; -import static org.apache.commons.lang3.StringUtils.isNotBlank; -import static org.apache.commons.lang3.StringUtils.trim; +import static org.apache.commons.lang3.StringUtils.*; /* * #%L @@ -41,6 +41,7 @@ public class HapiLocalizer { private static boolean ourFailOnMissingMessage; private final Map myKeyToMessageFormat = new ConcurrentHashMap<>(); private List myBundle = new ArrayList<>(); + private final Map myHardcodedMessages = new HashMap<>(); private String[] myBundleNames; public HapiLocalizer() { @@ -50,6 +51,15 @@ public class HapiLocalizer { public HapiLocalizer(String... theBundleNames) { myBundleNames = theBundleNames; init(); + addMessage("hapi.version", VersionUtil.getVersion()); + } + + /** + * Subclasses may use this to add hardcoded messages + */ + @SuppressWarnings("WeakerAccess") + protected void addMessage(String theKey, String theMessage) { + myHardcodedMessages.put(theKey, theMessage); } public Set getAllKeys() { @@ -68,14 +78,16 @@ public class HapiLocalizer { */ @SuppressWarnings("WeakerAccess") public String getFormatString(String theQualifiedKey) { - String formatString = null; - for (ResourceBundle nextBundle : myBundle) { - if (nextBundle.containsKey(theQualifiedKey)) { - formatString = nextBundle.getString(theQualifiedKey); - formatString = trim(formatString); - } - if (isNotBlank(formatString)) { - break; + String formatString = myHardcodedMessages.get(theQualifiedKey); + if (isBlank(formatString)) { + for (ResourceBundle nextBundle : myBundle) { + if (nextBundle.containsKey(theQualifiedKey)) { + formatString = nextBundle.getString(theQualifiedKey); + formatString = trim(formatString); + } + if (isNotBlank(formatString)) { + break; + } } } diff --git a/hapi-fhir-base/src/test/java/ca/uhn/fhir/i18n/HapiLocalizerTest.java b/hapi-fhir-base/src/test/java/ca/uhn/fhir/i18n/HapiLocalizerTest.java index d63a5080fa3..3105b50c46f 100644 --- a/hapi-fhir-base/src/test/java/ca/uhn/fhir/i18n/HapiLocalizerTest.java +++ b/hapi-fhir-base/src/test/java/ca/uhn/fhir/i18n/HapiLocalizerTest.java @@ -1,7 +1,6 @@ package ca.uhn.fhir.i18n; -import static org.hamcrest.Matchers.empty; -import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.*; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; @@ -31,5 +30,12 @@ public class HapiLocalizerTest { svc.getMessage(next); } } + + @Test + public void testGetVersion() { + HapiLocalizer svc = new HapiLocalizer(); + String version = svc.getMessage("hapi.version"); + assertThat(version, matchesPattern("[0-9]+.*")); + } }