more work on language utilities

This commit is contained in:
Grahame Grieve 2024-03-20 22:44:13 +11:00
parent 4f1269dcff
commit 9a66c1968e
8 changed files with 45 additions and 24 deletions

View File

@ -1513,7 +1513,7 @@ public class Element extends Base implements NamedItem {
ext.addElement("valueCode").setValue(lang); ext.addElement("valueCode").setValue(lang);
ext = t.addElement("extension"); ext = t.addElement("extension");
ext.addElement("url").setValue("value"); ext.addElement("url").setValue("content");
ext.addElement("valueString").setValue(translation); ext.addElement("valueString").setValue(translation);
} }

View File

@ -385,9 +385,9 @@ public class LanguageUtils {
String v = null; String v = null;
for (Element subExt : ext.getChildren()) { for (Element subExt : ext.getChildren()) {
if ("Extension".equals(subExt.fhirType()) && "lang".equals(subExt.getNamedChildValue("url"))) { if ("Extension".equals(subExt.fhirType()) && "lang".equals(subExt.getNamedChildValue("url"))) {
lang = subExt.getNamedChildValue("value"); l = subExt.getNamedChildValue("value");
} }
if ("Extension".equals(subExt.fhirType()) && "lang".equals(subExt.getNamedChildValue("content"))) { if ("Extension".equals(subExt.fhirType()) && "content".equals(subExt.getNamedChildValue("url"))) {
v = subExt.getNamedChildValue("value"); v = subExt.getNamedChildValue("value");
} }
} }

View File

@ -24,7 +24,7 @@ public class ResourceLanguageFileBuilderTests {
ctxt.cacheResource(new JsonParser().parse(TestingUtilities.loadTestResourceStream("r5", "languages", "StructureDefinition-ed-translatable.json"))); 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"))); 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.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.prepare(new XLIFFProducer("[tmp]", "language", false), ctxt, "en", "fr");
lang.build(res); lang.build(res);
} }

View File

@ -13,8 +13,8 @@ import org.hl7.fhir.utilities.json.parser.JsonParser;
public class JsonLangFileProducer extends LanguageFileProducer { public class JsonLangFileProducer extends LanguageFileProducer {
public JsonLangFileProducer(String folder) { public JsonLangFileProducer(String rootFolder, String folderName, boolean useLangFolder) {
super(folder); super(rootFolder, folderName, useLangFolder);
} }
public JsonLangFileProducer() { public JsonLangFileProducer() {
@ -102,7 +102,7 @@ public class JsonLangFileProducer extends LanguageFileProducer {
} }
private String getFileName(String id, String baseLang) throws IOException { private String getFileName(String id, String baseLang) throws IOException {
return Utilities.path(getFolder(), id+"-"+baseLang+".json"); return Utilities.path(getRootFolder(), getFolderName(), id+"-"+baseLang+".json");
} }
@Override @Override
@ -132,7 +132,7 @@ public class JsonLangFileProducer extends LanguageFileProducer {
entry.add("source", tu.getSrcText()); entry.add("source", tu.getSrcText());
entry.add("target", tu.getTgtText()); entry.add("target", tu.getTgtText());
} }
TextFile.stringToFile(JsonParser.compose(json, true), Utilities.path(getFolder(), filename)); TextFile.stringToFile(JsonParser.compose(json, true), getTargetFileName(targetLang, filename));
} }
} }

View File

@ -8,6 +8,7 @@ import java.util.Map;
import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.ParserConfigurationException;
import org.hl7.fhir.utilities.Utilities;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
@ -143,21 +144,41 @@ public abstract class LanguageFileProducer {
public abstract void finish() throws IOException; public abstract void finish() throws IOException;
} }
private String folder; private String rootFolder;
private String folderName;
private boolean useLangFolder;
public LanguageFileProducer(String folder) { public LanguageFileProducer(String rootFolder, String folderName, boolean useLangFolder) {
super(); super();
this.folder = folder; this.rootFolder = rootFolder;
this.folderName = folderName;
this.useLangFolder = useLangFolder;
} }
public LanguageFileProducer() { public LanguageFileProducer() {
super(); super();
} }
public String getFolder() {
return folder; public String getRootFolder() {
return rootFolder;
} }
public String getFolderName() {
return folderName;
}
public boolean isUseLangFolder() {
return useLangFolder;
}
protected String getTargetFileName(String targetLang, String filename) throws IOException {
return Utilities.path(getRootFolder(), isUseLangFolder() ? targetLang : ".", getFolderName(), filename);
}
public abstract LanguageProducerSession startSession(String id, String baseLang) throws IOException; public abstract LanguageProducerSession startSession(String id, String baseLang) throws IOException;
public abstract void finish(); public abstract void finish();

View File

@ -15,8 +15,8 @@ public class PoGetTextProducer extends LanguageFileProducer {
private int filecount; private int filecount;
private boolean incLangInFilename; private boolean incLangInFilename;
public PoGetTextProducer(String folder) { public PoGetTextProducer(String rootFolder, String folderName, boolean useLangFolder) {
super(folder); super(rootFolder, folderName, useLangFolder);
} }
public PoGetTextProducer() { public PoGetTextProducer() {
@ -142,7 +142,7 @@ public class PoGetTextProducer extends LanguageFileProducer {
} }
private String getFileName(String id, String baseLang, String targetLang) throws IOException { private String getFileName(String id, String baseLang, String targetLang) throws IOException {
return Utilities.path(getFolder(), id+(incLangInFilename ? "-"+baseLang+"-"+targetLang+".po" : "")); return Utilities.path(getRootFolder(), getFolderName(), id+(incLangInFilename ? "-"+baseLang+"-"+targetLang+".po" : ""));
} }
public boolean isIncLangInFilename() { public boolean isIncLangInFilename() {
@ -171,7 +171,7 @@ public class PoGetTextProducer extends LanguageFileProducer {
ln(po, "msgstr \""+(tu.getTgtText() == null ? "" : stripEoln(tu.getTgtText()))+"\""); ln(po, "msgstr \""+(tu.getTgtText() == null ? "" : stripEoln(tu.getTgtText()))+"\"");
ln(po, ""); ln(po, "");
} }
TextFile.stringToFile(po.toString(), Utilities.path(getFolder(), filename)); TextFile.stringToFile(po.toString(), getTargetFileName(targetLang, filename));
} }
private String stripEoln(String s) { private String stripEoln(String s) {

View File

@ -39,7 +39,7 @@ public class XLIFFProducer extends LanguageFileProducer {
ln(" </body>"); ln(" </body>");
ln(" </file>"); ln(" </file>");
ln("</xliff>"); ln("</xliff>");
TextFile.stringToFile(xml.toString(), Utilities.path(getFolder(), id+".xliff")); TextFile.stringToFile(xml.toString(), Utilities.path(getRootFolder(), getFolderName(), id+".xliff"));
filecount++; filecount++;
} }
@ -80,8 +80,8 @@ public class XLIFFProducer extends LanguageFileProducer {
private int filecount; private int filecount;
public XLIFFProducer(String folder) { public XLIFFProducer(String rootFolder, String folderName, boolean useLangFolder) {
super(folder); super(rootFolder, folderName, useLangFolder);
} }
public XLIFFProducer() { public XLIFFProducer() {
@ -163,7 +163,7 @@ public class XLIFFProducer extends LanguageFileProducer {
ln(xml, " </body>"); ln(xml, " </body>");
ln(xml, " </file>"); ln(xml, " </file>");
ln(xml, "</xliff>"); ln(xml, "</xliff>");
TextFile.stringToFile(xml.toString(), Utilities.path(getFolder(), filename)); TextFile.stringToFile(xml.toString(), getTargetFileName(targetLang, filename));
} }

View File

@ -619,9 +619,9 @@ public class ValidationService {
private void transformLangExtract(CliContext cliContext, ValidationEngine validator) throws IOException { private void transformLangExtract(CliContext cliContext, ValidationEngine validator) throws IOException {
String dst = cliContext.getOutput(); String dst = cliContext.getOutput();
Utilities.createDirectory(dst); Utilities.createDirectory(dst);
PoGetTextProducer po = new PoGetTextProducer(Utilities.path(dst)); PoGetTextProducer po = new PoGetTextProducer(dst, ".", false);
XLIFFProducer xliff = new XLIFFProducer(Utilities.path(dst)); XLIFFProducer xliff = new XLIFFProducer(dst, ".", false);
JsonLangFileProducer jl = new JsonLangFileProducer(Utilities.path(dst)); JsonLangFileProducer jl = new JsonLangFileProducer(dst, ".", false);
List<SourceFile> refs = new ArrayList<>(); List<SourceFile> refs = new ArrayList<>();
ValidatorUtils.parseSources(cliContext.getSources(), refs, validator.getContext()); ValidatorUtils.parseSources(cliContext.getSources(), refs, validator.getContext());