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.Enumeration;
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.StringType;
import org.hl7.fhir.r5.renderers.CodeSystemRenderer.Translateable;
@ -445,7 +446,7 @@ public class CodeSystemRenderer extends TerminologyRenderer {
if (hasDisplay) {
td = tr.td();
renderDisplayName(c, cs, td);
hasExtensions = renderDisplayName(c, cs, td, langs) || hasExtensions;
}
if (hasDefinitions) {
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)) {
addMarkdown(renderStatusDiv(defn, td), defn.asStringValue());
} else {
@ -469,6 +470,7 @@ public class CodeSystemRenderer extends TerminologyRenderer {
List<Translateable> list = new ArrayList<>();
list.add(new Translateable(cs.getLanguage(), defn));
for (Extension ext : defn.getExtensionsByUrl(ToolingExtensions.EXT_TRANSLATION)) {
hasExtensions = true;
list.add(new Translateable(ext.getExtensionString("lang"), ext.getExtensionByUrl("content").getValueStringType()));
}
for (ConceptDefinitionDesignationComponent cd : c.getDesignation()) {
@ -525,39 +527,30 @@ public class CodeSystemRenderer extends TerminologyRenderer {
if (comment) {
td = tr.td();
Extension ext = c.getExtensionByUrl(ToolingExtensions.EXT_CS_COMMENT);
if (ext != null) {
if (ext != null && ext.hasValue() && ext.getValue().primitiveValue() != null) {
hasExtensions = true;
String bc = ext.hasValue() ? ext.getValue().primitiveValue() : null;
Map<String, String> translations = ToolingExtensions.getLanguageTranslations(ext.getValue());
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);
StringType defn = context.getTranslatedElement((PrimitiveType<?>) ext.getValue());
if (getContext().getMultiLanguagePolicy() == MultiLanguagePolicy.NONE ||!(ToolingExtensions.hasLanguageTranslations(ext.getValue()))) {
td.addText(defn.asStringValue());
} else {
if (bc != null)
translations.put(cs.getLanguage("en"), bc);
for (String l : translations.keySet()) {
if (l.equals(getContext().getLang())) {
td.addText(translations.get(l));
List<Translateable> list = new ArrayList<>();
list.add(new Translateable(cs.getLanguage(), defn));
for (Extension ex : defn.getExtensionsByUrl(ToolingExtensions.EXT_TRANSLATION)) {
hasExtensions = true;
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());
if (hasDisplay) {
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();
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 (getContext().getLang() == null) {
renderStatus(c.getDisplayElement(), td).addText(c.getDisplay());
} else if (getContext().getLang().equals("*")) {
boolean sl = false;
for (ConceptDefinitionDesignationComponent cd : c.getDesignation())
if (cd.getUse().is("http://terminology.hl7.org/CodeSystem/designation-usage", "display") && cd.hasLanguage() && !c.getDisplay().equalsIgnoreCase(cd.getValue()))
sl = true;
td.addText((sl ? cs.getLanguage("en")+": " : "")+c.getDisplay());
StringType disp = c.getDisplayElement();
List<Translateable> list = new ArrayList<>();
list.add(new Translateable(cs.getLanguage(), disp));
for (Extension ext : disp.getExtensionsByUrl(ToolingExtensions.EXT_TRANSLATION)) {
if (!langs.contains(ext.getExtensionString("lang"))) {
hasExtensions = true;
list.add(new Translateable(ext.getExtensionString("lang"), ext.getExtensionByUrl("content").getValueStringType()));
}
}
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());
if (cd.hasLanguage() && !langs.contains(cd.getLanguage()) && !c.getDefinition().equalsIgnoreCase(cd.getValue())) {
list.add(new Translateable(cd.getLanguage(), cd.getValueElement()));
}
}
} else if (getContext().getLang().equals(cs.getLanguage()) || (getContext().getLang().equals("en") && !cs.hasLanguage())) {
renderStatus(c.getDisplayElement(), td).addText(c.getDisplay());
if (getContext().getMultiLanguagePolicy() == MultiLanguagePolicy.NONE || list.size() <= 1) {
renderStatus(disp, td).addText(disp.asStringValue());
} else {
for (ConceptDefinitionDesignationComponent cd : c.getDesignation()) {
if (cd.getUse().is("http://terminology.hl7.org/CodeSystem/designation-usage", "display") && cd.hasLanguage() && cd.getLanguage().equals(getContext().getLang())) {
td.addText(cd.getValue());
}
}
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) {

View File

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

View File

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

View File

@ -903,4 +903,14 @@ public class RenderingContext extends RenderingI18nContext {
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.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Stream;
@ -158,7 +159,7 @@ public class ComparisonTests {
if (content.has("version")) {
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.setPkp(new TestProfileKnowledgeProvider(context));
if (content.has("version")) {