Skip to content

Commit

Permalink
Refactor i18N fix encoding bug 🐛
Browse files Browse the repository at this point in the history
  • Loading branch information
ndegwamartin committed Jul 12, 2024
1 parent 956ce22 commit 7cff3fe
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 36 deletions.
2 changes: 1 addition & 1 deletion efsity/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ repositories {

group = "org.smartregister"

version = "2.3.6-SNAPSHOT"
version = "2.3.7-SNAPSHOT"

description = "fhircore-tooling (efsity)"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
import com.google.gson.JsonParser;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
Expand Down Expand Up @@ -105,13 +103,9 @@ public class PublishFhirResourcesCommand implements Runnable {
public void run() {
long start = System.currentTimeMillis();
if (propertiesFile != null && !propertiesFile.isBlank()) {
try (InputStream inputProperties = new FileInputStream(propertiesFile)) {
Properties properties = new Properties();
properties.load(inputProperties);
setProperties(properties);
} catch (IOException e) {
throw new RuntimeException(e);
}

Properties properties = FctUtils.readPropertiesFile(propertiesFile);
setProperties(properties);
}
try {
if (compositionFilePath != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ private static void mergeContent(
objectMapper.readTree(Files.newBufferedReader(inputFilePath, StandardCharsets.UTF_8));

// Load the translation properties
Properties translationProperties = getPropertiesFile(translationFile);
Properties translationProperties = FctUtils.readPropertiesFile(translationFile);

// Traverse and update the JSON structure
JsonNode updatedJson = updateJson(rootNode, translationProperties, locale, targetFields);
Expand All @@ -235,20 +235,6 @@ private static void mergeContent(
String.format("Merged JSON saved to \u001b[36m%s\u001b[0m", inputFilePath.toString()));
}

public static Properties getPropertiesFile(String propertiesFilePath) throws IOException {
Properties properties = new Properties();
// Read existing properties file, if it exists
if (Files.exists(Paths.get(propertiesFilePath))) {
try (FileInputStream fileInputStream = new FileInputStream(propertiesFilePath);
InputStreamReader reader =
new InputStreamReader(fileInputStream, StandardCharsets.UTF_8)) {

properties.load(reader);
}
}
return properties;
}

private static JsonNode updateJson(
JsonNode node, Properties translationProperties, String locale, Set<String> targetFields)
throws NoSuchAlgorithmException {
Expand Down Expand Up @@ -416,7 +402,7 @@ private static void extractContent(
throw new RuntimeException("Provide a valid `resourceFile` directory or file.");
}

Properties existingProperties = getPropertiesFile(propertiesFilePath.toString());
Properties existingProperties = FctUtils.readPropertiesFile(propertiesFilePath.toString());

// Merge existing properties with new properties
existingProperties.putAll(textToHash);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -15,6 +13,7 @@
import java.util.Properties;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.smartregister.util.FctUtils;

public class TranslateCommandTest {

Expand Down Expand Up @@ -55,14 +54,11 @@ public void testRunExtract() throws IOException {

assertDoesNotThrow(() -> translateCommand.run());

Properties existingProperties = new Properties();
InputStream defaultPropertiesInput = new FileInputStream(defaultPropertiesPath.toFile());
existingProperties.load(defaultPropertiesInput);
Properties existingProperties =
FctUtils.readPropertiesFile(defaultPropertiesPath.toFile().getAbsolutePath());

Properties newProperties = new Properties();
InputStream tempDefaultPropertiesInput =
new FileInputStream(tempDefaultPropertiesPath.toFile());
newProperties.load(tempDefaultPropertiesInput);
Properties newProperties =
FctUtils.readPropertiesFile(tempDefaultPropertiesPath.toFile().getAbsolutePath());

// Compare the contents of the two files
assertEquals(existingProperties, newProperties, "File contents are similar.");
Expand Down
2 changes: 1 addition & 1 deletion efsity/src/test/resources/strings_fr.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#Thu Nov 02 09:51:17 EAT 2023
b989b9027fad22ec89690367614e2da6=Raison de l?examen abdominal
f89de0a4cd9317a2987dfc2546e2da5e=Examen abdominal effectué
f89de0a4cd9317a2987dfc2546e2da5e=Examen abdominal effectué
bafd7322c6e97d25b6299b5d6fe8920b=Non
93cba07454f06a4a960172bbd6e2a435=Oui

0 comments on commit 7cff3fe

Please sign in to comment.