Add hardcoded keys to localizer

This commit is contained in:
James Agnew 2019-07-08 08:58:06 -04:00
parent 9e7cabd464
commit cc20893018
2 changed files with 30 additions and 12 deletions

View File

@ -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<String, MessageFormat> myKeyToMessageFormat = new ConcurrentHashMap<>();
private List<ResourceBundle> myBundle = new ArrayList<>();
private final Map<String, String> 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<String> 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;
}
}
}

View File

@ -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]+.*"));
}
}