From eff2298d9d84299ad0a7480fae9c18acde9c51f1 Mon Sep 17 00:00:00 2001 From: "Balazs E. Pataki" Date: Wed, 18 Oct 2023 19:02:23 +0200 Subject: [PATCH] Fix generation of lang bundle after mdb-cedar sync 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 --- .../iq/dataverse/arp/ArpServiceBean.java | 56 ++++++++++++------- .../iq/dataverse/arp/TsvToCedarTemplate.java | 6 +- 2 files changed, 40 insertions(+), 22 deletions(-) diff --git a/src/main/java/edu/harvard/iq/dataverse/arp/ArpServiceBean.java b/src/main/java/edu/harvard/iq/dataverse/arp/ArpServiceBean.java index 2c302cad756..7b3325eb8a8 100644 --- a/src/main/java/edu/harvard/iq/dataverse/arp/ArpServiceBean.java +++ b/src/main/java/edu/harvard/iq/dataverse/arp/ArpServiceBean.java @@ -956,17 +956,17 @@ public void updateMetadataBlock(String dvIdtf, String metadataBlockName) throws } - public static ArrayList collectHunTranslations(String cedarTemplate, String parentPath, ArrayList hunTranslations) { + public static Map collectHunTranslations(String cedarTemplate, String parentPath, Map 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()); } } @@ -982,31 +982,28 @@ public static ArrayList 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! @@ -1024,14 +1021,14 @@ public static ArrayList 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); } @@ -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 hunTranslations = collectHunTranslations(templateJson, "/properties", new ArrayList<>()); + ResourceBundle resourceBundle = BundleUtil.getResourceBundle(metadataBlockName, Locale.forLanguageTag("hu")); + + // Load with current translations + Map hunTranslations = new TreeMap<>(); + Enumeration 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 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/ @@ -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); } } diff --git a/src/main/java/edu/harvard/iq/dataverse/arp/TsvToCedarTemplate.java b/src/main/java/edu/harvard/iq/dataverse/arp/TsvToCedarTemplate.java index 766f1239cde..495bf9e008c 100644 --- a/src/main/java/edu/harvard/iq/dataverse/arp/TsvToCedarTemplate.java +++ b/src/main/java/edu/harvard/iq/dataverse/arp/TsvToCedarTemplate.java @@ -338,7 +338,8 @@ 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 @@ -346,7 +347,8 @@ private void processCommonFields(JsonObject cedarTemplate, DataverseDatasetField 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