diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/LanguageUtils.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/LanguageUtils.java index d224dcfc5..29aec862a 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/LanguageUtils.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/LanguageUtils.java @@ -12,6 +12,8 @@ import org.hl7.fhir.r5.context.IWorkerContext; import org.hl7.fhir.r5.model.CodeSystem; import org.hl7.fhir.r5.model.CodeSystem.ConceptDefinitionComponent; import org.hl7.fhir.r5.model.CodeSystem.ConceptDefinitionDesignationComponent; +import org.hl7.fhir.r5.model.CodeSystem.ConceptPropertyComponent; +import org.hl7.fhir.r5.model.DataType; import org.hl7.fhir.r5.model.Resource; import org.hl7.fhir.r5.terminologies.CodeSystemUtilities; import org.hl7.fhir.utilities.TextFile; @@ -36,7 +38,7 @@ import org.hl7.fhir.utilities.i18n.LanguageFileProducer.TranslationUnit; */ public class LanguageUtils { - public static final List TRANSLATION_SUPPLEMENT_RESOURCE_TYPES = Arrays.asList("CodeSystem", "StructureDefinition"); + public static final List TRANSLATION_SUPPLEMENT_RESOURCE_TYPES = Arrays.asList("CodeSystem", "StructureDefinition", "Questionnaire"); private static final String ORPHAN_TRANSLATIONS_NAME = "translations.orphans"; @@ -64,7 +66,7 @@ public class LanguageUtils { if (translation == null) { translation = element.getTranslation(langSession.getTargetLang()); } - langSession.entry(new TextUnit(pathForElement(element), base, translation)); + langSession.entry(new TextUnit(pathForElement(element), contextForElement(element), base, translation)); } } for (Element c: element.getChildren()) { @@ -75,6 +77,10 @@ public class LanguageUtils { } + private String contextForElement(Element element) { + throw new Error("Not done yet"); + } + private String getSpecialTranslation(Element parent, Element element, String targetLang) { if (parent == null) { return null; @@ -188,7 +194,7 @@ public class LanguageUtils { private Set findTranslations(String path, String src, Set translations) { Set res = new HashSet<>(); for (TranslationUnit translation : translations) { - if (path.equals(translation.getContext()) && src.equals(translation.getSrcText())) { + if (path.equals(translation.getId()) && src.equals(translation.getSrcText())) { res.add(translation); } } @@ -202,7 +208,7 @@ public class LanguageUtils { public static void fillSupplement(CodeSystem cs, List list) { cs.setUserData(SUPPLEMENT_NAME, "true"); for (TranslationUnit tu : list) { - ConceptDefinitionComponent cd = CodeSystemUtilities.getCode(cs, tu.getContext()); + ConceptDefinitionComponent cd = CodeSystemUtilities.getCode(cs, tu.getId()); if (cd != null && cd.hasDisplay() && cd.getDisplay().equals(tu.getSrcText())) { cd.addDesignation().setLanguage(tu.getLanguage()).setValue(tu.getTgtText()); } else { @@ -273,9 +279,18 @@ public class LanguageUtils { target = d.getValue(); } } - list.add(new TranslationUnit(lang, code, display, target)); + list.add(new TranslationUnit(lang, code, getDefinition(cd), display, target)); for (ConceptDefinitionComponent cd1 : cd.getConcept()) { generateTranslations(list, cd1, lang); } } + + private static String getDefinition(ConceptDefinitionComponent cd) { + ConceptPropertyComponent v = CodeSystemUtilities.getProperty(cd, "translation-context"); + if (v != null && v.hasValue()) { + return v.getValue().primitiveValue(); + } else { + return cd.getDefinition(); + } + } } diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/ResourceLanguageFileBuilder.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/ResourceLanguageFileBuilder.java index 8943beb01..27af2116f 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/ResourceLanguageFileBuilder.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/ResourceLanguageFileBuilder.java @@ -77,7 +77,7 @@ public class ResourceLanguageFileBuilder { String ppath = path+"."+p.getName()+(p.isList() ? "["+i+"]" : ""); i++; if (isTranslatable(p, b, pid)) { - sess.entry(new TextUnit(ppath, b.primitiveValue(), getTranslation(b, target))); + sess.entry(new TextUnit(ppath, getContext(), b.primitiveValue(), getTranslation(b, target))); } for (Property pp : b.children()) { process(sess, pp, pid, ppath); @@ -86,6 +86,11 @@ public class ResourceLanguageFileBuilder { } } + private String getContext() { + throw new Error("not done yet"); + } + + private boolean isTranslatable(Property p, Base b, String id) { if (new ContextUtilities(context).isPrimitiveDatatype(b.fhirType())) { // never any translations for non-primitives ElementDefinition ed = null; diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/i18n/JsonLangFileProducer.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/i18n/JsonLangFileProducer.java index 2b5e1e8d1..d24e8b2f9 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/i18n/JsonLangFileProducer.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/i18n/JsonLangFileProducer.java @@ -74,7 +74,10 @@ public class JsonLangFileProducer extends LanguageFileProducer { public void entry(TextUnit unit) { JsonObject entry = new JsonObject(); json.forceArray("entries").add(entry); - entry.add("context", unit.getContext()); + entry.add("id", unit.getId()); + if (unit.getContext1() != null) { + entry.add("context", unit.getContext1()); + } entry.add("source", unit.getSrcText()); entry.add("target", unit.getTgtText()); } @@ -88,7 +91,7 @@ public class JsonLangFileProducer extends LanguageFileProducer { JsonObject json = JsonParser.parseObject(source); for (JsonObject lang : json.forceArray("languages").asJsonObjects()) { for (JsonObject entry : lang.forceArray("entries").asJsonObjects()) { - list.add(new TranslationUnit(lang.asString("targetLang"), entry.asString("context"), entry.asString("source"), entry.asString("target"))); + list.add(new TranslationUnit(lang.asString("targetLang"), entry.asString("id"), entry.asString("context"), entry.asString("source"), entry.asString("target"))); } } return list; @@ -118,7 +121,10 @@ public class JsonLangFileProducer extends LanguageFileProducer { for (TranslationUnit tu : translations) { JsonObject entry = new JsonObject(); lj.forceArray("entries").add(entry); - entry.add("context", tu.getContext()); + entry.add("id", tu.getId()); + if (tu.getContext1() != null) { + entry.add("context", tu.getContext1()); + } entry.add("source", tu.getSrcText()); entry.add("target", tu.getTgtText()); } diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/i18n/LanguageFileProducer.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/i18n/LanguageFileProducer.java index 97da04bb7..2b5a1e19d 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/i18n/LanguageFileProducer.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/i18n/LanguageFileProducer.java @@ -18,35 +18,59 @@ import java.util.HashMap; public abstract class LanguageFileProducer { public static class TextUnit { + protected String id; protected String context; protected String srcText; protected String tgtText; - public TextUnit(String context, String srcText, String tgtText) { + public TextUnit(String id, String context, String srcText, String tgtText) { super(); + this.id = id; this.context = context; this.srcText = srcText; this.tgtText = tgtText; } - public String getContext() { + /** + * The identity of the item being translated + * + * @return + */ + public String getId() { + return id; + } + + /** + * Additional language that helps establish the context + * @return + */ + public String getContext1() { return context; } + /** + * The language that's being translated from + * + * @return + */ public String getSrcText() { return srcText; } + + /** + * The language that's being translated to + * + * @return + */ public String getTgtText() { return tgtText; } - - } public static class TranslationUnit extends TextUnit { private String language; - public TranslationUnit(String language, String context, String srcText, String tgtText) { - super(context, srcText, tgtText); + public TranslationUnit(String language, String id, String context, String srcText, String tgtText) { + super(id, context, srcText, tgtText); this.language = language; } diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/i18n/PoGetTextProducer.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/i18n/PoGetTextProducer.java index b035fb82e..798adc645 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/i18n/PoGetTextProducer.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/i18n/PoGetTextProducer.java @@ -81,10 +81,10 @@ public class PoGetTextProducer extends LanguageFileProducer { @Override public void entry(TextUnit unit) { - ln("#: "+unit.getContext()); - // if (context != null) { - // ln("#. "+context); - // } + ln("#: "+unit.getId()); + if (unit.getContext1() != null) { + ln("#. "+unit.getContext1()); + } ln("msgid \""+unit.getSrcText()+"\""); ln("msgstr \""+(unit.getTgtText() == null ? "" : unit.getTgtText())+"\""); ln(""); @@ -117,7 +117,7 @@ public class PoGetTextProducer extends LanguageFileProducer { lang = p[1].trim(); } } else if (s.startsWith("#:")) { - tu = new TranslationUnit(lang, s.substring(2).trim(), null, null); + tu = new TranslationUnit(lang, s.substring(2).trim(), null, null, null); } else { throw new IOException("Encountered unexpected line '"+s+"'"); } @@ -166,10 +166,10 @@ public class PoGetTextProducer extends LanguageFileProducer { ln(po, "# "+baseLang+" -> "+targetLang); ln(po, ""); for (TranslationUnit tu : translations) { - ln(po, "#: "+tu.getContext()); - // if (context != null) { - // ln("#. "+context); - // } + ln(po, "#: "+tu.getId()); + if (tu.getContext1() != null) { + ln(po, "#. "+tu.getContext1()); + } ln(po, "msgid \""+tu.getSrcText()+"\""); ln(po, "msgstr \""+(tu.getTgtText() == null ? "" : tu.getTgtText())+"\""); ln(po, ""); diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/i18n/XLIFFProducer.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/i18n/XLIFFProducer.java index d4c6c1ac1..33ad53b71 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/i18n/XLIFFProducer.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/i18n/XLIFFProducer.java @@ -50,12 +50,12 @@ public class XLIFFProducer extends LanguageFileProducer { @Override public void entry(TextUnit unit) { i++; - ln(" "); -// if (context != null) { -// ln(" "); -// ln(" "+Utilities.escapeXml(context)+""); -// ln(" "); -// } + ln(" "); + if (unit.getContext1() != null) { + ln(" "); + ln(" "+Utilities.escapeXml(unit.getContext1())+""); + ln(" "); + } ln(" "+Utilities.escapeXml(unit.getSrcText())+""); ln(" "+Utilities.escapeXml(unit.getTgtText())+""); ln(" "); @@ -114,7 +114,9 @@ public class XLIFFProducer extends LanguageFileProducer { for (Element file : XMLUtil.getNamedChildren(xliff, "file")) { Element body = XMLUtil.getNamedChild(file, "body"); for (Element transUnit : XMLUtil.getNamedChildren(body, "trans-unit")) { - TranslationUnit tu = new TranslationUnit(file.getAttribute("target-language"), transUnit.getAttribute("id"), + Element notes = XMLUtil.getNamedChild(transUnit, "notes"); + TranslationUnit tu = new TranslationUnit(file.getAttribute("target-language"), transUnit.getAttribute("id"), + notes == null ? null : XMLUtil.getNamedChildText(notes, "note"), XMLUtil.getNamedChildText(transUnit, "source"), XMLUtil.getNamedChildText(transUnit, "target")); if (!Utilities.noString(tu.getSrcText()) && !Utilities.noString(tu.getTgtText())) { list.add(tu); @@ -149,7 +151,14 @@ public class XLIFFProducer extends LanguageFileProducer { ln(xml, " "); ln(xml, " "); for (TranslationUnit tu : translations) { - ln(xml, " "); + int i = 0; + ln(xml, " "); + if (tu.getContext1() != null) { + i++; + ln(xml, " "); + ln(xml, " "+Utilities.escapeXml(tu.getContext1())+""); + ln(xml, " "); + } ln(xml, " "+Utilities.escapeXml(tu.getSrcText())+""); ln(xml, " "+Utilities.escapeXml(tu.getTgtText())+""); ln(xml, " ");