update PO generator for plurals

This commit is contained in:
Grahame Grieve 2024-03-29 20:38:54 +11:00
parent 8d86abe0c1
commit d4d356731e
2 changed files with 91 additions and 14 deletions

View File

@ -7,10 +7,12 @@ import org.hl7.fhir.r5.model.Parameters;
import org.hl7.fhir.r5.model.Parameters.ParametersParameterComponent; import org.hl7.fhir.r5.model.Parameters.ParametersParameterComponent;
import org.hl7.fhir.r5.model.Resource; import org.hl7.fhir.r5.model.Resource;
import org.hl7.fhir.r5.renderers.utils.BaseWrappers.ResourceWrapper; import org.hl7.fhir.r5.renderers.utils.BaseWrappers.ResourceWrapper;
import org.hl7.fhir.r5.renderers.utils.Resolver.ResourceReferenceKind;
import org.w3c.dom.Element; import org.w3c.dom.Element;
public class Resolver { public class Resolver {
public interface IReferenceResolver { public interface IReferenceResolver {
ResourceWithReference resolve(RenderingContext context, String url); ResourceWithReference resolve(RenderingContext context, String url);
@ -174,16 +176,29 @@ public class Resolver {
} }
} }
public enum ResourceReferenceKind {
CONTAINED, BUNDLE, EXTERNAL, UNKNOWN
}
public static class ResourceWithReference { public static class ResourceWithReference {
private ResourceReferenceKind kind;
private String reference; private String reference;
private ResourceWrapper resource; private ResourceWrapper resource;
public ResourceWithReference(String reference, ResourceWrapper resource) { public ResourceWithReference(ResourceReferenceKind kind, String reference, ResourceWrapper resource) {
super();
this.kind = kind;
this.reference = reference; this.reference = reference;
this.resource = resource; this.resource = resource;
} }
public ResourceReferenceKind getKind() {
return kind;
}
public String getReference() { public String getReference() {
return reference; return reference;
} }

View File

@ -3,9 +3,12 @@ package org.hl7.fhir.utilities.i18n;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import org.hl7.fhir.utilities.TextFile; import org.hl7.fhir.utilities.TextFile;
import org.hl7.fhir.utilities.Utilities; import org.hl7.fhir.utilities.Utilities;
@ -16,10 +19,10 @@ public class POGenerator {
// if (true) { // if (true) {
// throw new Error("This needs to be rewritten for using existing .po files"); // throw new Error("This needs to be rewritten for using existing .po files");
// } // }
new POGenerator().execute(args[0], args[1], args[2], args[3], args.length == 5 ? args[4] : null); new POGenerator().execute(args[0], Integer.valueOf(args[1]), args[2], args[3], args[4], args.length == 6 ? args[5] : null);
} }
private void execute(String lang, String dest, String source, String current, String altVersion) throws IOException { private void execute(String lang, int count, String dest, String source, String current, String altVersion) throws IOException {
File dst = new File(dest); File dst = new File(dest);
if (dst.exists()) { if (dst.exists()) {
dst.delete(); dst.delete();
@ -31,24 +34,83 @@ public class POGenerator {
StringBuilder b = new StringBuilder(); StringBuilder b = new StringBuilder();
b.append("en -> "+lang+"\r\n"); b.append("en -> "+lang+"\r\n");
Set<String> plurals = new HashSet<>();
for (String n : Utilities.sorted(curr.keySet())) { for (String n : Utilities.sorted(curr.keySet())) {
if (n.endsWith("_one") || n.endsWith("_other") || n.endsWith("_many")) {
if (n.endsWith("_one")) {
n = n.substring(0, n.length() - 4);
} else if (n.endsWith("_other")) {
n = n.substring(0, n.length() - 6);
} else if (n.endsWith("_many")) {
n = n.substring(0, n.length() - 5);
}
if (!plurals.contains(n)) {
plurals.add(n);
b.append("\r\n");
String v1 = curr.get(n+"_one");
String vO = curr.get(n+"_other");
List<String> ll = new ArrayList<>();
addToList(ll, props, n+"_one");
addToList(ll, props, n+"_other");
addToList(ll, props, n+"_many");
String p1 = past == null ? null : past.get(n+"_one");
String pO = past == null ? null : past.get(n+"_other");
boolean changed = false;
if (ll.size() > 0 && p1 != null) {
if (!p1.equals(v1)) {
changed = true;
ll.set(0, "!!"+ll.get(0));
}
}
if (ll.size() > 1 && pO != null) {
if (!pO.equals(vO)) {
changed = true;
ll.set(1, "!!"+ll.get(1));
if (ll.size() == 3) {
ll.set(2, "!!"+ll.get(2));
}
}
}
b.append("#: "+n+"\r\n");
if (changed) {
b.append("#| one "+p1+"\r\n");
b.append("#| plural "+pO+"\r\n");
}
b.append("msgid \""+v1+"\"\r\n");
b.append("msgid_plural \""+vO+"\"\r\n");
for (int i = 0; i < count; i++) {
b.append("msgstr["+i+"] \""+(i < ll.size() ? ll.get(i) : "")+"\"\r\n");
}
}
} else {
b.append("\r\n"); b.append("\r\n");
String v = curr.get(n); String v = curr.get(n);
String l = props.get(n); String l = props.get(n);
if (l != null && past != null) { String p = past == null ? null : past.get(n);
String p = past.get(n); boolean changed = false;
if (p != null && !p.equals(v)) { if (l != null && p != null) {
// it's changed if (!p.equals(v)) {
changed = true;
l = "!!"+l; l = "!!"+l;
} }
} }
b.append("#: "+n+"\r\n"); b.append("#: "+n+"\r\n");
if (changed) {
b.append("#| "+p+"\r\n");
}
b.append("msgid \""+v+"\"\r\n"); b.append("msgid \""+v+"\"\r\n");
b.append("msgid \""+(l == null ? "" : l)+"\"\r\n"); b.append("msgstr \""+(l == null ? "" : l)+"\"\r\n");
}
} }
TextFile.stringToFile(b.toString(), dst); TextFile.stringToFile(b.toString(), dst);
} }
private void addToList(List<String> l, Map<String, String> props, String string) {
// TODO Auto-generated method stub
}
private Map<String, String> loadProperties(String source) throws IOException { private Map<String, String> loadProperties(String source) throws IOException {
if (source == null) { if (source == null) {
return null; return null;