More work on language production
This commit is contained in:
parent
142c6503f8
commit
f13dc58d8b
|
@ -0,0 +1,34 @@
|
|||
package org.hl7.fhir.r5.elementmodel;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.lang3.NotImplementedException;
|
||||
import org.hl7.fhir.exceptions.DefinitionException;
|
||||
import org.hl7.fhir.exceptions.FHIRException;
|
||||
import org.hl7.fhir.exceptions.FHIRFormatError;
|
||||
import org.hl7.fhir.r5.context.IWorkerContext;
|
||||
import org.hl7.fhir.r5.elementmodel.ParserBase.NamedElement;
|
||||
import org.hl7.fhir.r5.formats.IParser.OutputStyle;
|
||||
|
||||
public class ResourceParser extends ParserBase {
|
||||
|
||||
public ResourceParser(IWorkerContext context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<NamedElement> parse(InputStream stream) throws IOException, FHIRFormatError, DefinitionException, FHIRException {
|
||||
throw new NotImplementedException("parse(InputStream stream)"); // doesns't make sense
|
||||
}
|
||||
|
||||
@Override
|
||||
public void compose(Element e, OutputStream destination, OutputStyle style, String base)
|
||||
throws FHIRException, IOException {
|
||||
throw new NotImplementedException("compose(Element e, OutputStream destination, OutputStyle style, String base)"); // doesns't make sense
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -21,7 +21,7 @@ public class ResourceLanguageFileBuilder {
|
|||
private String source;
|
||||
private String target;
|
||||
private IWorkerContext context;
|
||||
StructureDefinition sd = null;
|
||||
StructureDefinition profile = null;
|
||||
|
||||
public void prepare(LanguageFileProducer file, IWorkerContext context, String source, String target) {
|
||||
this.file = file;
|
||||
|
@ -30,17 +30,30 @@ public class ResourceLanguageFileBuilder {
|
|||
this.context = context;
|
||||
}
|
||||
|
||||
|
||||
public StructureDefinition getProfile() {
|
||||
return profile;
|
||||
}
|
||||
|
||||
public void setProfile(StructureDefinition profile) {
|
||||
this.profile = profile;
|
||||
}
|
||||
|
||||
public void build(Resource res) throws IOException {
|
||||
String id = res.fhirType();
|
||||
String path = res.fhirType() +"-"+res.getIdBase();
|
||||
|
||||
if (!source.equals(res.getLanguage())) {
|
||||
throw new FHIRException("Language mismatch");
|
||||
throw new FHIRException("Language mismatch: '"+source+"' => '"+target+"' but resource language is '"+res.getLanguage()+"'");
|
||||
}
|
||||
sd = context.fetchTypeDefinition(res.fhirType());
|
||||
if (sd == null) {
|
||||
throw new FHIRException("profile");
|
||||
|
||||
if (profile == null) {
|
||||
profile = context.fetchTypeDefinition(res.fhirType());
|
||||
if (profile == null) {
|
||||
throw new FHIRException("profile");
|
||||
}
|
||||
}
|
||||
|
||||
file.start(path, path, res.getUserString("path"), source, target);
|
||||
|
||||
for (Property p : res.children()) {
|
||||
|
@ -50,7 +63,6 @@ public class ResourceLanguageFileBuilder {
|
|||
file.finish();
|
||||
}
|
||||
|
||||
|
||||
private void process(Property p, String id, String path) throws IOException {
|
||||
if (p.hasValues()) {
|
||||
int i = 0;
|
||||
|
@ -58,7 +70,7 @@ public class ResourceLanguageFileBuilder {
|
|||
String pid = id+"."+p.getName();
|
||||
String ppath = path+"."+p.getName()+(p.isList() ? "["+i+"]" : "");
|
||||
i++;
|
||||
if (isTranslatable(p, b, id)) {
|
||||
if (isTranslatable(p, b, pid)) {
|
||||
file.makeEntry(ppath, null, null, b.primitiveValue(), getTranslation(b, target));
|
||||
}
|
||||
for (Property pp : b.children()) {
|
||||
|
@ -71,7 +83,7 @@ public class ResourceLanguageFileBuilder {
|
|||
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;
|
||||
for (ElementDefinition t : sd.getSnapshot().getElement()) {
|
||||
for (ElementDefinition t : profile.getSnapshot().getElement()) {
|
||||
if (t.getId().equals(id)) {
|
||||
ed = t;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
package org.hl7.fhir.r5.utils;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.hl7.fhir.exceptions.FHIRFormatError;
|
||||
import org.hl7.fhir.r5.context.IWorkerContext;
|
||||
import org.hl7.fhir.r5.formats.JsonParser;
|
||||
import org.hl7.fhir.r5.formats.XmlParser;
|
||||
import org.hl7.fhir.r5.model.Resource;
|
||||
import org.hl7.fhir.r5.model.StructureDefinition;
|
||||
import org.hl7.fhir.r5.test.utils.TestingUtilities;
|
||||
import org.hl7.fhir.utilities.Utilities;
|
||||
import org.hl7.fhir.utilities.i18n.XLIFFProducer;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class ResourceLanguageFileBuilderTests {
|
||||
|
||||
@Test
|
||||
public void testXLIFFGeneration() throws FHIRFormatError, IOException {
|
||||
Resource res = new JsonParser().parse(TestingUtilities.loadTestResourceStream("r5", "structuredefinition-language.json"));
|
||||
res.setUserData("path", "test.resource.xml");
|
||||
ResourceLanguageFileBuilder lang = new ResourceLanguageFileBuilder();
|
||||
IWorkerContext ctxt = TestingUtilities.getSharedWorkerContext();
|
||||
ctxt.cacheResource(new JsonParser().parse(TestingUtilities.loadTestResourceStream("r5", "languages", "StructureDefinition-ed-translatable.json")));
|
||||
ctxt.cacheResource(new JsonParser().parse(TestingUtilities.loadTestResourceStream("r5", "languages", "StructureDefinition-sd-translatable.json")));
|
||||
lang.setProfile(ctxt.fetchResource(StructureDefinition.class, "http://hl7.org/tests/fhir/StructureDefinition/sd-translatable"));
|
||||
lang.prepare(new XLIFFProducer(Utilities.path("[tmp]", "language")), ctxt, "en", "fr");
|
||||
lang.build(res);
|
||||
}
|
||||
|
||||
}
|
|
@ -20,32 +20,29 @@ public class XLIFFProducer extends LanguageFileProducer {
|
|||
public void start(String fileName, String contextId, String contextDesc, String baseLang, String targetLang) {
|
||||
this.fileName = fileName;
|
||||
xml = new StringBuilder();
|
||||
ln("<xliff xmlns=\"urn:oasis:names:tc:xliff:document:2.0\" version=\"2.0\"");
|
||||
ln(" srcLang=\""+baseLang+"\" trgLang=\""+targetLang+"\">");
|
||||
ln(" <file id=\""+contextId+"\">");
|
||||
ln(" <notes>");
|
||||
ln(" <note id=\"n1\">"+Utilities.escapeXml(contextDesc)+"</note>");
|
||||
ln(" </notes>");
|
||||
ln("<?xml version=\"1.0\" ?>\r\n");
|
||||
ln("<xliff xmlns=\"urn:oasis:names:tc:xliff:document:2.0\" version=\"2.0\">");
|
||||
ln(" <file source-language=\""+baseLang+"\" target-language=\""+targetLang+"\" id=\""+contextId+"\" original=\""+contextDesc+"\" datatype=\"KEYVALUEJSON\">");
|
||||
ln(" <body>");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void makeEntry(String id, String ref, String context, String source, String target) {
|
||||
i++;
|
||||
ln(" <unit id=\""+id+"\">");
|
||||
ln(" <trans-unit id=\""+id+"\" resname=\""+ref+"\">");
|
||||
if (context != null) {
|
||||
ln(" <notes>");
|
||||
ln(" <note id=\"n"+i+"\">"+Utilities.escapeXml(context)+"</note>");
|
||||
ln(" </notes>");
|
||||
ln(" <notes>");
|
||||
ln(" <note id=\"n"+i+"\">"+Utilities.escapeXml(context)+"</note>");
|
||||
ln(" </notes>");
|
||||
}
|
||||
ln(" <segment id=\""+ref+"\">");
|
||||
ln(" <source><pc id=\"src"+i+"\">"+Utilities.escapeXml(source)+"</source>");
|
||||
ln(" <target><pc id=\"tgt"+i+"\">"+Utilities.escapeXml(target)+"</target>");
|
||||
ln(" </segment>");
|
||||
ln(" </unit>");
|
||||
ln(" <source>"+Utilities.escapeXml(source)+"</source>");
|
||||
ln(" <target>"+Utilities.escapeXml(target)+"</target>");
|
||||
ln(" </trans-unit>");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void finish() throws IOException {
|
||||
ln(" </body>");
|
||||
ln(" </file>");
|
||||
ln("</xliff>");
|
||||
TextFile.stringToFile(xml.toString(), Utilities.path(getFolder(), fileName+".xliff"));
|
||||
|
|
Loading…
Reference in New Issue