From 7443eb3f5991d7a6ca400017bdd4011681b43e08 Mon Sep 17 00:00:00 2001 From: patrick-werner Date: Mon, 9 Mar 2020 17:25:08 +0100 Subject: [PATCH] added i18N handling to all BaseWorkerContexts --- .../fhir/dstu2/utils/BaseWorkerContext.java | 39 ++++++++++++++++++ .../hl7/fhir/dstu2/utils/IWorkerContext.java | 11 ++++- .../dstu2016may/utils/BaseWorkerContext.java | 38 ++++++++++++++++++ .../dstu2016may/utils/IWorkerContext.java | 11 ++++- .../fhir/dstu3/context/BaseWorkerContext.java | 38 ++++++++++++++++++ .../fhir/dstu3/context/IWorkerContext.java | 11 ++++- .../fhir/r4/context/BaseWorkerContext.java | 40 ++++++++++++++++++- .../hl7/fhir/r4/context/IWorkerContext.java | 11 ++++- .../fhir/r5/context/BaseWorkerContext.java | 29 ++++++++------ .../hl7/fhir/r5/context/IWorkerContext.java | 2 + 10 files changed, 212 insertions(+), 18 deletions(-) diff --git a/org.hl7.fhir.dstu2/src/main/java/org/hl7/fhir/dstu2/utils/BaseWorkerContext.java b/org.hl7.fhir.dstu2/src/main/java/org/hl7/fhir/dstu2/utils/BaseWorkerContext.java index 59024f9a3..c153e9be9 100644 --- a/org.hl7.fhir.dstu2/src/main/java/org/hl7/fhir/dstu2/utils/BaseWorkerContext.java +++ b/org.hl7.fhir.dstu2/src/main/java/org/hl7/fhir/dstu2/utils/BaseWorkerContext.java @@ -23,11 +23,15 @@ package org.hl7.fhir.dstu2.utils; import java.io.FileNotFoundException; import java.io.IOException; +import java.text.MessageFormat; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Locale; import java.util.Map; +import java.util.Objects; +import java.util.ResourceBundle; import java.util.Set; import org.hl7.fhir.dstu2.model.BooleanType; @@ -73,6 +77,8 @@ public abstract class BaseWorkerContext implements IWorkerContext { // private ValueSetExpansionCache expansionCache; // protected FHIRToolingClient txServer; + private Locale locale; + private ResourceBundle i18Nmessages; @Override public ValueSet fetchCodeSystem(String system) { @@ -402,4 +408,37 @@ public abstract class BaseWorkerContext implements IWorkerContext { return fetchResource(StructureDefinition.class, "http://hl7.org/fhir/StructureDefinition/"+typeName); } + @Override + public Locale getLocale() { + if (Objects.nonNull(locale)){ + return locale; + } else { + return Locale.US; + } + } + + @Override + public void setLocale(Locale locale) { + this.locale = locale; + setValidationMessageLanguage(getLocale()); + } + + @Override + public String formatMessage(String theMessage, Object... theMessageArguments) { + String message; + if (theMessageArguments != null && theMessageArguments.length > 0) { + message = MessageFormat.format(i18Nmessages.getString(theMessage), theMessageArguments); + } else if (i18Nmessages.containsKey(theMessage)) { + message = i18Nmessages.getString(theMessage); + } else { + message = theMessage; + } + return message; + } + + @Override + public void setValidationMessageLanguage(Locale locale) { + i18Nmessages = ResourceBundle.getBundle("Messages", locale ); + } + } diff --git a/org.hl7.fhir.dstu2/src/main/java/org/hl7/fhir/dstu2/utils/IWorkerContext.java b/org.hl7.fhir.dstu2/src/main/java/org/hl7/fhir/dstu2/utils/IWorkerContext.java index c566a64f8..ddf1dd822 100644 --- a/org.hl7.fhir.dstu2/src/main/java/org/hl7/fhir/dstu2/utils/IWorkerContext.java +++ b/org.hl7.fhir.dstu2/src/main/java/org/hl7/fhir/dstu2/utils/IWorkerContext.java @@ -23,6 +23,7 @@ package org.hl7.fhir.dstu2.utils; import java.util.List; +import java.util.Locale; import org.hl7.fhir.dstu2.formats.IParser; import org.hl7.fhir.dstu2.formats.ParserType; import org.hl7.fhir.dstu2.model.CodeableConcept; @@ -208,7 +209,15 @@ public interface IWorkerContext { * @return */ public ValueSetExpansionComponent expandVS(ConceptSetComponent inc); - + + Locale getLocale(); + + void setLocale(Locale locale); + + String formatMessage(String theMessage, Object... theMessageArguments); + + void setValidationMessageLanguage(Locale locale); + public class ValidationResult { private ConceptDefinitionComponent definition; private IssueSeverity severity; diff --git a/org.hl7.fhir.dstu2016may/src/main/java/org/hl7/fhir/dstu2016may/utils/BaseWorkerContext.java b/org.hl7.fhir.dstu2016may/src/main/java/org/hl7/fhir/dstu2016may/utils/BaseWorkerContext.java index f56158e9d..b48c1ddb4 100644 --- a/org.hl7.fhir.dstu2016may/src/main/java/org/hl7/fhir/dstu2016may/utils/BaseWorkerContext.java +++ b/org.hl7.fhir.dstu2016may/src/main/java/org/hl7/fhir/dstu2016may/utils/BaseWorkerContext.java @@ -23,11 +23,15 @@ package org.hl7.fhir.dstu2016may.utils; import java.io.FileNotFoundException; import java.io.IOException; +import java.text.MessageFormat; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Locale; import java.util.Map; +import java.util.Objects; +import java.util.ResourceBundle; import java.util.Set; import org.hl7.fhir.dstu2016may.model.BooleanType; @@ -75,6 +79,8 @@ public abstract class BaseWorkerContext implements IWorkerContext { protected FHIRToolingClient txServer; private Bundle bndCodeSystems; + private Locale locale; + private ResourceBundle i18Nmessages; @Override public CodeSystem fetchCodeSystem(String system) { @@ -441,5 +447,37 @@ public abstract class BaseWorkerContext implements IWorkerContext { return fetchResource(StructureDefinition.class, "http://hl7.org/fhir/StructureDefinition/"+typeName); } + @Override + public Locale getLocale() { + if (Objects.nonNull(locale)){ + return locale; + } else { + return Locale.US; + } + } + + @Override + public void setLocale(Locale locale) { + this.locale = locale; + setValidationMessageLanguage(getLocale()); + } + + @Override + public String formatMessage(String theMessage, Object... theMessageArguments) { + String message; + if (theMessageArguments != null && theMessageArguments.length > 0) { + message = MessageFormat.format(i18Nmessages.getString(theMessage), theMessageArguments); + } else if (i18Nmessages.containsKey(theMessage)) { + message = i18Nmessages.getString(theMessage); + } else { + message = theMessage; + } + return message; + } + + @Override + public void setValidationMessageLanguage(Locale locale) { + i18Nmessages = ResourceBundle.getBundle("Messages", locale ); + } } diff --git a/org.hl7.fhir.dstu2016may/src/main/java/org/hl7/fhir/dstu2016may/utils/IWorkerContext.java b/org.hl7.fhir.dstu2016may/src/main/java/org/hl7/fhir/dstu2016may/utils/IWorkerContext.java index d9e91b6ee..fa96c4915 100644 --- a/org.hl7.fhir.dstu2016may/src/main/java/org/hl7/fhir/dstu2016may/utils/IWorkerContext.java +++ b/org.hl7.fhir.dstu2016may/src/main/java/org/hl7/fhir/dstu2016may/utils/IWorkerContext.java @@ -22,6 +22,7 @@ package org.hl7.fhir.dstu2016may.utils; import java.util.List; +import java.util.Locale; import java.util.Set; import org.hl7.fhir.dstu2016may.formats.IParser; @@ -204,7 +205,15 @@ public interface IWorkerContext { * @return */ public ValueSetExpansionComponent expandVS(ConceptSetComponent inc); - + + Locale getLocale(); + + void setLocale(Locale locale); + + String formatMessage(String theMessage, Object... theMessageArguments); + + void setValidationMessageLanguage(Locale locale); + public class ValidationResult { private ConceptDefinitionComponent definition; private IssueSeverity severity; diff --git a/org.hl7.fhir.dstu3/src/main/java/org/hl7/fhir/dstu3/context/BaseWorkerContext.java b/org.hl7.fhir.dstu3/src/main/java/org/hl7/fhir/dstu3/context/BaseWorkerContext.java index 8fbcee29a..8b33adf08 100644 --- a/org.hl7.fhir.dstu3/src/main/java/org/hl7/fhir/dstu3/context/BaseWorkerContext.java +++ b/org.hl7.fhir.dstu3/src/main/java/org/hl7/fhir/dstu3/context/BaseWorkerContext.java @@ -27,11 +27,15 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; +import java.text.MessageFormat; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Locale; import java.util.Map; +import java.util.Objects; +import java.util.ResourceBundle; import java.util.Set; import ca.uhn.fhir.rest.api.Constants; @@ -122,6 +126,8 @@ public abstract class BaseWorkerContext implements IWorkerContext { private int expandCodesLimit = 1000; protected ILoggingService logger; protected ExpansionProfile expProfile; + private Locale locale; + private ResourceBundle i18Nmessages; public Map getCodeSystems() { return codeSystems; @@ -991,5 +997,37 @@ public abstract class BaseWorkerContext implements IWorkerContext { return fetchResource(StructureDefinition.class, "http://hl7.org/fhir/StructureDefinition/"+typeName); } + @Override + public Locale getLocale() { + if (Objects.nonNull(locale)){ + return locale; + } else { + return Locale.US; + } + } + + @Override + public void setLocale(Locale locale) { + this.locale = locale; + setValidationMessageLanguage(getLocale()); + } + + @Override + public String formatMessage(String theMessage, Object... theMessageArguments) { + String message; + if (theMessageArguments != null && theMessageArguments.length > 0) { + message = MessageFormat.format(i18Nmessages.getString(theMessage), theMessageArguments); + } else if (i18Nmessages.containsKey(theMessage)) { + message = i18Nmessages.getString(theMessage); + } else { + message = theMessage; + } + return message; + } + + @Override + public void setValidationMessageLanguage(Locale locale) { + i18Nmessages = ResourceBundle.getBundle("Messages", locale ); + } } diff --git a/org.hl7.fhir.dstu3/src/main/java/org/hl7/fhir/dstu3/context/IWorkerContext.java b/org.hl7.fhir.dstu3/src/main/java/org/hl7/fhir/dstu3/context/IWorkerContext.java index f51e69f89..bcfc0ef98 100644 --- a/org.hl7.fhir.dstu3/src/main/java/org/hl7/fhir/dstu3/context/IWorkerContext.java +++ b/org.hl7.fhir.dstu3/src/main/java/org/hl7/fhir/dstu3/context/IWorkerContext.java @@ -22,6 +22,7 @@ package org.hl7.fhir.dstu3.context; import java.util.List; +import java.util.Locale; import java.util.Set; import org.hl7.fhir.dstu3.formats.IParser; @@ -236,7 +237,15 @@ public interface IWorkerContext { * @throws FHIRException */ public ValueSetExpansionComponent expandVS(ConceptSetComponent inc, boolean heiarchical) throws TerminologyServiceException; - + + Locale getLocale(); + + void setLocale(Locale locale); + + String formatMessage(String theMessage, Object... theMessageArguments); + + void setValidationMessageLanguage(Locale locale); + public class ValidationResult { private ConceptDefinitionComponent definition; private IssueSeverity severity; diff --git a/org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/context/BaseWorkerContext.java b/org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/context/BaseWorkerContext.java index 75a4b365a..3bb377f18 100644 --- a/org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/context/BaseWorkerContext.java +++ b/org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/context/BaseWorkerContext.java @@ -21,6 +21,10 @@ package org.hl7.fhir.r4.context; */ import com.google.gson.JsonObject; +import java.text.MessageFormat; +import java.util.Locale; +import java.util.Objects; +import java.util.ResourceBundle; import org.apache.commons.lang3.StringUtils; import org.fhir.ucum.UcumService; import org.hl7.fhir.exceptions.DefinitionException; @@ -99,7 +103,9 @@ public abstract class BaseWorkerContext implements IWorkerContext { protected TerminologyCache txCache; private boolean tlogging = true; - + private Locale locale; + private ResourceBundle i18Nmessages; + public BaseWorkerContext() throws FileNotFoundException, IOException, FHIRException { super(); txCache = new TerminologyCache(lock, null); @@ -1174,5 +1180,37 @@ public abstract class BaseWorkerContext implements IWorkerContext { return null; } + @Override + public Locale getLocale() { + if (Objects.nonNull(locale)){ + return locale; + } else { + return Locale.US; + } + } + + @Override + public void setLocale(Locale locale) { + this.locale = locale; + setValidationMessageLanguage(getLocale()); + } + + @Override + public String formatMessage(String theMessage, Object... theMessageArguments) { + String message; + if (theMessageArguments != null && theMessageArguments.length > 0) { + message = MessageFormat.format(i18Nmessages.getString(theMessage), theMessageArguments); + } else if (i18Nmessages.containsKey(theMessage)) { + message = i18Nmessages.getString(theMessage); + } else { + message = theMessage; + } + return message; + } + + @Override + public void setValidationMessageLanguage(Locale locale) { + i18Nmessages = ResourceBundle.getBundle("Messages", locale ); + } } diff --git a/org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/context/IWorkerContext.java b/org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/context/IWorkerContext.java index 765379522..b5ce51f24 100644 --- a/org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/context/IWorkerContext.java +++ b/org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/context/IWorkerContext.java @@ -22,6 +22,7 @@ package org.hl7.fhir.r4.context; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.Set; @@ -283,7 +284,15 @@ public interface IWorkerContext { * @throws FHIRException */ public ValueSetExpansionOutcome expandVS(ConceptSetComponent inc, boolean heirarchical) throws TerminologyServiceException; - + + Locale getLocale(); + + void setLocale(Locale locale); + + String formatMessage(String theMessage, Object... theMessageArguments); + + void setValidationMessageLanguage(Locale locale); + public class ValidationResult { private ConceptDefinitionComponent definition; private IssueSeverity severity; diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/BaseWorkerContext.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/BaseWorkerContext.java index 1c7555d06..314a0385c 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/BaseWorkerContext.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/BaseWorkerContext.java @@ -425,19 +425,6 @@ public abstract class BaseWorkerContext implements IWorkerContext { this.expandCodesLimit = expandCodesLimit; } - @Override - public Locale getLocale() { - if (Objects.nonNull(locale)){ - return locale; - } else { - return Locale.US; - } - } - - @Override - public void setLocale(Locale locale) { - this.locale = locale; - } @Override public ValueSetExpansionOutcome expandVS(ElementDefinitionBindingComponent binding, boolean cacheOk, boolean heirarchical) throws FHIRException { ValueSet vs = null; @@ -1293,6 +1280,21 @@ public abstract class BaseWorkerContext implements IWorkerContext { return binaries; } + @Override + public Locale getLocale() { + if (Objects.nonNull(locale)){ + return locale; + } else { + return Locale.US; + } + } + + @Override + public void setLocale(Locale locale) { + this.locale = locale; + setValidationMessageLanguage(getLocale()); + } + @Override public String formatMessage(String theMessage, Object... theMessageArguments) { String message; @@ -1306,6 +1308,7 @@ public abstract class BaseWorkerContext implements IWorkerContext { return message; } + @Override public void setValidationMessageLanguage(Locale locale) { i18Nmessages = ResourceBundle.getBundle("Messages", locale ); } diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/IWorkerContext.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/IWorkerContext.java index 28987f401..aa9143962 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/IWorkerContext.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/IWorkerContext.java @@ -334,6 +334,8 @@ public interface IWorkerContext { String formatMessage(String theMessage, Object... theMessageArguments); + void setValidationMessageLanguage(Locale locale); + class ValidationResult { private ConceptDefinitionComponent definition; private IssueSeverity severity;