Skip to content

Commit

Permalink
Fix generation of lang bundle after mdb-cedar sync
Browse files Browse the repository at this point in the history
2 things needed fixing:
- Properties with ':' in their name
- Making sure existing non changing properties are kept when generating new
values for the resynced cedar template
  • Loading branch information
beepsoft committed Oct 18, 2023
1 parent 4479b7b commit eff2298
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 22 deletions.
56 changes: 36 additions & 20 deletions src/main/java/edu/harvard/iq/dataverse/arp/ArpServiceBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -956,17 +956,17 @@ public void updateMetadataBlock(String dvIdtf, String metadataBlockName) throws
}


public static ArrayList<String> collectHunTranslations(String cedarTemplate, String parentPath, ArrayList<String> hunTranslations) {
public static Map<String, String> collectHunTranslations(String cedarTemplate, String parentPath, Map<String, String> hunTranslations) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonObject cedarTemplateJson = gson.fromJson(cedarTemplate, JsonObject.class);

if (parentPath.equals("/properties")) {
hunTranslations.add("metadatablock.name = " + getJsonElement(cedarTemplateJson, "schema:name").getAsString());
hunTranslations.put("metadatablock.name", getJsonElement(cedarTemplateJson, "hunName").getAsString());
if (cedarTemplateJson.has("hunName")) {
hunTranslations.add("metadatablock.displayName = " + getJsonElement(cedarTemplateJson, "hunName").getAsString());
hunTranslations.put("metadatablock.displayName", getJsonElement(cedarTemplateJson, "hunName").getAsString());
}
if (cedarTemplateJson.has("hunDescription")) {
hunTranslations.add("metadatablock.description = " + getJsonElement(cedarTemplateJson, "hunDescription").getAsString());
hunTranslations.put("metadatablock.description", getJsonElement(cedarTemplateJson, "hunDescription").getAsString());
}
}

Expand All @@ -982,31 +982,28 @@ public static ArrayList<String> collectHunTranslations(String cedarTemplate, Str
actProp = actProp.getAsJsonObject("items");
}

String dftName = getJsonElement(actProp, "schema:name").getAsString();
String dftName = getJsonElement(actProp, "schema:name").getAsString().replace(":", ".");

//Label
if (actProp.has("hunLabel")) {
String hunLabel = getJsonElement(actProp, "hunLabel").getAsString();
hunTranslations.add(String.format("datasetfieldtype.%1$s.title = %2$s", dftName, hunLabel));
hunTranslations.put(String.format("datasetfieldtype.%1$s.title", dftName), hunLabel);
}
else{
hunTranslations.add(String.format("datasetfieldtype.%1$s.title = %2$s",
dftName,
getJsonElement(actProp, "skos:prefLabel").getAsString())+" (magyarul)");
hunTranslations.put(String.format("datasetfieldtype.%1$s.title", dftName),
getJsonElement(actProp, "skos:prefLabel").getAsString()+" (magyarul)");
}

// Help text / tip
if (actProp.has("hunDescription")) {
hunTranslations.add(String.format("datasetfieldtype.%1$s.description = %2$s",
dftName,
actProp.get("hunDescription").getAsString()));
hunTranslations.put(String.format("datasetfieldtype.%1$s.description", dftName),
actProp.get("hunDescription").getAsString());
}
else {
// Note: english help text should be in schema:description, but it is not, it is in
// propertyDescriptions
hunTranslations.add(String.format("datasetfieldtype.%1$s.description = %2$s",
dftName,
propertyDescriptions.get(dftName).getAsString())+" (magyarul)");
hunTranslations.put(String.format("datasetfieldtype.%1$s.description", dftName),
propertyDescriptions.get(prop).getAsString()+" (magyarul)");
}

// TODO: revise how elemnets work!
Expand All @@ -1024,14 +1021,14 @@ public static ArrayList<String> collectHunTranslations(String cedarTemplate, Str
}
}
if (propType.equals("TemplateElement") || propType.equals("array")) {
dftName = getJsonElement(actProp, "schema:name").getAsString();
dftName = getJsonElement(actProp, "schema:name").getAsString().replace(":", ".");
if (actProp.has("hunTitle") && !actProp.has("hunLabel")) {
String hunTitle = getJsonElement(actProp, "hunTitle").getAsString();
hunTranslations.add(String.format("datasetfieldtype.%1$s.title = %2$s", dftName, hunTitle));
hunTranslations.put(String.format("datasetfieldtype.%1$s.title", dftName), hunTitle);
}
if (actProp.has("hunDescription")) {
String hunDescription = getJsonElement(actProp, "hunDescription").getAsString();
hunTranslations.add(String.format("datasetfieldtype.%1$s.description = %2$s", dftName, hunDescription));
hunTranslations.put(String.format("datasetfieldtype.%1$s.description", dftName), hunDescription);
}
collectHunTranslations(actProp.toString(), newPath, hunTranslations);
}
Expand Down Expand Up @@ -1081,9 +1078,27 @@ public String createOrUpdateMdbFromCedarTemplate(String dvIdtf, String templateJ
String langDirPath = System.getProperty("dataverse.lang.directory");
if (langDirPath != null) {
String fileName = metadataBlockName + "_hu.properties";
List<String> hunTranslations = collectHunTranslations(templateJson, "/properties", new ArrayList<>());
ResourceBundle resourceBundle = BundleUtil.getResourceBundle(metadataBlockName, Locale.forLanguageTag("hu"));

// Load with current translations
Map<String, String> hunTranslations = new TreeMap<>();
Enumeration<String> keys = resourceBundle.getKeys();
while (keys.hasMoreElements()) {
String key = keys.nextElement();
hunTranslations.put(key, resourceBundle.getString(key));
}

// Update with translations from templateJson
collectHunTranslations(templateJson, "/properties", hunTranslations);

// Convert back to properties file format
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, String> entry : hunTranslations.entrySet()) {
sb.append(entry.getKey()).append('=').append(entry.getValue()).append('\n');
}

FileWriter writer = new FileWriter(langDirPath + "/" + fileName);
writer.write(String.join("\n", hunTranslations));
writer.write(sb.toString());
writer.close();
}
// Force reloading language bundles/
Expand Down Expand Up @@ -1111,6 +1126,7 @@ public void syncMetadataBlockWithCedar(String mdbName, ExportToCedarParams cedar
String templateJson = exportTemplateToCedar(cedarTemplate, cedarParams);
createOrUpdateMdbFromCedarTemplate("root", templateJson, false);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Syncing metadatablock '"+mdbName+"' with CEDAR failed: "+e.getLocalizedMessage(), e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,15 +338,17 @@ private void processCommonFields(JsonObject cedarTemplate, DataverseDatasetField
cedarTemplate.addProperty("schema:name", propName);
cedarTemplate.addProperty("schema:description", datasetField.getDescription());
try {
cedarTemplate.addProperty("hunDescription", BundleUtil.getStringFromPropertyFile("datasetfieldtype." + propName + ".description", datasetField.getmetadatablock_id(), hunLocale));
// We need dot notation to access prop translation
cedarTemplate.addProperty("hunDescription", BundleUtil.getStringFromPropertyFile("datasetfieldtype." + propName.replace(":", ".") + ".description", datasetField.getmetadatablock_id(), hunLocale));
}
catch(MissingResourceException ex) {
// ignore
}
if (datasetField.getTitle() != null) {
cedarTemplate.addProperty("skos:prefLabel", datasetField.getTitle());
try {
cedarTemplate.addProperty("hunLabel", BundleUtil.getStringFromPropertyFile("datasetfieldtype." + propName + ".title", datasetField.getmetadatablock_id(), hunLocale));
// We need dot notation to access prop translation
cedarTemplate.addProperty("hunLabel", BundleUtil.getStringFromPropertyFile("datasetfieldtype." + propName.replace(":", ".") + ".title", datasetField.getmetadatablock_id(), hunLocale));
}
catch (MissingResourceException ex) {
// ignore
Expand Down

0 comments on commit eff2298

Please sign in to comment.