More i18n work (#1592)

This commit is contained in:
Grahame Grieve 2024-04-10 08:18:58 +10:00
parent 0038e3a57b
commit 8ce99140aa
6 changed files with 106 additions and 59 deletions

View File

@ -23,6 +23,7 @@ import org.hl7.fhir.r5.model.CodeSystem.PropertyComponent;
import org.hl7.fhir.r5.model.Coding; import org.hl7.fhir.r5.model.Coding;
import org.hl7.fhir.r5.model.Enumeration; import org.hl7.fhir.r5.model.Enumeration;
import org.hl7.fhir.r5.model.Extension; import org.hl7.fhir.r5.model.Extension;
import org.hl7.fhir.r5.model.PrimitiveType;
import org.hl7.fhir.r5.model.Resource; import org.hl7.fhir.r5.model.Resource;
import org.hl7.fhir.r5.model.StringType; import org.hl7.fhir.r5.model.StringType;
import org.hl7.fhir.r5.renderers.CodeSystemRenderer.Translateable; import org.hl7.fhir.r5.renderers.CodeSystemRenderer.Translateable;
@ -445,7 +446,7 @@ public class CodeSystemRenderer extends TerminologyRenderer {
if (hasDisplay) { if (hasDisplay) {
td = tr.td(); td = tr.td();
renderDisplayName(c, cs, td); hasExtensions = renderDisplayName(c, cs, td, langs) || hasExtensions;
} }
if (hasDefinitions) { if (hasDefinitions) {
td = tr.td(); td = tr.td();
@ -459,7 +460,7 @@ public class CodeSystemRenderer extends TerminologyRenderer {
} }
} }
if (getContext().getMultiLanguagePolicy() == MultiLanguagePolicy.NONE && (sl || ToolingExtensions.hasLanguageTranslations(defn))) { if (getContext().getMultiLanguagePolicy() == MultiLanguagePolicy.NONE || !(sl || ToolingExtensions.hasLanguageTranslations(defn))) {
if (hasMarkdownInDefinitions(cs)) { if (hasMarkdownInDefinitions(cs)) {
addMarkdown(renderStatusDiv(defn, td), defn.asStringValue()); addMarkdown(renderStatusDiv(defn, td), defn.asStringValue());
} else { } else {
@ -469,6 +470,7 @@ public class CodeSystemRenderer extends TerminologyRenderer {
List<Translateable> list = new ArrayList<>(); List<Translateable> list = new ArrayList<>();
list.add(new Translateable(cs.getLanguage(), defn)); list.add(new Translateable(cs.getLanguage(), defn));
for (Extension ext : defn.getExtensionsByUrl(ToolingExtensions.EXT_TRANSLATION)) { for (Extension ext : defn.getExtensionsByUrl(ToolingExtensions.EXT_TRANSLATION)) {
hasExtensions = true;
list.add(new Translateable(ext.getExtensionString("lang"), ext.getExtensionByUrl("content").getValueStringType())); list.add(new Translateable(ext.getExtensionString("lang"), ext.getExtensionByUrl("content").getValueStringType()));
} }
for (ConceptDefinitionDesignationComponent cd : c.getDesignation()) { for (ConceptDefinitionDesignationComponent cd : c.getDesignation()) {
@ -525,39 +527,30 @@ public class CodeSystemRenderer extends TerminologyRenderer {
if (comment) { if (comment) {
td = tr.td(); td = tr.td();
Extension ext = c.getExtensionByUrl(ToolingExtensions.EXT_CS_COMMENT); Extension ext = c.getExtensionByUrl(ToolingExtensions.EXT_CS_COMMENT);
if (ext != null) { if (ext != null && ext.hasValue() && ext.getValue().primitiveValue() != null) {
hasExtensions = true; hasExtensions = true;
String bc = ext.hasValue() ? ext.getValue().primitiveValue() : null; StringType defn = context.getTranslatedElement((PrimitiveType<?>) ext.getValue());
Map<String, String> translations = ToolingExtensions.getLanguageTranslations(ext.getValue()); if (getContext().getMultiLanguagePolicy() == MultiLanguagePolicy.NONE ||!(ToolingExtensions.hasLanguageTranslations(ext.getValue()))) {
td.addText(defn.asStringValue());
if (getContext().getLang() == null) {
if (bc != null)
td.addText(bc);
} else if (getContext().getLang().equals("*")) {
boolean sl = false;
for (String l : translations.keySet())
if (bc == null || !bc.equalsIgnoreCase(translations.get(l)))
sl = true;
if (bc != null) {
td.addText((sl ? cs.getLanguage("en")+": " : "")+bc);
}
for (String l : translations.keySet()) {
if (bc == null || !bc.equalsIgnoreCase(translations.get(l))) {
if (!td.getChildNodes().isEmpty())
td.br();
td.addText(l+": "+translations.get(l));
}
}
} else if (getContext().getLang().equals(cs.getLanguage()) || (getContext().getLang().equals("en") && !cs.hasLanguage())) {
if (bc != null)
td.addText(bc);
} else { } else {
if (bc != null) List<Translateable> list = new ArrayList<>();
translations.put(cs.getLanguage("en"), bc); list.add(new Translateable(cs.getLanguage(), defn));
for (String l : translations.keySet()) { for (Extension ex : defn.getExtensionsByUrl(ToolingExtensions.EXT_TRANSLATION)) {
if (l.equals(getContext().getLang())) { hasExtensions = true;
td.addText(translations.get(l)); list.add(new Translateable(ex.getExtensionString("lang"), ex.getExtensionByUrl("content").getValueStringType()));
}
boolean first = true;
for (Translateable ti : list) {
if (first) {
first = false;
} else {
td.br();
} }
if (ti.lang != null) {
td.addText(ti.lang + ": ");
}
renderStatus(ti.getValue(), td).addText(ti.getValue().asStringValue());
} }
} }
} }
@ -633,7 +626,7 @@ public class CodeSystemRenderer extends TerminologyRenderer {
a.addText(cc.getCode()); a.addText(cc.getCode());
if (hasDisplay) { if (hasDisplay) {
td = tr.td(); td = tr.td();
renderDisplayName(cc, cs, td); hasExtensions = renderDisplayName(cc, cs, td, langs) || hasExtensions;
} }
int w = 1 + (deprecated ? 1 : 0) + (comment ? 1 : 0) + (version ? 1 : 0) + maps.size(); int w = 1 + (deprecated ? 1 : 0) + (comment ? 1 : 0) + (version ? 1 : 0) + maps.size();
if (properties != null) { if (properties != null) {
@ -676,32 +669,44 @@ public class CodeSystemRenderer extends TerminologyRenderer {
} }
public void renderDisplayName(ConceptDefinitionComponent c, CodeSystem cs, XhtmlNode td) { public boolean renderDisplayName(ConceptDefinitionComponent c, CodeSystem cs, XhtmlNode td, List<String> langs) {
boolean hasExtensions = false;
if (c.hasDisplayElement()) { if (c.hasDisplayElement()) {
if (getContext().getLang() == null) { StringType disp = c.getDisplayElement();
renderStatus(c.getDisplayElement(), td).addText(c.getDisplay()); List<Translateable> list = new ArrayList<>();
} else if (getContext().getLang().equals("*")) { list.add(new Translateable(cs.getLanguage(), disp));
boolean sl = false; for (Extension ext : disp.getExtensionsByUrl(ToolingExtensions.EXT_TRANSLATION)) {
for (ConceptDefinitionDesignationComponent cd : c.getDesignation()) if (!langs.contains(ext.getExtensionString("lang"))) {
if (cd.getUse().is("http://terminology.hl7.org/CodeSystem/designation-usage", "display") && cd.hasLanguage() && !c.getDisplay().equalsIgnoreCase(cd.getValue())) hasExtensions = true;
sl = true; list.add(new Translateable(ext.getExtensionString("lang"), ext.getExtensionByUrl("content").getValueStringType()));
td.addText((sl ? cs.getLanguage("en")+": " : "")+c.getDisplay());
for (ConceptDefinitionDesignationComponent cd : c.getDesignation()) {
if (cd.getUse().is("http://terminology.hl7.org/CodeSystem/designation-usage", "display") && cd.hasLanguage() && !c.getDisplay().equalsIgnoreCase(cd.getValue())) {
td.br();
td.addText(cd.getLanguage()+": "+cd.getValue());
}
} }
} else if (getContext().getLang().equals(cs.getLanguage()) || (getContext().getLang().equals("en") && !cs.hasLanguage())) { }
renderStatus(c.getDisplayElement(), td).addText(c.getDisplay()); for (ConceptDefinitionDesignationComponent cd : c.getDesignation()) {
} else { if (cd.hasLanguage() && !langs.contains(cd.getLanguage()) && !c.getDefinition().equalsIgnoreCase(cd.getValue())) {
for (ConceptDefinitionDesignationComponent cd : c.getDesignation()) { list.add(new Translateable(cd.getLanguage(), cd.getValueElement()));
if (cd.getUse().is("http://terminology.hl7.org/CodeSystem/designation-usage", "display") && cd.hasLanguage() && cd.getLanguage().equals(getContext().getLang())) { }
td.addText(cd.getValue()); }
}
} if (getContext().getMultiLanguagePolicy() == MultiLanguagePolicy.NONE || list.size() <= 1) {
} renderStatus(disp, td).addText(disp.asStringValue());
} else {
boolean first = true;
for (Translateable ti : list) {
if (first) {
first = false;
} else {
td.br();
}
if (ti.lang != null) {
td.addText(ti.lang + ": ");
}
renderStatus(ti.getValue(), td).addText(ti.getValue().asStringValue());
}
}
} }
return hasExtensions;
} }
private String getCodingReference(Coding cc, String system) { private String getCodingReference(Coding cc, String system) {

View File

@ -1422,14 +1422,16 @@ public class DataRenderer extends Renderer implements CodeResolver {
if (x.getName().equals("blockquote")) { if (x.getName().equals("blockquote")) {
x = x.para(); x = x.para();
} }
Currency c = Currency.getInstance(money.getCurrency()); Currency c = money.hasCurrency() ? Currency.getInstance(money.getCurrency()) : null;
if (c != null) { if (c != null) {
XhtmlNode s = x.span(null, c.getDisplayName()); XhtmlNode s = x.span(null, c.getDisplayName());
s.tx(c.getSymbol(context.getLocale())); s.tx(c.getSymbol(context.getLocale()));
s.tx(getLocalizedBigDecimalValue(money.getValue(), c)); s.tx(getLocalizedBigDecimalValue(money.getValue(), c));
x.tx(" ("+c.getCurrencyCode()+")"); x.tx(" ("+c.getCurrencyCode()+")");
} else { } else {
x.tx(money.getCurrency()); if (money.getCurrency() != null) {
x.tx(money.getCurrency());
}
x.tx(money.getValue().toPlainString()); x.tx(money.getValue().toPlainString());
} }
} }

View File

@ -84,6 +84,7 @@ public class ParametersRenderer extends ResourceRenderer {
XhtmlNode para = td.para(); XhtmlNode para = td.para();
para.tx(rw.fhirType()+"/"+rw.getId()); para.tx(rw.fhirType()+"/"+rw.getId());
para.an(rw.fhirType()+"_"+rw.getId()).tx(" "); para.an(rw.fhirType()+"_"+rw.getId()).tx(" ");
para.an("hc"+rw.fhirType()+"_"+rw.getId()).tx(" ");
XhtmlNode x = rw.getNarrative(); XhtmlNode x = rw.getNarrative();
if (x != null) { if (x != null) {
td.addChildren(x); td.addChildren(x);
@ -123,6 +124,7 @@ public class ParametersRenderer extends ResourceRenderer {
XhtmlNode para = td.para(); XhtmlNode para = td.para();
para.tx(r.fhirType()+"/"+r.getId()); para.tx(r.fhirType()+"/"+r.getId());
para.an(r.fhirType()+"_"+r.getId()).tx(" "); para.an(r.fhirType()+"_"+r.getId()).tx(" ");
para.an("hc"+r.fhirType()+"_"+r.getId()).tx(" ");
ResourceRenderer rr = RendererFactory.factory(r, context); ResourceRenderer rr = RendererFactory.factory(r, context);
rr.render(td, r); rr.render(td, r);
} else if (p.hasPart()) { } else if (p.hasPart()) {

View File

@ -902,5 +902,15 @@ public class RenderingContext extends RenderingI18nContext {
} }
return e.primitiveValue(); return e.primitiveValue();
} }
public RenderingContext withLocale(Locale locale) {
setLocale(locale);
return this;
}
public RenderingContext withLocaleCode(String locale) {
setLocale(new Locale(locale));
return this;
}
} }

View File

@ -0,0 +1,27 @@
package org.hl7.fhir.utilities.i18n;
import java.util.Locale;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class LocaleTests {
@Test
public void testSimple() {
Locale l = new Locale("en");
Assertions.assertEquals("en", l.getLanguage());
}
@Test
public void testSimple2() {
Locale l = new Locale("*");
Assertions.assertEquals("*", l.getLanguage());
}
@Test
public void testSimpleX() {
Locale l = new Locale("xx");
Assertions.assertEquals("xx", l.getLanguage());
}
}

View File

@ -9,6 +9,7 @@ import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -158,7 +159,7 @@ public class ComparisonTests {
if (content.has("version")) { if (content.has("version")) {
session.setAnnotate(true); session.setAnnotate(true);
} }
RenderingContext lrc = new RenderingContext(context, new MarkDownProcessor(Dialect.COMMON_MARK), null, "http://hl7.org/fhir", "", "en", ResourceRendererMode.TECHNICAL, GenerationRules.IG_PUBLISHER); RenderingContext lrc = new RenderingContext(context, new MarkDownProcessor(Dialect.COMMON_MARK), null, "http://hl7.org/fhir", "", new Locale("en"), ResourceRendererMode.TECHNICAL, GenerationRules.IG_PUBLISHER);
lrc.setDestDir(Utilities.path("[tmp]", "comparison")); lrc.setDestDir(Utilities.path("[tmp]", "comparison"));
lrc.setPkp(new TestProfileKnowledgeProvider(context)); lrc.setPkp(new TestProfileKnowledgeProvider(context));
if (content.has("version")) { if (content.has("version")) {