From 49dc519b8ff9b4b438d541d98f5c574521ad4451 Mon Sep 17 00:00:00 2001 From: Vincent Karuri Date: Thu, 20 Feb 2020 14:09:11 +0300 Subject: [PATCH 01/20] Add util that replaces mls placeholders with a literal string --- .../jsonwizard/utils/NativeFormLangUtils.java | 15 +++++++++++++++ .../jsonwizard/utils/NativeFomLangUtilsTest.java | 16 ++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/NativeFomLangUtilsTest.java diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/NativeFormLangUtils.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/NativeFormLangUtils.java index ac7bc8c38..d43cfc450 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/NativeFormLangUtils.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/NativeFormLangUtils.java @@ -7,6 +7,9 @@ import android.util.Log; import java.util.Locale; +import java.util.ResourceBundle; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class NativeFormLangUtils { @@ -31,4 +34,16 @@ public static Context setAppLocale(Context ctx, String language) { return context; } + + public static String getTranslatedJSONForm(String jsonForm) { + StringBuffer stringBuffer = new StringBuffer(); + Pattern placeholderPattern = Pattern.compile("\\{\\{([a-zA-Z_0-9]+)\\}\\}"); + Matcher matcher = placeholderPattern.matcher(jsonForm); + while (matcher.find()) { + matcher.appendReplacement(stringBuffer, "$1"); + } + matcher.appendTail(stringBuffer); + + return stringBuffer.toString(); + } } diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/NativeFomLangUtilsTest.java b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/NativeFomLangUtilsTest.java new file mode 100644 index 000000000..a321f4e7d --- /dev/null +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/NativeFomLangUtilsTest.java @@ -0,0 +1,16 @@ +package com.vijay.jsonwizard.utils; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * Created by Vincent Karuri on 20/02/2020 + */ +public class NativeFomLangUtilsTest { + + @Test + public void testStringInterpolation() { + assertEquals("hello_world_str, another_string,yet_another_string", NativeFormLangUtils.getTranslatedJSONForm("{{hello_world_str}}, {{another_string}},{{yet_another_string}}")); + } +} From 8ecce94a7946af0658a7cb9bd0daa36cc420e837 Mon Sep 17 00:00:00 2001 From: Vincent Karuri Date: Thu, 20 Feb 2020 16:26:24 +0300 Subject: [PATCH 02/20] Add mls test property and json files --- .../com/vijay/jsonwizard/utils/strings_en | 16 ++ .../com/vijay/jsonwizard/utils/strings_id | 16 ++ .../jsonwizard/utils/test_translation_en | 200 ++++++++++++++++++ .../jsonwizard/utils/test_translation_id | 179 ++++++++++++++++ .../utils/test_translation_interpolated | 186 ++++++++++++++++ 5 files changed, 597 insertions(+) create mode 100644 android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/strings_en create mode 100644 android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/strings_id create mode 100644 android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/test_translation_en create mode 100644 android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/test_translation_id create mode 100644 android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/test_translation_interpolated diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/strings_en b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/strings_en new file mode 100644 index 000000000..27a27a2a2 --- /dev/null +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/strings_en @@ -0,0 +1,16 @@ +step1.title = "New client record" +step1.previous_label = "SAVE AND EXIT" +step1.submit_label = "SAVE" +step1.patient_name_label.text = "Name" +step1.sex.label = "Gender" +step1.sex.options.Female.text = "Female" +step1.sex.options.Male.text = "Male" +step1.sex.v_required.err = "Please specify gender" +step1.patient_dob.hint = "Patient's date of birth" +step1.patient_dob.duration.label = "Age" +step1.patient_occupation_label.text = "Occupation" +step1.been_treated.label = "Has been treated for malaria in the past 3 months?" +step1.been_treated.options.Yes.text = "Yes" +step1.been_treated.options.No.text = "No" +step1.been_treated.options.not_answered.text = "Not Answered" +step1.been_treated.v_required.err = "Has the patient been treated for malaria in the past 3 months?" \ No newline at end of file diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/strings_id b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/strings_id new file mode 100644 index 000000000..98e42154a --- /dev/null +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/strings_id @@ -0,0 +1,16 @@ +step1.title = "Data Pasien Baru" +step1.previous_label = "SIMPAN DAN KELUAR" +step1.submit_label = "SIMPAN" +step1.patient_name_label.text = "Nama" +step1.sex.label = "Jenis kelamin pasien" +step1.sex.options.Female.text = "Perempuan" +step1.sex.options.Male.text = "Laki-laki" +step1.sex.v_required.err = "Silakan sebutkan jenis kelamin" +step1.patient_dob.hint = "Tanggal lahir pasien" +step1.patient_dob.duration.label = "Usia" +step1.patient_occupation_label.text = "Pekerjaan pasien" +step1.been_treated.label = "Pernah dirawat karena Malaria dalam 3 bulan terakhir?" +step1.been_treated.options.Yes.text = "Ya" +step1.been_treated.options.No.text = "Tidak" +step1.been_treated.options.not_answered.text = "Tidak dijawab" +step1.been_treated.v_required.err = "Pernahkah pasien dirawat karena malaria dalam 3 bulan terakhir?" \ No newline at end of file diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/test_translation_en b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/test_translation_en new file mode 100644 index 000000000..a3776e834 --- /dev/null +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/test_translation_en @@ -0,0 +1,200 @@ +{ + "count": "1", + "encounter_type": "patient_registration", + "entity_id": "", + "metadata": { + "start": { + "openmrs_entity_parent": "", + "openmrs_entity": "concept", + "openmrs_data_type": "start", + "openmrs_entity_id": "163137AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + }, + "end": { + "openmrs_entity_parent": "", + "openmrs_entity": "concept", + "openmrs_data_type": "end", + "openmrs_entity_id": "163138AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + }, + "today": { + "openmrs_entity_parent": "", + "openmrs_entity": "encounter", + "openmrs_entity_id": "encounter_date" + }, + "deviceid": { + "openmrs_entity_parent": "", + "openmrs_entity": "concept", + "openmrs_data_type": "deviceid", + "openmrs_entity_id": "163149AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + }, + "subscriberid": { + "openmrs_entity_parent": "", + "openmrs_entity": "concept", + "openmrs_data_type": "subscriberid", + "openmrs_entity_id": "163150AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + }, + "simserial": { + "openmrs_entity_parent": "", + "openmrs_entity": "concept", + "openmrs_data_type": "simserial", + "openmrs_entity_id": "163151AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + }, + "phonenumber": { + "openmrs_entity_parent": "", + "openmrs_entity": "concept", + "openmrs_data_type": "phonenumber", + "openmrs_entity_id": "163152AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + }, + "encounter_location": "" + }, + "step1": { + "title": "New client record", + "display_back_button": "true", + "previous_label": "SAVE AND EXIT", + "bottom_navigation": "true", + "bottom_navigation_orientation": "vertical", + "next_type": "submit", + "submit_label": "SAVE", + "next_form": "json.form/patient-registration-form.json", + "fields": [ + { + "key": "patient_name_label", + "type": "label", + "text": "Name", + "text_color": "#000000" + }, + { + "key": "patient_name", + "openmrs_entity_parent": "", + "openmrs_entity": "person", + "openmrs_entity_id": "first_name", + "type": "edit_text", + "edit_type": "name" + }, + { + "key": "patient_id_label", + "type": "label", + "text": "Patient ID", + "text_color": "#000000" + }, + { + "key": "patient_id", + "openmrs_entity_parent": "", + "openmrs_entity": "", + "openmrs_entity_id": "", + "type": "edit_text", + "v_required": { + "value": "true", + "err": "Please enter patient ID" + } + }, + { + "key": "sex", + "openmrs_entity_parent": "", + "openmrs_entity": "person", + "openmrs_entity_id": "gender", + "type": "native_radio", + "label": "Gender", + "options": [ + { + "key": "Female", + "text": "Female" + }, + { + "key": "Male", + "text": "Male" + } + ], + "v_required": { + "value": "true", + "err": "Please specify gender" + } + }, + { + "key": "patient_dob", + "openmrs_entity_parent": "", + "openmrs_entity": "person_attribute", + "openmrs_entity_id": "birthdate", + "type": "date_picker", + "hint": "Patient's date of birth", + "expanded": false, + "duration": { + "label": "Age" + }, + "min_date": "today-100y", + "max_date": "today" + }, + { + "key": "age", + "openmrs_entity_parent": "", + "openmrs_entity": "", + "openmrs_entity_id": "", + "type": "hidden", + "value": "" + }, + { + "key": "patient_occupation_label", + "type": "label", + "text": "Occupation", + "text_color": "#000000" + }, + { + "key": "patient_occupation", + "openmrs_entity_parent": "", + "openmrs_entity": "", + "openmrs_entity_id": "", + "type": "edit_text", + "v_required": { + "value": "false", + "err": "" + } + }, + { + "key": "skip_diagnostic_questions", + "openmrs_entity_parent": "", + "openmrs_entity": "", + "openmrs_entity_id": "", + "type": "check_box", + "options": [ + { + "key": "skip_questions", + "text": "Skip diagnostic questions", + "value": "true" + } + ] + }, + { + "key": "been_treated", + "openmrs_entity_parent": "", + "openmrs_entity": "", + "openmrs_entity_id": "", + "type": "native_radio", + "label": "Has been treated for malaria in the past 3 months?", + "options": [ + { + "key": "Yes", + "text": "Yes" + }, + { + "key": "No", + "text": "No" + }, + { + "key": "not_answered", + "text": "Not Answered" + } + ], + "value": "not_answered", + "v_required": { + "value": "true", + "err": "Has the patient been treated for malaria in the past 3 months?" + }, + "relevance": { + "step1:skip_diagnostic_questions": { + "type": "string", + "ex": "equalTo(., \"false\")" + } + } + } + ] + } +} \ No newline at end of file diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/test_translation_id b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/test_translation_id new file mode 100644 index 000000000..02a168599 --- /dev/null +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/test_translation_id @@ -0,0 +1,179 @@ +{ + "count": "1", + "encounter_type": "patient_registration", + "entity_id": "", + "metadata": { + "start": { + "openmrs_entity_parent": "", + "openmrs_entity": "concept", + "openmrs_data_type": "start", + "openmrs_entity_id": "163137AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + }, + "end": { + "openmrs_entity_parent": "", + "openmrs_entity": "concept", + "openmrs_data_type": "end", + "openmrs_entity_id": "163138AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + }, + "today": { + "openmrs_entity_parent": "", + "openmrs_entity": "encounter", + "openmrs_entity_id": "encounter_date" + }, + "deviceid": { + "openmrs_entity_parent": "", + "openmrs_entity": "concept", + "openmrs_data_type": "deviceid", + "openmrs_entity_id": "163149AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + }, + "subscriberid": { + "openmrs_entity_parent": "", + "openmrs_entity": "concept", + "openmrs_data_type": "subscriberid", + "openmrs_entity_id": "163150AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + }, + "simserial": { + "openmrs_entity_parent": "", + "openmrs_entity": "concept", + "openmrs_data_type": "simserial", + "openmrs_entity_id": "163151AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + }, + "phonenumber": { + "openmrs_entity_parent": "", + "openmrs_entity": "concept", + "openmrs_data_type": "phonenumber", + "openmrs_entity_id": "163152AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + }, + "encounter_location": "" + }, + "step1": { + "title": "Data Pasien Baru", + "display_back_button": "true", + "previous_label": "SIMPAN DAN KELUAR", + "bottom_navigation": "true", + "bottom_navigation_orientation": "vertical", + "next_type": "submit", + "submit_label": "SIMPAN", + "next_form": "json.form/patient-registration-form.json", + "fields": [ + { + "key": "patient_name_label", + "type": "label", + "text": "Nama", + "text_color": "#000000" + }, + { + "key": "patient_name", + "openmrs_entity_parent": "", + "openmrs_entity": "person", + "openmrs_entity_id": "first_name", + "type": "edit_text", + "edit_type": "name" + }, + { + "key": "patient_id_label", + "type": "label", + "text": "Patient ID", + "text_color": "#000000" + }, + { + "key": "patient_id", + "openmrs_entity_parent": "", + "openmrs_entity": "", + "openmrs_entity_id": "", + "type": "edit_text", + "v_required": { + "value": "true", + "err": "Please enter patient ID" + } + }, + { + "key": "sex", + "openmrs_entity_parent": "", + "openmrs_entity": "person", + "openmrs_entity_id": "gender", + "type": "native_radio", + "label": "Jenis kelamin pasien", + "options": [ + { + "key": "Female", + "text": "Perempuan" + }, + { + "key": "Male", + "text": "Laki-laki" + } + ], + "v_required": { + "value": "true", + "err": "Silakan sebutkan jenis kelamin" + } + }, + { + "key": "patient_dob", + "openmrs_entity_parent": "", + "openmrs_entity": "person_attribute", + "openmrs_entity_id": "birthdate", + "type": "date_picker", + "hint": "Tanggal lahir pasien", + "expanded": false, + "duration": { + "label": "Usia" + }, + "min_date": "today-100y", + "max_date": "today" + }, + { + "key": "age", + "openmrs_entity_parent": "", + "openmrs_entity": "", + "openmrs_entity_id": "", + "type": "hidden", + "value": "" + }, + { + "key": "patient_occupation_label", + "type": "label", + "text": "Pekerjaan pasien", + "text_color": "#000000" + }, + { + "key": "patient_occupation", + "openmrs_entity_parent": "", + "openmrs_entity": "", + "openmrs_entity_id": "", + "type": "edit_text", + "v_required": { + "value": "false", + "err": "" + } + }, + { + "key": "been_treated", + "openmrs_entity_parent": "", + "openmrs_entity": "", + "openmrs_entity_id": "", + "type": "native_radio", + "label": "Pernah dirawat karena Malaria dalam 3 bulan terakhir?", + "options": [ + { + "key": "Yes", + "text": "Ya" + }, + { + "key": "No", + "text": "Tidak" + }, + { + "key": "Not Answered", + "text": "Tidak dijawab" + } + ], + "v_required": { + "value": "true", + "err": "Pernahkah pasien dirawat karena malaria dalam 3 bulan terakhir?" + } + } + ] + } +} diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/test_translation_interpolated b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/test_translation_interpolated new file mode 100644 index 000000000..87a807a34 --- /dev/null +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/test_translation_interpolated @@ -0,0 +1,186 @@ +{ + "count": "1", + "encounter_type": "patient_registration", + "entity_id": "", + "metadata": { + "start": { + "openmrs_entity_parent": "", + "openmrs_entity": "concept", + "openmrs_data_type": "start", + "openmrs_entity_id": "163137AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + }, + "end": { + "openmrs_entity_parent": "", + "openmrs_entity": "concept", + "openmrs_data_type": "end", + "openmrs_entity_id": "163138AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + }, + "today": { + "openmrs_entity_parent": "", + "openmrs_entity": "encounter", + "openmrs_entity_id": "encounter_date" + }, + "deviceid": { + "openmrs_entity_parent": "", + "openmrs_entity": "concept", + "openmrs_data_type": "deviceid", + "openmrs_entity_id": "163149AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + }, + "subscriberid": { + "openmrs_entity_parent": "", + "openmrs_entity": "concept", + "openmrs_data_type": "subscriberid", + "openmrs_entity_id": "163150AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + }, + "simserial": { + "openmrs_entity_parent": "", + "openmrs_entity": "concept", + "openmrs_data_type": "simserial", + "openmrs_entity_id": "163151AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + }, + "phonenumber": { + "openmrs_entity_parent": "", + "openmrs_entity": "concept", + "openmrs_data_type": "phonenumber", + "openmrs_entity_id": "163152AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + }, + "encounter_location": "" + }, + "step1": { + "title": "{{step1.title}}", + "display_back_button": "true", + "previous_label": "{{step1.previous_label}}", + "bottom_navigation": "true", + "bottom_navigation_orientation": "vertical", + "next_type": "submit", + "submit_label": "{{step1.submit_label}}", + "next_form": "json.form/patient-registration-form.json", + "fields": [ + { + "key": "patient_name_label", + "type": "label", + "text": "{{step1.patient_name_label.text}}", + "text_color": "#000000" + }, + { + "key": "patient_name", + "openmrs_entity_parent": "", + "openmrs_entity": "person", + "openmrs_entity_id": "first_name", + "type": "edit_text", + "edit_type": "name" + }, + { + "key": "patient_id_label", + "type": "label", + "text": "Patient ID", + "text_color": "#000000" + }, + { + "key": "patient_id", + "openmrs_entity_parent": "", + "openmrs_entity": "", + "openmrs_entity_id": "", + "type": "edit_text", + "v_required": { + "value": "true", + "err": "Please enter patient ID" + } + }, + { + "key": "sex", + "openmrs_entity_parent": "", + "openmrs_entity": "person", + "openmrs_entity_id": "gender", + "type": "native_radio", + "label": "{{step1.sex.label}}", + "options": [ + { + "key": "Female", + "text": "{{step1.sex.options.Female.text}}" + }, + { + "key": "Male", + "text": "{{step1.sex.options.Male.text}}" + } + ], + "v_required": { + "value": "true", + "err": "{{step1.sex.v_required.err}}" + } + }, + { + "key": "patient_dob", + "openmrs_entity_parent": "", + "openmrs_entity": "person_attribute", + "openmrs_entity_id": "birthdate", + "type": "date_picker", + "hint": "{{step1.patient_dob.hint}}", + "expanded": false, + "duration": { + "label": "{{step1.patient_dob.duration.label}}" + }, + "min_date": "today-100y", + "max_date": "today" + }, + { + "key": "age", + "openmrs_entity_parent": "", + "openmrs_entity": "", + "openmrs_entity_id": "", + "type": "hidden", + "value": "" + }, + { + "key": "patient_occupation_label", + "type": "label", + "text": "{{step1.patient_occupation_label.text}}", + "text_color": "#000000" + }, + { + "key": "patient_occupation", + "openmrs_entity_parent": "", + "openmrs_entity": "", + "openmrs_entity_id": "", + "type": "edit_text", + "v_required": { + "value": "false", + "err": "" + } + }, + { + "key": "been_treated", + "openmrs_entity_parent": "", + "openmrs_entity": "", + "openmrs_entity_id": "", + "type": "native_radio", + "label": "{{step1.been_treated.label}}", + "options": [ + { + "key": "Yes", + "text": "{{step1.been_treated.options.Yes.text}}" + }, + { + "key": "No", + "text": "{{step1.been_treated.options.No.text}}" + }, + { + "key": "not_answered", + "text": "{{step1.been_treated.options.not_answered.text}}" + } + ], + "value": "not_answered", + "v_required": { + "value": "true", + "err": "{{step1.been_treated.v_required.err}}" + }, + "relevance": { + "step1:skip_diagnostic_questions": { + "type": "string", + "ex": "equalTo(., \"false\")" + } + } + } + ] + } +} \ No newline at end of file From 354972b9356ad5a2bd49f248382bef140b076aed Mon Sep 17 00:00:00 2001 From: Vincent Karuri Date: Mon, 24 Feb 2020 13:18:27 +0300 Subject: [PATCH 03/20] Move test files to test resources dir and add resource bundle property fetching logic --- .../vijay/jsonwizard/utils/NativeFormLangUtils.java | 7 +++++-- .../jsonwizard/utils/NativeFomLangUtilsTest.java | 13 +++++++++++-- .../strings_en_US.properties} | 0 .../strings_id => resources/strings_in.properties} | 0 .../utils => resources}/test_translation_en | 0 .../test_translation_in} | 0 .../test_translation_interpolated | 0 7 files changed, 16 insertions(+), 4 deletions(-) rename android-json-form-wizard/src/test/{java/com/vijay/jsonwizard/utils/strings_en => resources/strings_en_US.properties} (100%) rename android-json-form-wizard/src/test/{java/com/vijay/jsonwizard/utils/strings_id => resources/strings_in.properties} (100%) rename android-json-form-wizard/src/test/{java/com/vijay/jsonwizard/utils => resources}/test_translation_en (100%) rename android-json-form-wizard/src/test/{java/com/vijay/jsonwizard/utils/test_translation_id => resources/test_translation_in} (100%) rename android-json-form-wizard/src/test/{java/com/vijay/jsonwizard/utils => resources}/test_translation_interpolated (100%) diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/NativeFormLangUtils.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/NativeFormLangUtils.java index d43cfc450..43884dcde 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/NativeFormLangUtils.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/NativeFormLangUtils.java @@ -35,12 +35,15 @@ public static Context setAppLocale(Context ctx, String language) { return context; } - public static String getTranslatedJSONForm(String jsonForm) { + public static String getTranslatedJSONForm(String jsonForm, String filePath) { + Locale.setDefault(new Locale("id")); + ResourceBundle mlsResourceBundle = ResourceBundle.getBundle(filePath); + StringBuffer stringBuffer = new StringBuffer(); Pattern placeholderPattern = Pattern.compile("\\{\\{([a-zA-Z_0-9]+)\\}\\}"); Matcher matcher = placeholderPattern.matcher(jsonForm); while (matcher.find()) { - matcher.appendReplacement(stringBuffer, "$1"); + matcher.appendReplacement(stringBuffer, mlsResourceBundle.getString(matcher.group(1))); } matcher.appendTail(stringBuffer); diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/NativeFomLangUtilsTest.java b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/NativeFomLangUtilsTest.java index a321f4e7d..4ba330218 100644 --- a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/NativeFomLangUtilsTest.java +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/NativeFomLangUtilsTest.java @@ -2,6 +2,9 @@ import org.junit.Test; +import java.io.IOException; +import java.io.InputStream; + import static org.junit.Assert.assertEquals; /** @@ -10,7 +13,13 @@ public class NativeFomLangUtilsTest { @Test - public void testStringInterpolation() { - assertEquals("hello_world_str, another_string,yet_another_string", NativeFormLangUtils.getTranslatedJSONForm("{{hello_world_str}}, {{another_string}},{{yet_another_string}}")); + public void testStringInterpolation() throws IOException { + String interpolatedJsonForm = convertStreamToString(getClass().getClassLoader().getResourceAsStream("test_translation_interpolated")); + assertEquals("hello_world_str, another_string,yet_another_string", NativeFormLangUtils.getTranslatedJSONForm(interpolatedJsonForm, "strings")); + } + + private String convertStreamToString(InputStream is) { + java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A"); + return s.hasNext() ? s.next() : ""; } } diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/strings_en b/android-json-form-wizard/src/test/resources/strings_en_US.properties similarity index 100% rename from android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/strings_en rename to android-json-form-wizard/src/test/resources/strings_en_US.properties diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/strings_id b/android-json-form-wizard/src/test/resources/strings_in.properties similarity index 100% rename from android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/strings_id rename to android-json-form-wizard/src/test/resources/strings_in.properties diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/test_translation_en b/android-json-form-wizard/src/test/resources/test_translation_en similarity index 100% rename from android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/test_translation_en rename to android-json-form-wizard/src/test/resources/test_translation_en diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/test_translation_id b/android-json-form-wizard/src/test/resources/test_translation_in similarity index 100% rename from android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/test_translation_id rename to android-json-form-wizard/src/test/resources/test_translation_in diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/test_translation_interpolated b/android-json-form-wizard/src/test/resources/test_translation_interpolated similarity index 100% rename from android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/test_translation_interpolated rename to android-json-form-wizard/src/test/resources/test_translation_interpolated From c2293035ca21754111581e6f04eeba7fa29eba12 Mon Sep 17 00:00:00 2001 From: Vincent Karuri Date: Mon, 24 Feb 2020 14:38:21 +0300 Subject: [PATCH 04/20] Modify regex to include enclosing quotation marks around interpolated string --- .../jsonwizard/utils/NativeFormLangUtils.java | 3 +-- .../jsonwizard/utils/NativeFomLangUtilsTest.java | 14 ++++++++++---- .../src/test/resources/test_translation_in | 2 +- .../test/resources/test_translation_interpolated | 9 +-------- 4 files changed, 13 insertions(+), 15 deletions(-) diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/NativeFormLangUtils.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/NativeFormLangUtils.java index 43884dcde..3a2931f1f 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/NativeFormLangUtils.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/NativeFormLangUtils.java @@ -36,11 +36,10 @@ public static Context setAppLocale(Context ctx, String language) { } public static String getTranslatedJSONForm(String jsonForm, String filePath) { - Locale.setDefault(new Locale("id")); ResourceBundle mlsResourceBundle = ResourceBundle.getBundle(filePath); StringBuffer stringBuffer = new StringBuffer(); - Pattern placeholderPattern = Pattern.compile("\\{\\{([a-zA-Z_0-9]+)\\}\\}"); + Pattern placeholderPattern = Pattern.compile("\"\\{\\{([a-zA-Z_0-9\\.]+)\\}\\}\""); Matcher matcher = placeholderPattern.matcher(jsonForm); while (matcher.find()) { matcher.appendReplacement(stringBuffer, mlsResourceBundle.getString(matcher.group(1))); diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/NativeFomLangUtilsTest.java b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/NativeFomLangUtilsTest.java index 4ba330218..b8b93a27a 100644 --- a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/NativeFomLangUtilsTest.java +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/NativeFomLangUtilsTest.java @@ -2,8 +2,8 @@ import org.junit.Test; -import java.io.IOException; import java.io.InputStream; +import java.util.Locale; import static org.junit.Assert.assertEquals; @@ -13,9 +13,15 @@ public class NativeFomLangUtilsTest { @Test - public void testStringInterpolation() throws IOException { - String interpolatedJsonForm = convertStreamToString(getClass().getClassLoader().getResourceAsStream("test_translation_interpolated")); - assertEquals("hello_world_str, another_string,yet_another_string", NativeFormLangUtils.getTranslatedJSONForm(interpolatedJsonForm, "strings")); + public void testStringInterpolation() { + Locale.setDefault(new Locale("id")); + String indonesiaJsonForm = getFileContentsAsString("test_translation_in"); + String interpolatedJsonForm = getFileContentsAsString("test_translation_interpolated"); + assertEquals(indonesiaJsonForm, NativeFormLangUtils.getTranslatedJSONForm(interpolatedJsonForm, "strings")); + } + + private String getFileContentsAsString(String filePath) { + return convertStreamToString(getClass().getClassLoader().getResourceAsStream(filePath)); } private String convertStreamToString(InputStream is) { diff --git a/android-json-form-wizard/src/test/resources/test_translation_in b/android-json-form-wizard/src/test/resources/test_translation_in index 02a168599..bc3e27b9d 100644 --- a/android-json-form-wizard/src/test/resources/test_translation_in +++ b/android-json-form-wizard/src/test/resources/test_translation_in @@ -176,4 +176,4 @@ } ] } -} +} \ No newline at end of file diff --git a/android-json-form-wizard/src/test/resources/test_translation_interpolated b/android-json-form-wizard/src/test/resources/test_translation_interpolated index 87a807a34..85560e552 100644 --- a/android-json-form-wizard/src/test/resources/test_translation_interpolated +++ b/android-json-form-wizard/src/test/resources/test_translation_interpolated @@ -165,20 +165,13 @@ "text": "{{step1.been_treated.options.No.text}}" }, { - "key": "not_answered", + "key": "Not Answered", "text": "{{step1.been_treated.options.not_answered.text}}" } ], - "value": "not_answered", "v_required": { "value": "true", "err": "{{step1.been_treated.v_required.err}}" - }, - "relevance": { - "step1:skip_diagnostic_questions": { - "type": "string", - "ex": "equalTo(., \"false\")" - } } } ] From 4ce15ca9038a6cc3c9aeebe4a64ce6a099139731 Mon Sep 17 00:00:00 2001 From: Vincent Karuri Date: Mon, 24 Feb 2020 14:48:52 +0300 Subject: [PATCH 05/20] Add test for en_US translation --- .../utils/NativeFomLangUtilsTest.java | 8 +++++-- ..._translation_en => test_translation_en_US} | 23 +------------------ 2 files changed, 7 insertions(+), 24 deletions(-) rename android-json-form-wizard/src/test/resources/{test_translation_en => test_translation_en_US} (88%) diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/NativeFomLangUtilsTest.java b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/NativeFomLangUtilsTest.java index b8b93a27a..cfb59c817 100644 --- a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/NativeFomLangUtilsTest.java +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/NativeFomLangUtilsTest.java @@ -15,9 +15,13 @@ public class NativeFomLangUtilsTest { @Test public void testStringInterpolation() { Locale.setDefault(new Locale("id")); - String indonesiaJsonForm = getFileContentsAsString("test_translation_in"); + String expectedJsonForm = getFileContentsAsString("test_translation_in"); String interpolatedJsonForm = getFileContentsAsString("test_translation_interpolated"); - assertEquals(indonesiaJsonForm, NativeFormLangUtils.getTranslatedJSONForm(interpolatedJsonForm, "strings")); + assertEquals(expectedJsonForm, NativeFormLangUtils.getTranslatedJSONForm(interpolatedJsonForm, "strings")); + + Locale.setDefault(new Locale("en", "US")); + expectedJsonForm = getFileContentsAsString("test_translation_en_US"); + assertEquals(expectedJsonForm, NativeFormLangUtils.getTranslatedJSONForm(interpolatedJsonForm, "strings")); } private String getFileContentsAsString(String filePath) { diff --git a/android-json-form-wizard/src/test/resources/test_translation_en b/android-json-form-wizard/src/test/resources/test_translation_en_US similarity index 88% rename from android-json-form-wizard/src/test/resources/test_translation_en rename to android-json-form-wizard/src/test/resources/test_translation_en_US index a3776e834..15b1bd95c 100644 --- a/android-json-form-wizard/src/test/resources/test_translation_en +++ b/android-json-form-wizard/src/test/resources/test_translation_en_US @@ -148,20 +148,6 @@ "err": "" } }, - { - "key": "skip_diagnostic_questions", - "openmrs_entity_parent": "", - "openmrs_entity": "", - "openmrs_entity_id": "", - "type": "check_box", - "options": [ - { - "key": "skip_questions", - "text": "Skip diagnostic questions", - "value": "true" - } - ] - }, { "key": "been_treated", "openmrs_entity_parent": "", @@ -179,20 +165,13 @@ "text": "No" }, { - "key": "not_answered", + "key": "Not Answered", "text": "Not Answered" } ], - "value": "not_answered", "v_required": { "value": "true", "err": "Has the patient been treated for malaria in the past 3 months?" - }, - "relevance": { - "step1:skip_diagnostic_questions": { - "type": "string", - "ex": "equalTo(., \"false\")" - } } } ] From ce458225b93ab1920fed43caae187a90321ae38f Mon Sep 17 00:00:00 2001 From: Vincent Karuri Date: Mon, 24 Feb 2020 14:52:04 +0300 Subject: [PATCH 06/20] Code clean-up --- .../com/vijay/jsonwizard/utils/NativeFormLangUtils.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/NativeFormLangUtils.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/NativeFormLangUtils.java index 3a2931f1f..99e694b98 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/NativeFormLangUtils.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/NativeFormLangUtils.java @@ -35,12 +35,12 @@ public static Context setAppLocale(Context ctx, String language) { return context; } - public static String getTranslatedJSONForm(String jsonForm, String filePath) { - ResourceBundle mlsResourceBundle = ResourceBundle.getBundle(filePath); + public static String getTranslatedJSONForm(String jsonForm, String fileName) { + ResourceBundle mlsResourceBundle = ResourceBundle.getBundle(fileName); StringBuffer stringBuffer = new StringBuffer(); - Pattern placeholderPattern = Pattern.compile("\"\\{\\{([a-zA-Z_0-9\\.]+)\\}\\}\""); - Matcher matcher = placeholderPattern.matcher(jsonForm); + Pattern interpolatedStringPattern = Pattern.compile("\"\\{\\{([a-zA-Z_0-9\\.]+)\\}\\}\""); + Matcher matcher = interpolatedStringPattern.matcher(jsonForm); while (matcher.find()) { matcher.appendReplacement(stringBuffer, mlsResourceBundle.getString(matcher.group(1))); } From 7e4736af9218c7efeb3d0dc64f2c3cd924475167 Mon Sep 17 00:00:00 2001 From: Vincent Karuri Date: Thu, 27 Feb 2020 12:24:28 +0300 Subject: [PATCH 07/20] Get the translations' property file name from the json form --- .../vijay/jsonwizard/utils/NativeFormLangUtils.java | 12 ++++++++++-- .../jsonwizard/utils/NativeFomLangUtilsTest.java | 4 ++-- .../src/test/resources/test_translation_en_US | 1 + .../src/test/resources/test_translation_in | 1 + .../src/test/resources/test_translation_interpolated | 1 + 5 files changed, 15 insertions(+), 4 deletions(-) diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/NativeFormLangUtils.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/NativeFormLangUtils.java index 99e694b98..813882ba2 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/NativeFormLangUtils.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/NativeFormLangUtils.java @@ -35,8 +35,8 @@ public static Context setAppLocale(Context ctx, String language) { return context; } - public static String getTranslatedJSONForm(String jsonForm, String fileName) { - ResourceBundle mlsResourceBundle = ResourceBundle.getBundle(fileName); + public static String getTranslatedJSONForm(String jsonForm) { + ResourceBundle mlsResourceBundle = ResourceBundle.getBundle(getPropertiesFileName(jsonForm)); StringBuffer stringBuffer = new StringBuffer(); Pattern interpolatedStringPattern = Pattern.compile("\"\\{\\{([a-zA-Z_0-9\\.]+)\\}\\}\""); @@ -48,4 +48,12 @@ public static String getTranslatedJSONForm(String jsonForm, String fileName) { return stringBuffer.toString(); } + + public static String getPropertiesFileName(String jsonForm) { + Pattern propertiesFileNamePattern = Pattern.compile("\"properties_file_name\": \"(strings)\""); + Matcher matcher = propertiesFileNamePattern.matcher(jsonForm); + matcher.find(); + String propertiesFileName = matcher.group(1); + return propertiesFileName; + } } diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/NativeFomLangUtilsTest.java b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/NativeFomLangUtilsTest.java index cfb59c817..9c417e4c0 100644 --- a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/NativeFomLangUtilsTest.java +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/NativeFomLangUtilsTest.java @@ -17,11 +17,11 @@ public void testStringInterpolation() { Locale.setDefault(new Locale("id")); String expectedJsonForm = getFileContentsAsString("test_translation_in"); String interpolatedJsonForm = getFileContentsAsString("test_translation_interpolated"); - assertEquals(expectedJsonForm, NativeFormLangUtils.getTranslatedJSONForm(interpolatedJsonForm, "strings")); + assertEquals(expectedJsonForm, NativeFormLangUtils.getTranslatedJSONForm(interpolatedJsonForm)); Locale.setDefault(new Locale("en", "US")); expectedJsonForm = getFileContentsAsString("test_translation_en_US"); - assertEquals(expectedJsonForm, NativeFormLangUtils.getTranslatedJSONForm(interpolatedJsonForm, "strings")); + assertEquals(expectedJsonForm, NativeFormLangUtils.getTranslatedJSONForm(interpolatedJsonForm)); } private String getFileContentsAsString(String filePath) { diff --git a/android-json-form-wizard/src/test/resources/test_translation_en_US b/android-json-form-wizard/src/test/resources/test_translation_en_US index 15b1bd95c..0ab4da551 100644 --- a/android-json-form-wizard/src/test/resources/test_translation_en_US +++ b/android-json-form-wizard/src/test/resources/test_translation_en_US @@ -2,6 +2,7 @@ "count": "1", "encounter_type": "patient_registration", "entity_id": "", + "properties_file_name": "strings", "metadata": { "start": { "openmrs_entity_parent": "", diff --git a/android-json-form-wizard/src/test/resources/test_translation_in b/android-json-form-wizard/src/test/resources/test_translation_in index bc3e27b9d..09c0af923 100644 --- a/android-json-form-wizard/src/test/resources/test_translation_in +++ b/android-json-form-wizard/src/test/resources/test_translation_in @@ -2,6 +2,7 @@ "count": "1", "encounter_type": "patient_registration", "entity_id": "", + "properties_file_name": "strings", "metadata": { "start": { "openmrs_entity_parent": "", diff --git a/android-json-form-wizard/src/test/resources/test_translation_interpolated b/android-json-form-wizard/src/test/resources/test_translation_interpolated index 85560e552..182c2bb2d 100644 --- a/android-json-form-wizard/src/test/resources/test_translation_interpolated +++ b/android-json-form-wizard/src/test/resources/test_translation_interpolated @@ -2,6 +2,7 @@ "count": "1", "encounter_type": "patient_registration", "entity_id": "", + "properties_file_name": "strings", "metadata": { "start": { "openmrs_entity_parent": "", From 7028f0f26b53df523a2ce29cf51747a2fd878627 Mon Sep 17 00:00:00 2001 From: Vincent Karuri Date: Thu, 27 Feb 2020 12:43:54 +0300 Subject: [PATCH 08/20] Enable translating native forms at launch --- .../activities/JsonFormBaseActivity.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/activities/JsonFormBaseActivity.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/activities/JsonFormBaseActivity.java index acf0dd9fa..9ef455613 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/activities/JsonFormBaseActivity.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/activities/JsonFormBaseActivity.java @@ -33,6 +33,8 @@ import java.util.List; import java.util.Map; +import static com.vijay.jsonwizard.utils.NativeFormLangUtils.getTranslatedJSONForm; + abstract class JsonFormBaseActivity extends MultiLanguageActivity implements OnFieldsInvalid { protected static final String TAG = JsonFormActivity.class.getSimpleName(); protected static final String JSON_STATE = "jsonState"; @@ -56,6 +58,8 @@ abstract class JsonFormBaseActivity extends MultiLanguageActivity implements OnF private Map invalidFields = new HashMap<>(); private boolean isPreviousPressed = false; + public static final String PERFORM_FORM_TRANSLATION = "perform_form_translation"; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -69,7 +73,7 @@ protected void onCreate(Bundle savedInstanceState) { lifeCycleListeners = new ArrayList<>(); isFormFragmentInitialized = false; if (savedInstanceState == null) { - init(getIntent().getStringExtra(JsonFormConstants.JSON_FORM_KEY.JSON)); + init(getForm()); initializeFormFragment(); onFormStart(); this.form = extractForm(getIntent().getSerializableExtra(JsonFormConstants.JSON_FORM_KEY.FORM)); @@ -82,6 +86,14 @@ protected void onCreate(Bundle savedInstanceState) { } } + private String getForm() { + String jsonForm = getIntent().getStringExtra(JsonFormConstants.JSON_FORM_KEY.JSON); + if (getIntent().getBooleanExtra(PERFORM_FORM_TRANSLATION, false)) { + jsonForm = getTranslatedJSONForm(jsonForm); + } + return jsonForm; + } + public void init(String json) { try { setmJSONObject(new JSONObject(json)); From 3a07d8e70868e08598bff0ce38efdc04e6785fb5 Mon Sep 17 00:00:00 2001 From: Vincent Karuri Date: Mon, 2 Mar 2020 11:49:57 +0300 Subject: [PATCH 09/20] Rename method --- .../com/vijay/jsonwizard/activities/JsonFormBaseActivity.java | 4 ++-- .../java/com/vijay/jsonwizard/utils/NativeFormLangUtils.java | 2 +- .../com/vijay/jsonwizard/utils/NativeFomLangUtilsTest.java | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/activities/JsonFormBaseActivity.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/activities/JsonFormBaseActivity.java index 9ef455613..a914d7077 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/activities/JsonFormBaseActivity.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/activities/JsonFormBaseActivity.java @@ -33,7 +33,7 @@ import java.util.List; import java.util.Map; -import static com.vijay.jsonwizard.utils.NativeFormLangUtils.getTranslatedJSONForm; +import static com.vijay.jsonwizard.utils.NativeFormLangUtils.getTranslatedString; abstract class JsonFormBaseActivity extends MultiLanguageActivity implements OnFieldsInvalid { protected static final String TAG = JsonFormActivity.class.getSimpleName(); @@ -89,7 +89,7 @@ protected void onCreate(Bundle savedInstanceState) { private String getForm() { String jsonForm = getIntent().getStringExtra(JsonFormConstants.JSON_FORM_KEY.JSON); if (getIntent().getBooleanExtra(PERFORM_FORM_TRANSLATION, false)) { - jsonForm = getTranslatedJSONForm(jsonForm); + jsonForm = getTranslatedString(jsonForm); } return jsonForm; } diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/NativeFormLangUtils.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/NativeFormLangUtils.java index 813882ba2..6be00548d 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/NativeFormLangUtils.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/NativeFormLangUtils.java @@ -35,7 +35,7 @@ public static Context setAppLocale(Context ctx, String language) { return context; } - public static String getTranslatedJSONForm(String jsonForm) { + public static String getTranslatedString(String jsonForm) { ResourceBundle mlsResourceBundle = ResourceBundle.getBundle(getPropertiesFileName(jsonForm)); StringBuffer stringBuffer = new StringBuffer(); diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/NativeFomLangUtilsTest.java b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/NativeFomLangUtilsTest.java index 9c417e4c0..275fe16ad 100644 --- a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/NativeFomLangUtilsTest.java +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/NativeFomLangUtilsTest.java @@ -17,11 +17,11 @@ public void testStringInterpolation() { Locale.setDefault(new Locale("id")); String expectedJsonForm = getFileContentsAsString("test_translation_in"); String interpolatedJsonForm = getFileContentsAsString("test_translation_interpolated"); - assertEquals(expectedJsonForm, NativeFormLangUtils.getTranslatedJSONForm(interpolatedJsonForm)); + assertEquals(expectedJsonForm, NativeFormLangUtils.getTranslatedString(interpolatedJsonForm)); Locale.setDefault(new Locale("en", "US")); expectedJsonForm = getFileContentsAsString("test_translation_en_US"); - assertEquals(expectedJsonForm, NativeFormLangUtils.getTranslatedJSONForm(interpolatedJsonForm)); + assertEquals(expectedJsonForm, NativeFormLangUtils.getTranslatedString(interpolatedJsonForm)); } private String getFileContentsAsString(String filePath) { From abbfba7e94d016e99781c898604d303d3ebf3572 Mon Sep 17 00:00:00 2001 From: Vincent Karuri Date: Mon, 2 Mar 2020 11:53:12 +0300 Subject: [PATCH 10/20] Add yaml file translation support --- .../com/vijay/jsonwizard/utils/Utils.java | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/Utils.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/Utils.java index 89310006b..e507c6fca 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/Utils.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/Utils.java @@ -38,9 +38,11 @@ import org.json.JSONObject; import org.yaml.snakeyaml.Yaml; +import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; import java.text.ParseException; import java.util.ArrayList; import java.util.Arrays; @@ -58,6 +60,7 @@ import timber.log.Timber; import static com.vijay.jsonwizard.constants.JsonFormConstants.KEY; +import static com.vijay.jsonwizard.utils.NativeFormLangUtils.getTranslatedString; public class Utils { private static ProgressDialog progressDialog; @@ -482,11 +485,10 @@ private static String cleanConditionString(String conditionStringRaw) { public static Iterable readYamlFile(String fileName, Context context) { Yaml yaml = new Yaml(); - InputStreamReader inputStreamReader; Iterable objectIterable = null; try { - inputStreamReader = new InputStreamReader(context.getAssets().open(fileName)); - objectIterable = yaml.loadAll(inputStreamReader); + String translatedYamlStr = getTranslatedString(getAssetFileAsString(fileName, context)); + objectIterable = yaml.loadAll(translatedYamlStr); } catch (IOException e) { Timber.e(e); } @@ -494,6 +496,20 @@ public static Iterable readYamlFile(String fileName, Context context) { return objectIterable; } + + public static String getAssetFileAsString(String fileName, Context context) throws IOException { + StringBuilder stringBuilder = new StringBuilder(); + InputStream inputStream = context.getAssets().open(fileName); + BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); + String str; + while ((str = bufferedReader.readLine()) != null) { + stringBuilder.append(str); + } + bufferedReader.close(); + + return stringBuilder.toString(); + } + public static void buildRulesWithUniqueId(JSONObject element, String uniqueId, String ruleType, WidgetArgs widgetArgs, Map>> rulesFileMap) throws JSONException { JSONObject rules = element.optJSONObject(ruleType); if (rules != null) { From bd98cf461da5d9d93bdc2fbf348c45f367f41ff5 Mon Sep 17 00:00:00 2001 From: Vincent Karuri Date: Mon, 2 Mar 2020 13:18:29 +0300 Subject: [PATCH 11/20] Rename form translation test files and modify interpolation regex to consider yaml syntax --- .../jsonwizard/utils/NativeFormLangUtils.java | 4 +-- .../utils/NativeFomLangUtilsTest.java | 32 ++++++++++++++++--- .../resources/form_strings_en_US.properties | 16 ++++++++++ .../test/resources/form_strings_in.properties | 16 ++++++++++ .../test/resources/strings_en_US.properties | 16 ---------- .../src/test/resources/strings_in.properties | 16 ---------- ...tion_en_US => test_form_translation_en_US} | 2 +- ...ranslation_in => test_form_translation_in} | 2 +- ...ted => test_form_translation_interpolated} | 2 +- 9 files changed, 65 insertions(+), 41 deletions(-) create mode 100644 android-json-form-wizard/src/test/resources/form_strings_en_US.properties create mode 100644 android-json-form-wizard/src/test/resources/form_strings_in.properties delete mode 100644 android-json-form-wizard/src/test/resources/strings_en_US.properties delete mode 100644 android-json-form-wizard/src/test/resources/strings_in.properties rename android-json-form-wizard/src/test/resources/{test_translation_en_US => test_form_translation_en_US} (99%) rename android-json-form-wizard/src/test/resources/{test_translation_in => test_form_translation_in} (99%) rename android-json-form-wizard/src/test/resources/{test_translation_interpolated => test_form_translation_interpolated} (99%) diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/NativeFormLangUtils.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/NativeFormLangUtils.java index 6be00548d..06ddaaa08 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/NativeFormLangUtils.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/NativeFormLangUtils.java @@ -39,7 +39,7 @@ public static String getTranslatedString(String jsonForm) { ResourceBundle mlsResourceBundle = ResourceBundle.getBundle(getPropertiesFileName(jsonForm)); StringBuffer stringBuffer = new StringBuffer(); - Pattern interpolatedStringPattern = Pattern.compile("\"\\{\\{([a-zA-Z_0-9\\.]+)\\}\\}\""); + Pattern interpolatedStringPattern = Pattern.compile("\\{\\{([a-zA-Z_0-9\\.]+)\\}\\}"); Matcher matcher = interpolatedStringPattern.matcher(jsonForm); while (matcher.find()) { matcher.appendReplacement(stringBuffer, mlsResourceBundle.getString(matcher.group(1))); @@ -50,7 +50,7 @@ public static String getTranslatedString(String jsonForm) { } public static String getPropertiesFileName(String jsonForm) { - Pattern propertiesFileNamePattern = Pattern.compile("\"properties_file_name\": \"(strings)\""); + Pattern propertiesFileNamePattern = Pattern.compile("\"?properties_file_name\"?: \"([a-zA-Z_0-9\\.]+)\""); Matcher matcher = propertiesFileNamePattern.matcher(jsonForm); matcher.find(); String propertiesFileName = matcher.group(1); diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/NativeFomLangUtilsTest.java b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/NativeFomLangUtilsTest.java index 275fe16ad..99eff9590 100644 --- a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/NativeFomLangUtilsTest.java +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/NativeFomLangUtilsTest.java @@ -1,11 +1,20 @@ package com.vijay.jsonwizard.utils; +import android.content.Context; +import android.content.res.AssetManager; + +import org.junit.Ignore; import org.junit.Test; +import java.io.IOException; import java.io.InputStream; import java.util.Locale; +import static com.vijay.jsonwizard.utils.Utils.getTranslatedYamlFile; import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; /** * Created by Vincent Karuri on 20/02/2020 @@ -13,17 +22,32 @@ public class NativeFomLangUtilsTest { @Test - public void testStringInterpolation() { + public void testJsonFormTranslation() { Locale.setDefault(new Locale("id")); - String expectedJsonForm = getFileContentsAsString("test_translation_in"); - String interpolatedJsonForm = getFileContentsAsString("test_translation_interpolated"); + String expectedJsonForm = getFileContentsAsString("test_form_translation_in"); + String interpolatedJsonForm = getFileContentsAsString("test_form_translation_interpolated"); assertEquals(expectedJsonForm, NativeFormLangUtils.getTranslatedString(interpolatedJsonForm)); Locale.setDefault(new Locale("en", "US")); - expectedJsonForm = getFileContentsAsString("test_translation_en_US"); + expectedJsonForm = getFileContentsAsString("test_form_translation_en_US"); assertEquals(expectedJsonForm, NativeFormLangUtils.getTranslatedString(interpolatedJsonForm)); } + @Test + @Ignore + public void testYamlFileTranslation() throws IOException { + Context context = mock(Context.class); + AssetManager assetManager = mock(AssetManager.class); + doReturn(getClass().getClassLoader().getResourceAsStream("interpolated_yaml")) + .when(assetManager) + .open(eq("file_name")); + doReturn(assetManager).when(context).getAssets(); + + Locale.setDefault(new Locale("en", "US")); + String translatedYamlStr = getTranslatedYamlFile("file_name", context); + assertEquals(getFileContentsAsString("test_yaml_translation_en_US"), translatedYamlStr); + } + private String getFileContentsAsString(String filePath) { return convertStreamToString(getClass().getClassLoader().getResourceAsStream(filePath)); } diff --git a/android-json-form-wizard/src/test/resources/form_strings_en_US.properties b/android-json-form-wizard/src/test/resources/form_strings_en_US.properties new file mode 100644 index 000000000..0af12d9bf --- /dev/null +++ b/android-json-form-wizard/src/test/resources/form_strings_en_US.properties @@ -0,0 +1,16 @@ +step1.title = New client record +step1.previous_label = SAVE AND EXIT +step1.submit_label = SAVE +step1.patient_name_label.text = Name +step1.sex.label = Gender +step1.sex.options.Female.text = Female +step1.sex.options.Male.text = Male +step1.sex.v_required.err = Please specify gender +step1.patient_dob.hint = Patient's date of birth +step1.patient_dob.duration.label = Age +step1.patient_occupation_label.text = Occupation +step1.been_treated.label = Has been treated for malaria in the past 3 months? +step1.been_treated.options.Yes.text = Yes +step1.been_treated.options.No.text = No +step1.been_treated.options.not_answered.text = Not Answered +step1.been_treated.v_required.err = Has the patient been treated for malaria in the past 3 months? \ No newline at end of file diff --git a/android-json-form-wizard/src/test/resources/form_strings_in.properties b/android-json-form-wizard/src/test/resources/form_strings_in.properties new file mode 100644 index 000000000..b12a28c3f --- /dev/null +++ b/android-json-form-wizard/src/test/resources/form_strings_in.properties @@ -0,0 +1,16 @@ +step1.title = Data Pasien Baru +step1.previous_label = SIMPAN DAN KELUAR +step1.submit_label = SIMPAN +step1.patient_name_label.text = Nama +step1.sex.label = Jenis kelamin pasien +step1.sex.options.Female.text = Perempuan +step1.sex.options.Male.text = Laki-laki +step1.sex.v_required.err = Silakan sebutkan jenis kelamin +step1.patient_dob.hint = Tanggal lahir pasien +step1.patient_dob.duration.label = Usia +step1.patient_occupation_label.text = Pekerjaan pasien +step1.been_treated.label = Pernah dirawat karena Malaria dalam 3 bulan terakhir? +step1.been_treated.options.Yes.text = Ya +step1.been_treated.options.No.text = Tidak +step1.been_treated.options.not_answered.text = Tidak dijawab +step1.been_treated.v_required.err = Pernahkah pasien dirawat karena malaria dalam 3 bulan terakhir? \ No newline at end of file diff --git a/android-json-form-wizard/src/test/resources/strings_en_US.properties b/android-json-form-wizard/src/test/resources/strings_en_US.properties deleted file mode 100644 index 27a27a2a2..000000000 --- a/android-json-form-wizard/src/test/resources/strings_en_US.properties +++ /dev/null @@ -1,16 +0,0 @@ -step1.title = "New client record" -step1.previous_label = "SAVE AND EXIT" -step1.submit_label = "SAVE" -step1.patient_name_label.text = "Name" -step1.sex.label = "Gender" -step1.sex.options.Female.text = "Female" -step1.sex.options.Male.text = "Male" -step1.sex.v_required.err = "Please specify gender" -step1.patient_dob.hint = "Patient's date of birth" -step1.patient_dob.duration.label = "Age" -step1.patient_occupation_label.text = "Occupation" -step1.been_treated.label = "Has been treated for malaria in the past 3 months?" -step1.been_treated.options.Yes.text = "Yes" -step1.been_treated.options.No.text = "No" -step1.been_treated.options.not_answered.text = "Not Answered" -step1.been_treated.v_required.err = "Has the patient been treated for malaria in the past 3 months?" \ No newline at end of file diff --git a/android-json-form-wizard/src/test/resources/strings_in.properties b/android-json-form-wizard/src/test/resources/strings_in.properties deleted file mode 100644 index 98e42154a..000000000 --- a/android-json-form-wizard/src/test/resources/strings_in.properties +++ /dev/null @@ -1,16 +0,0 @@ -step1.title = "Data Pasien Baru" -step1.previous_label = "SIMPAN DAN KELUAR" -step1.submit_label = "SIMPAN" -step1.patient_name_label.text = "Nama" -step1.sex.label = "Jenis kelamin pasien" -step1.sex.options.Female.text = "Perempuan" -step1.sex.options.Male.text = "Laki-laki" -step1.sex.v_required.err = "Silakan sebutkan jenis kelamin" -step1.patient_dob.hint = "Tanggal lahir pasien" -step1.patient_dob.duration.label = "Usia" -step1.patient_occupation_label.text = "Pekerjaan pasien" -step1.been_treated.label = "Pernah dirawat karena Malaria dalam 3 bulan terakhir?" -step1.been_treated.options.Yes.text = "Ya" -step1.been_treated.options.No.text = "Tidak" -step1.been_treated.options.not_answered.text = "Tidak dijawab" -step1.been_treated.v_required.err = "Pernahkah pasien dirawat karena malaria dalam 3 bulan terakhir?" \ No newline at end of file diff --git a/android-json-form-wizard/src/test/resources/test_translation_en_US b/android-json-form-wizard/src/test/resources/test_form_translation_en_US similarity index 99% rename from android-json-form-wizard/src/test/resources/test_translation_en_US rename to android-json-form-wizard/src/test/resources/test_form_translation_en_US index 0ab4da551..c871d137e 100644 --- a/android-json-form-wizard/src/test/resources/test_translation_en_US +++ b/android-json-form-wizard/src/test/resources/test_form_translation_en_US @@ -2,7 +2,7 @@ "count": "1", "encounter_type": "patient_registration", "entity_id": "", - "properties_file_name": "strings", + "properties_file_name": "form_strings", "metadata": { "start": { "openmrs_entity_parent": "", diff --git a/android-json-form-wizard/src/test/resources/test_translation_in b/android-json-form-wizard/src/test/resources/test_form_translation_in similarity index 99% rename from android-json-form-wizard/src/test/resources/test_translation_in rename to android-json-form-wizard/src/test/resources/test_form_translation_in index 09c0af923..8a206c188 100644 --- a/android-json-form-wizard/src/test/resources/test_translation_in +++ b/android-json-form-wizard/src/test/resources/test_form_translation_in @@ -2,7 +2,7 @@ "count": "1", "encounter_type": "patient_registration", "entity_id": "", - "properties_file_name": "strings", + "properties_file_name": "form_strings", "metadata": { "start": { "openmrs_entity_parent": "", diff --git a/android-json-form-wizard/src/test/resources/test_translation_interpolated b/android-json-form-wizard/src/test/resources/test_form_translation_interpolated similarity index 99% rename from android-json-form-wizard/src/test/resources/test_translation_interpolated rename to android-json-form-wizard/src/test/resources/test_form_translation_interpolated index 182c2bb2d..1f15b80ec 100644 --- a/android-json-form-wizard/src/test/resources/test_translation_interpolated +++ b/android-json-form-wizard/src/test/resources/test_form_translation_interpolated @@ -2,7 +2,7 @@ "count": "1", "encounter_type": "patient_registration", "entity_id": "", - "properties_file_name": "strings", + "properties_file_name": "form_strings", "metadata": { "start": { "openmrs_entity_parent": "", From 2707dc9992829a395c13cabb391123491fb4ec72 Mon Sep 17 00:00:00 2001 From: Vincent Karuri Date: Mon, 2 Mar 2020 13:19:32 +0300 Subject: [PATCH 12/20] Test yaml translation --- .../java/com/vijay/jsonwizard/utils/Utils.java | 5 ++++- .../jsonwizard/utils/NativeFomLangUtilsTest.java | 1 - .../src/test/resources/interpolated_yaml | 15 +++++++++++++++ .../test/resources/test_yaml_translation_en_US | 14 ++++++++++++++ .../test/resources/yaml_strings_en_US.properties | 4 ++++ 5 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 android-json-form-wizard/src/test/resources/interpolated_yaml create mode 100644 android-json-form-wizard/src/test/resources/test_yaml_translation_en_US create mode 100644 android-json-form-wizard/src/test/resources/yaml_strings_en_US.properties diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/Utils.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/Utils.java index e507c6fca..32d97805e 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/Utils.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/Utils.java @@ -487,7 +487,7 @@ public static Iterable readYamlFile(String fileName, Context context) { Yaml yaml = new Yaml(); Iterable objectIterable = null; try { - String translatedYamlStr = getTranslatedString(getAssetFileAsString(fileName, context)); + String translatedYamlStr = getTranslatedYamlFile(fileName, context); objectIterable = yaml.loadAll(translatedYamlStr); } catch (IOException e) { Timber.e(e); @@ -496,6 +496,9 @@ public static Iterable readYamlFile(String fileName, Context context) { return objectIterable; } + public static String getTranslatedYamlFile(String fileName, Context context) throws IOException { + return getTranslatedString(getAssetFileAsString(fileName, context)); + } public static String getAssetFileAsString(String fileName, Context context) throws IOException { StringBuilder stringBuilder = new StringBuilder(); diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/NativeFomLangUtilsTest.java b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/NativeFomLangUtilsTest.java index 99eff9590..2d0ef93e0 100644 --- a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/NativeFomLangUtilsTest.java +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/NativeFomLangUtilsTest.java @@ -34,7 +34,6 @@ public void testJsonFormTranslation() { } @Test - @Ignore public void testYamlFileTranslation() throws IOException { Context context = mock(Context.class); AssetManager assetManager = mock(AssetManager.class); diff --git a/android-json-form-wizard/src/test/resources/interpolated_yaml b/android-json-form-wizard/src/test/resources/interpolated_yaml new file mode 100644 index 000000000..87e84747d --- /dev/null +++ b/android-json-form-wizard/src/test/resources/interpolated_yaml @@ -0,0 +1,15 @@ +--- +properties_file_name: "yaml_strings", +sub_group: ultrasound_tests_results +fields: + - template: "{{profile_contact_tab_tests.ultrasound_test}}: {ultrasound}" + relevance: "ultrasound != ''" + + - template: "{{profile_contact_tab_tests.ultrasound_test_not_done_reason}}: {ultrasound_notdone}" + relevance: "ultrasound_notdone != ''" + + - template: "{{profile_contact_tab_tests.ultrasound_test_not_done_other_reason}}: {ultrasound_notdone_other}" + relevance: "ultrasound_notdone_other != ''" + + - template: "{{profile_contact_tab_tests.ultrasound_test_done_date}}: {ultrasound_date}" + relevance: "ultrasound_date != ''" \ No newline at end of file diff --git a/android-json-form-wizard/src/test/resources/test_yaml_translation_en_US b/android-json-form-wizard/src/test/resources/test_yaml_translation_en_US new file mode 100644 index 000000000..97b8acf03 --- /dev/null +++ b/android-json-form-wizard/src/test/resources/test_yaml_translation_en_US @@ -0,0 +1,14 @@ +--- +sub_group: ultrasound_tests_results +fields: + - template: "Ultrasound test: {ultrasound}" + relevance: "ultrasound != ''" + + - template: "Ultrasound not done reason: {ultrasound_notdone}" + relevance: "ultrasound_notdone != ''" + + - template: "Ultrasound not done other reason: {ultrasound_notdone_other}" + relevance: "ultrasound_notdone_other != ''" + + - template: "Ultrasound done date: {ultrasound_date}" + relevance: "ultrasound_date != ''" \ No newline at end of file diff --git a/android-json-form-wizard/src/test/resources/yaml_strings_en_US.properties b/android-json-form-wizard/src/test/resources/yaml_strings_en_US.properties new file mode 100644 index 000000000..7d6edf73b --- /dev/null +++ b/android-json-form-wizard/src/test/resources/yaml_strings_en_US.properties @@ -0,0 +1,4 @@ +profile_contact_tab_tests.ultrasound_test=Ultrasound test +profile_contact_tab_tests.ultrasound_test_not_done_reason=Ultrasound not done reason +profile_contact_tab_tests.ultrasound_test_not_done_other_reason=Ultrasound not done other reason +profile_contact_tab_tests.ultrasound_test_done_date=Ultrasound done date From db8c3ec80d5859f7a7b97759c4fdbacbcfa498d2 Mon Sep 17 00:00:00 2001 From: Vincent Karuri Date: Mon, 2 Mar 2020 14:37:18 +0300 Subject: [PATCH 13/20] Modify how yaml files are read to account for new line characters --- .../java/com/vijay/jsonwizard/utils/Utils.java | 17 ++++++----------- .../utils/NativeFomLangUtilsTest.java | 8 +------- .../test/resources/test_yaml_translation_en_US | 1 + 3 files changed, 8 insertions(+), 18 deletions(-) diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/Utils.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/Utils.java index 32d97805e..8e5991417 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/Utils.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/Utils.java @@ -38,11 +38,8 @@ import org.json.JSONObject; import org.yaml.snakeyaml.Yaml; -import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; -import java.nio.charset.StandardCharsets; import java.text.ParseException; import java.util.ArrayList; import java.util.Arrays; @@ -54,6 +51,7 @@ import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Scanner; import java.util.Set; import java.util.concurrent.TimeUnit; @@ -501,16 +499,13 @@ public static String getTranslatedYamlFile(String fileName, Context context) thr } public static String getAssetFileAsString(String fileName, Context context) throws IOException { - StringBuilder stringBuilder = new StringBuilder(); InputStream inputStream = context.getAssets().open(fileName); - BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); - String str; - while ((str = bufferedReader.readLine()) != null) { - stringBuilder.append(str); - } - bufferedReader.close(); + return convertStreamToString(inputStream); + } - return stringBuilder.toString(); + public static String convertStreamToString(InputStream inputStream) { + Scanner s = new Scanner(inputStream).useDelimiter("\\A"); + return s.hasNext() ? s.next() : ""; } public static void buildRulesWithUniqueId(JSONObject element, String uniqueId, String ruleType, WidgetArgs widgetArgs, Map>> rulesFileMap) throws JSONException { diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/NativeFomLangUtilsTest.java b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/NativeFomLangUtilsTest.java index 2d0ef93e0..03d2a1a53 100644 --- a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/NativeFomLangUtilsTest.java +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/NativeFomLangUtilsTest.java @@ -3,13 +3,12 @@ import android.content.Context; import android.content.res.AssetManager; -import org.junit.Ignore; import org.junit.Test; import java.io.IOException; -import java.io.InputStream; import java.util.Locale; +import static com.vijay.jsonwizard.utils.Utils.convertStreamToString; import static com.vijay.jsonwizard.utils.Utils.getTranslatedYamlFile; import static org.junit.Assert.assertEquals; import static org.mockito.ArgumentMatchers.eq; @@ -50,9 +49,4 @@ public void testYamlFileTranslation() throws IOException { private String getFileContentsAsString(String filePath) { return convertStreamToString(getClass().getClassLoader().getResourceAsStream(filePath)); } - - private String convertStreamToString(InputStream is) { - java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A"); - return s.hasNext() ? s.next() : ""; - } } diff --git a/android-json-form-wizard/src/test/resources/test_yaml_translation_en_US b/android-json-form-wizard/src/test/resources/test_yaml_translation_en_US index 97b8acf03..25b485c80 100644 --- a/android-json-form-wizard/src/test/resources/test_yaml_translation_en_US +++ b/android-json-form-wizard/src/test/resources/test_yaml_translation_en_US @@ -1,4 +1,5 @@ --- +properties_file_name: "yaml_strings", sub_group: ultrasound_tests_results fields: - template: "Ultrasound test: {ultrasound}" From 3430e70ad0d32d029ab33d46b82acd8ad0c51606 Mon Sep 17 00:00:00 2001 From: Vincent Karuri Date: Mon, 2 Mar 2020 14:43:27 +0300 Subject: [PATCH 14/20] Minor refactore --- .../vijay/jsonwizard/utils/NativeFomLangUtilsTest.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/NativeFomLangUtilsTest.java b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/NativeFomLangUtilsTest.java index 03d2a1a53..f848efbc4 100644 --- a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/NativeFomLangUtilsTest.java +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/NativeFomLangUtilsTest.java @@ -6,6 +6,7 @@ import org.junit.Test; import java.io.IOException; +import java.io.InputStream; import java.util.Locale; import static com.vijay.jsonwizard.utils.Utils.convertStreamToString; @@ -36,7 +37,7 @@ public void testJsonFormTranslation() { public void testYamlFileTranslation() throws IOException { Context context = mock(Context.class); AssetManager assetManager = mock(AssetManager.class); - doReturn(getClass().getClassLoader().getResourceAsStream("interpolated_yaml")) + doReturn(getTestResource("interpolated_yaml")) .when(assetManager) .open(eq("file_name")); doReturn(assetManager).when(context).getAssets(); @@ -46,7 +47,11 @@ public void testYamlFileTranslation() throws IOException { assertEquals(getFileContentsAsString("test_yaml_translation_en_US"), translatedYamlStr); } + private InputStream getTestResource(String filePath) { + return getClass().getClassLoader().getResourceAsStream(filePath); + } + private String getFileContentsAsString(String filePath) { - return convertStreamToString(getClass().getClassLoader().getResourceAsStream(filePath)); + return convertStreamToString(getTestResource(filePath)); } } From f38d260a150833c00539f097c03246f77a0ee962 Mon Sep 17 00:00:00 2001 From: Vincent Karuri Date: Mon, 2 Mar 2020 15:55:12 +0300 Subject: [PATCH 15/20] Return untranslated String if the translations property file is not specified --- .../jsonwizard/utils/NativeFormLangUtils.java | 16 +- .../utils/NativeFomLangUtilsTest.java | 33 +++- ...nslation_interpolated_missing_translations | 179 ++++++++++++++++++ ...aml => test_yaml_translation_interpolated} | 0 ...nslation_interpolated_missing_translations | 14 ++ 5 files changed, 230 insertions(+), 12 deletions(-) create mode 100644 android-json-form-wizard/src/test/resources/test_form_translation_interpolated_missing_translations rename android-json-form-wizard/src/test/resources/{interpolated_yaml => test_yaml_translation_interpolated} (100%) create mode 100644 android-json-form-wizard/src/test/resources/test_yaml_translation_interpolated_missing_translations diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/NativeFormLangUtils.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/NativeFormLangUtils.java index 06ddaaa08..f57ff9103 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/NativeFormLangUtils.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/NativeFormLangUtils.java @@ -11,6 +11,8 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; +import timber.log.Timber; + public class NativeFormLangUtils { public static String getLanguage(Context ctx) { @@ -36,7 +38,13 @@ public static Context setAppLocale(Context ctx, String language) { } public static String getTranslatedString(String jsonForm) { - ResourceBundle mlsResourceBundle = ResourceBundle.getBundle(getPropertiesFileName(jsonForm)); + String translationsFileName = getTranslationsFileName(jsonForm); + if (translationsFileName.isEmpty()) { + Timber.e("Could not translate the string. Translation file name is not specified!"); + return jsonForm; + } + + ResourceBundle mlsResourceBundle = ResourceBundle.getBundle(getTranslationsFileName(jsonForm)); StringBuffer stringBuffer = new StringBuffer(); Pattern interpolatedStringPattern = Pattern.compile("\\{\\{([a-zA-Z_0-9\\.]+)\\}\\}"); @@ -49,11 +57,9 @@ public static String getTranslatedString(String jsonForm) { return stringBuffer.toString(); } - public static String getPropertiesFileName(String jsonForm) { + public static String getTranslationsFileName(String jsonForm) { Pattern propertiesFileNamePattern = Pattern.compile("\"?properties_file_name\"?: \"([a-zA-Z_0-9\\.]+)\""); Matcher matcher = propertiesFileNamePattern.matcher(jsonForm); - matcher.find(); - String propertiesFileName = matcher.group(1); - return propertiesFileName; + return matcher.find() ? matcher.group(1) : ""; } } diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/NativeFomLangUtilsTest.java b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/NativeFomLangUtilsTest.java index f848efbc4..91b9f7ebc 100644 --- a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/NativeFomLangUtilsTest.java +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/NativeFomLangUtilsTest.java @@ -22,7 +22,7 @@ public class NativeFomLangUtilsTest { @Test - public void testJsonFormTranslation() { + public void testJsonFormTranslationShouldTranslateForm() { Locale.setDefault(new Locale("id")); String expectedJsonForm = getFileContentsAsString("test_form_translation_in"); String interpolatedJsonForm = getFileContentsAsString("test_form_translation_interpolated"); @@ -34,17 +34,36 @@ public void testJsonFormTranslation() { } @Test - public void testYamlFileTranslation() throws IOException { + public void testYamlFileTranslationShouldTranslateYamlFile() throws IOException { + Context context = mockAssetManager("test_yaml_translation_interpolated"); + Locale.setDefault(new Locale("en", "US")); + String translatedYamlStr = getTranslatedYamlFile("file_name", context); + assertEquals(getFileContentsAsString("test_yaml_translation_en_US"), translatedYamlStr); + } + + @Test + public void testJsonFormTranslationShouldReturnUntranslatedForm() { + Locale.setDefault(new Locale("id")); + String interpolatedJsonForm = getFileContentsAsString("test_form_translation_interpolated_missing_translations"); + assertEquals(interpolatedJsonForm, NativeFormLangUtils.getTranslatedString(interpolatedJsonForm)); + } + + @Test + public void testYamlFileTranslationShouldReturnUntranslatedYamlFile() throws IOException { + Context context = mockAssetManager("test_yaml_translation_interpolated_missing_translations"); + Locale.setDefault(new Locale("en", "US")); + String translatedYamlStr = getTranslatedYamlFile("file_name", context); + assertEquals(getFileContentsAsString("test_yaml_translation_interpolated_missing_translations"), translatedYamlStr); + } + + private Context mockAssetManager(String yamlFilePath) throws IOException { Context context = mock(Context.class); AssetManager assetManager = mock(AssetManager.class); - doReturn(getTestResource("interpolated_yaml")) + doReturn(getTestResource(yamlFilePath)) .when(assetManager) .open(eq("file_name")); doReturn(assetManager).when(context).getAssets(); - - Locale.setDefault(new Locale("en", "US")); - String translatedYamlStr = getTranslatedYamlFile("file_name", context); - assertEquals(getFileContentsAsString("test_yaml_translation_en_US"), translatedYamlStr); + return context; } private InputStream getTestResource(String filePath) { diff --git a/android-json-form-wizard/src/test/resources/test_form_translation_interpolated_missing_translations b/android-json-form-wizard/src/test/resources/test_form_translation_interpolated_missing_translations new file mode 100644 index 000000000..85560e552 --- /dev/null +++ b/android-json-form-wizard/src/test/resources/test_form_translation_interpolated_missing_translations @@ -0,0 +1,179 @@ +{ + "count": "1", + "encounter_type": "patient_registration", + "entity_id": "", + "metadata": { + "start": { + "openmrs_entity_parent": "", + "openmrs_entity": "concept", + "openmrs_data_type": "start", + "openmrs_entity_id": "163137AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + }, + "end": { + "openmrs_entity_parent": "", + "openmrs_entity": "concept", + "openmrs_data_type": "end", + "openmrs_entity_id": "163138AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + }, + "today": { + "openmrs_entity_parent": "", + "openmrs_entity": "encounter", + "openmrs_entity_id": "encounter_date" + }, + "deviceid": { + "openmrs_entity_parent": "", + "openmrs_entity": "concept", + "openmrs_data_type": "deviceid", + "openmrs_entity_id": "163149AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + }, + "subscriberid": { + "openmrs_entity_parent": "", + "openmrs_entity": "concept", + "openmrs_data_type": "subscriberid", + "openmrs_entity_id": "163150AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + }, + "simserial": { + "openmrs_entity_parent": "", + "openmrs_entity": "concept", + "openmrs_data_type": "simserial", + "openmrs_entity_id": "163151AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + }, + "phonenumber": { + "openmrs_entity_parent": "", + "openmrs_entity": "concept", + "openmrs_data_type": "phonenumber", + "openmrs_entity_id": "163152AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + }, + "encounter_location": "" + }, + "step1": { + "title": "{{step1.title}}", + "display_back_button": "true", + "previous_label": "{{step1.previous_label}}", + "bottom_navigation": "true", + "bottom_navigation_orientation": "vertical", + "next_type": "submit", + "submit_label": "{{step1.submit_label}}", + "next_form": "json.form/patient-registration-form.json", + "fields": [ + { + "key": "patient_name_label", + "type": "label", + "text": "{{step1.patient_name_label.text}}", + "text_color": "#000000" + }, + { + "key": "patient_name", + "openmrs_entity_parent": "", + "openmrs_entity": "person", + "openmrs_entity_id": "first_name", + "type": "edit_text", + "edit_type": "name" + }, + { + "key": "patient_id_label", + "type": "label", + "text": "Patient ID", + "text_color": "#000000" + }, + { + "key": "patient_id", + "openmrs_entity_parent": "", + "openmrs_entity": "", + "openmrs_entity_id": "", + "type": "edit_text", + "v_required": { + "value": "true", + "err": "Please enter patient ID" + } + }, + { + "key": "sex", + "openmrs_entity_parent": "", + "openmrs_entity": "person", + "openmrs_entity_id": "gender", + "type": "native_radio", + "label": "{{step1.sex.label}}", + "options": [ + { + "key": "Female", + "text": "{{step1.sex.options.Female.text}}" + }, + { + "key": "Male", + "text": "{{step1.sex.options.Male.text}}" + } + ], + "v_required": { + "value": "true", + "err": "{{step1.sex.v_required.err}}" + } + }, + { + "key": "patient_dob", + "openmrs_entity_parent": "", + "openmrs_entity": "person_attribute", + "openmrs_entity_id": "birthdate", + "type": "date_picker", + "hint": "{{step1.patient_dob.hint}}", + "expanded": false, + "duration": { + "label": "{{step1.patient_dob.duration.label}}" + }, + "min_date": "today-100y", + "max_date": "today" + }, + { + "key": "age", + "openmrs_entity_parent": "", + "openmrs_entity": "", + "openmrs_entity_id": "", + "type": "hidden", + "value": "" + }, + { + "key": "patient_occupation_label", + "type": "label", + "text": "{{step1.patient_occupation_label.text}}", + "text_color": "#000000" + }, + { + "key": "patient_occupation", + "openmrs_entity_parent": "", + "openmrs_entity": "", + "openmrs_entity_id": "", + "type": "edit_text", + "v_required": { + "value": "false", + "err": "" + } + }, + { + "key": "been_treated", + "openmrs_entity_parent": "", + "openmrs_entity": "", + "openmrs_entity_id": "", + "type": "native_radio", + "label": "{{step1.been_treated.label}}", + "options": [ + { + "key": "Yes", + "text": "{{step1.been_treated.options.Yes.text}}" + }, + { + "key": "No", + "text": "{{step1.been_treated.options.No.text}}" + }, + { + "key": "Not Answered", + "text": "{{step1.been_treated.options.not_answered.text}}" + } + ], + "v_required": { + "value": "true", + "err": "{{step1.been_treated.v_required.err}}" + } + } + ] + } +} \ No newline at end of file diff --git a/android-json-form-wizard/src/test/resources/interpolated_yaml b/android-json-form-wizard/src/test/resources/test_yaml_translation_interpolated similarity index 100% rename from android-json-form-wizard/src/test/resources/interpolated_yaml rename to android-json-form-wizard/src/test/resources/test_yaml_translation_interpolated diff --git a/android-json-form-wizard/src/test/resources/test_yaml_translation_interpolated_missing_translations b/android-json-form-wizard/src/test/resources/test_yaml_translation_interpolated_missing_translations new file mode 100644 index 000000000..a48f7b469 --- /dev/null +++ b/android-json-form-wizard/src/test/resources/test_yaml_translation_interpolated_missing_translations @@ -0,0 +1,14 @@ +--- +sub_group: ultrasound_tests_results +fields: + - template: "{{profile_contact_tab_tests.ultrasound_test}}: {ultrasound}" + relevance: "ultrasound != ''" + + - template: "{{profile_contact_tab_tests.ultrasound_test_not_done_reason}}: {ultrasound_notdone}" + relevance: "ultrasound_notdone != ''" + + - template: "{{profile_contact_tab_tests.ultrasound_test_not_done_other_reason}}: {ultrasound_notdone_other}" + relevance: "ultrasound_notdone_other != ''" + + - template: "{{profile_contact_tab_tests.ultrasound_test_done_date}}: {ultrasound_date}" + relevance: "ultrasound_date != ''" \ No newline at end of file From 4bfc476ed95402ccc0add453ec136057c0e8809e Mon Sep 17 00:00:00 2001 From: Vincent Karuri Date: Mon, 2 Mar 2020 16:23:09 +0300 Subject: [PATCH 16/20] Add docstring --- .../jsonwizard/utils/NativeFormLangUtils.java | 28 ++++++++++++++----- .../com/vijay/jsonwizard/utils/Utils.java | 28 +++++++++++++++++++ 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/NativeFormLangUtils.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/NativeFormLangUtils.java index 73d769e53..d3be8fce7 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/NativeFormLangUtils.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/NativeFormLangUtils.java @@ -37,18 +37,26 @@ public static Context setAppLocale(Context ctx, String language) { return context; } - public static String getTranslatedString(String jsonForm) { - String translationsFileName = getTranslationsFileName(jsonForm); + /** + * Performs translation on an interpolated {@param str} + * i.e. a String containing tokens in the format {{string_name}}, + * replacing these tokens with their corresponding values for the current Locale + * + * @param str + * @return + */ + public static String getTranslatedString(String str) { + String translationsFileName = getTranslationsFileName(str); if (translationsFileName.isEmpty()) { Timber.e("Could not translate the String. Translation file name is not specified!"); - return jsonForm; + return str; } - ResourceBundle mlsResourceBundle = ResourceBundle.getBundle(getTranslationsFileName(jsonForm)); + ResourceBundle mlsResourceBundle = ResourceBundle.getBundle(getTranslationsFileName(str)); StringBuffer stringBuffer = new StringBuffer(); Pattern interpolatedStringPattern = Pattern.compile("\\{\\{([a-zA-Z_0-9\\.]+)\\}\\}"); - Matcher matcher = interpolatedStringPattern.matcher(jsonForm); + Matcher matcher = interpolatedStringPattern.matcher(str); while (matcher.find()) { matcher.appendReplacement(stringBuffer, mlsResourceBundle.getString(matcher.group(1))); } @@ -57,9 +65,15 @@ public static String getTranslatedString(String jsonForm) { return stringBuffer.toString(); } - public static String getTranslationsFileName(String jsonForm) { + /** + * Gets the name of the translation file to be applied to the {@param str} + * + * @param str + * @return + */ + public static String getTranslationsFileName(String str) { Pattern propertiesFileNamePattern = Pattern.compile("\"?properties_file_name\"?: \"([a-zA-Z_0-9\\.]+)\""); - Matcher matcher = propertiesFileNamePattern.matcher(jsonForm); + Matcher matcher = propertiesFileNamePattern.matcher(str); return matcher.find() ? matcher.group(1) : ""; } } diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/Utils.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/Utils.java index 8e5991417..1552c3320 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/Utils.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/Utils.java @@ -494,15 +494,43 @@ public static Iterable readYamlFile(String fileName, Context context) { return objectIterable; } + /** + * Translates a yaml file specified by {@param fileName} and returns its String representation + * + * @param fileName + * @param context + * + * @return Translated Yaml file in its String representation + * + * @throws IOException + */ public static String getTranslatedYamlFile(String fileName, Context context) throws IOException { return getTranslatedString(getAssetFileAsString(fileName, context)); } + /** + * + * Gets a file specified by {@param fileName} from the assets folder as a String + * + * @param fileName + * @param context + * + * @return A file from the assets folder as a String + * + * @throws IOException + */ public static String getAssetFileAsString(String fileName, Context context) throws IOException { InputStream inputStream = context.getAssets().open(fileName); return convertStreamToString(inputStream); } + /** + * Converts an {@link InputStream} into a String + * + * @param inputStream + * + * @return String representation of an {@link InputStream} + */ public static String convertStreamToString(InputStream inputStream) { Scanner s = new Scanner(inputStream).useDelimiter("\\A"); return s.hasNext() ? s.next() : ""; From e5ec6d4376600912e67aa1a696ca1b2043c6b434 Mon Sep 17 00:00:00 2001 From: Benjamin Mwalimu Date: Thu, 5 Mar 2020 16:43:00 +0300 Subject: [PATCH 17/20] :zap: Add the `ANC` registration forms to test translations, Update some logic to fix form loading on devices --- android-json-form-wizard/build.gradle | 4 +-- .../activities/JsonFormBaseActivity.java | 4 +-- .../constants/JsonFormConstants.java | 3 +- .../jsonwizard/utils/NativeFormLangUtils.java | 2 +- .../nativeform/MainActivity.java | 30 ++++++++-------- .../resources/anc_register_en_US.properties | 35 +++++++++++++++++++ .../main/resources/anc_register_fr.properties | 35 +++++++++++++++++++ 7 files changed, 91 insertions(+), 22 deletions(-) create mode 100644 sample/src/main/resources/anc_register_en_US.properties create mode 100644 sample/src/main/resources/anc_register_fr.properties diff --git a/android-json-form-wizard/build.gradle b/android-json-form-wizard/build.gradle index 7a0b4d776..65dd806f7 100644 --- a/android-json-form-wizard/build.gradle +++ b/android-json-form-wizard/build.gradle @@ -6,11 +6,11 @@ buildscript { dependencies { classpath ('com.android.tools.build:gradle:3.5.3'){ transitive = true - exclude group: 'org.apache.httpcomponents', module: 'httpmime' + //exclude group: 'org.apache.httpcomponents', module: 'httpmime' } classpath 'org.apache.commons:commons-lang3:3.7' classpath 'org.kt3k.gradle.plugin:coveralls-gradle-plugin:2.8.3' - classpath 'org.apache.httpcomponents:httpmime:4.3' + //classpath 'org.apache.httpcomponents:httpmime:4.3' } } diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/activities/JsonFormBaseActivity.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/activities/JsonFormBaseActivity.java index a914d7077..fdebc5ed7 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/activities/JsonFormBaseActivity.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/activities/JsonFormBaseActivity.java @@ -58,8 +58,6 @@ abstract class JsonFormBaseActivity extends MultiLanguageActivity implements OnF private Map invalidFields = new HashMap<>(); private boolean isPreviousPressed = false; - public static final String PERFORM_FORM_TRANSLATION = "perform_form_translation"; - @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -88,7 +86,7 @@ protected void onCreate(Bundle savedInstanceState) { private String getForm() { String jsonForm = getIntent().getStringExtra(JsonFormConstants.JSON_FORM_KEY.JSON); - if (getIntent().getBooleanExtra(PERFORM_FORM_TRANSLATION, false)) { + if (getIntent().getBooleanExtra(JsonFormConstants.PERFORM_FORM_TRANSLATION, false)) { jsonForm = getTranslatedString(jsonForm); } return jsonForm; diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/constants/JsonFormConstants.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/constants/JsonFormConstants.java index 529cb0670..585e5dd39 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/constants/JsonFormConstants.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/constants/JsonFormConstants.java @@ -207,13 +207,14 @@ public class JsonFormConstants { public static final String FALSE = "false"; public static final String DISPLAY_SCROLL_BARS = "display_scroll_bars"; public static final String SKIP_BLANK_STEPS = "skip_blank_steps"; - public static final String MULTI_SELECT_LIST ="multi_select_list" ; + public static final String MULTI_SELECT_LIST = "multi_select_list"; public static final String FORM_VERSION = "form_version"; public static final String LABEL_CONSTRAINT_LAYOUT = "label_constraint_layout"; public static final String STEP = "step"; public static final String RULE = "rule/"; public static final String REFERENCE_EDIT_TEXT = "reference_edit_text"; public static final String DISPLAY_LABEL = "display_label"; + public static final String PERFORM_FORM_TRANSLATION = "perform_form_translation"; public interface MultiSelectUtils { String IS_HEADER = "isHeader"; diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/NativeFormLangUtils.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/NativeFormLangUtils.java index d3be8fce7..9e1b1780c 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/NativeFormLangUtils.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/NativeFormLangUtils.java @@ -72,7 +72,7 @@ public static String getTranslatedString(String str) { * @return */ public static String getTranslationsFileName(String str) { - Pattern propertiesFileNamePattern = Pattern.compile("\"?properties_file_name\"?: \"([a-zA-Z_0-9\\.]+)\""); + Pattern propertiesFileNamePattern = Pattern.compile("\"?properties_file_name\"?:\"([a-zA-Z_0-9\\.]+)\""); Matcher matcher = propertiesFileNamePattern.matcher(str); return matcher.find() ? matcher.group(1) : ""; } diff --git a/sample/src/main/java/org/smartregister/nativeform/MainActivity.java b/sample/src/main/java/org/smartregister/nativeform/MainActivity.java index bda98987b..3a14a2eae 100644 --- a/sample/src/main/java/org/smartregister/nativeform/MainActivity.java +++ b/sample/src/main/java/org/smartregister/nativeform/MainActivity.java @@ -65,19 +65,19 @@ public boolean onOptionsItemSelected(MenuItem item) { return true; } else if (id == R.id.action_single) { try { - startForm(REQUEST_CODE_GET_JSON, "single_form", null); + startForm(REQUEST_CODE_GET_JSON, "single_form", null, false); } catch (Exception e) { Log.e(TAG, e.getMessage(), e); } } else if (id == R.id.action_wizard) { try { - startForm(REQUEST_CODE_GET_JSON, "wizard_form", null); + startForm(REQUEST_CODE_GET_JSON, "wizard_form", null, false); } catch (Exception e) { Log.e(TAG, e.getMessage(), e); } } else if (id == R.id.action_validation) { try { - startForm(REQUEST_CODE_GET_JSON, "validation_form", null); + startForm(REQUEST_CODE_GET_JSON, "validation_form", null, false); } catch (Exception e) { Log.e(TAG, e.getMessage(), e); } @@ -97,8 +97,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) { } - public void startForm(int jsonFormActivityRequestCode, - String formName, String entityId) throws Exception { + public void startForm(int jsonFormActivityRequestCode, String formName, String entityId, boolean translate) throws Exception { final String STEP1 = "step1"; final String FIELDS = "fields"; @@ -211,6 +210,7 @@ public void startForm(int jsonFormActivityRequestCode, Intent intent = new Intent(this, JsonFormActivity.class); intent.putExtra("json", jsonForm.toString()); + intent.putExtra(JsonFormConstants.PERFORM_FORM_TRANSLATION, translate); Log.d(getClass().getName(), "form is " + jsonForm.toString()); startActivityForResult(intent, jsonFormActivityRequestCode); break; @@ -252,34 +252,34 @@ public void onClick(View view) { try { switch (id) { case R.id.child_enrollment: - startForm(REQUEST_CODE_GET_JSON, "single_form", null); + startForm(REQUEST_CODE_GET_JSON, "anc_register", null, true); break; case R.id.wizard_form: - startForm(REQUEST_CODE_GET_JSON, "wizard_form", null); + startForm(REQUEST_CODE_GET_JSON, "wizard_form", null, false); break; case R.id.native_form_basic: - startForm(REQUEST_CODE_GET_JSON, "basic_form", null); + startForm(REQUEST_CODE_GET_JSON, "basic_form", null, false); break; case R.id.rules_engine_skip_logic: - startForm(REQUEST_CODE_GET_JSON, "rules_engine_demo", null); + startForm(REQUEST_CODE_GET_JSON, "rules_engine_demo", null, false); break; case R.id.numbers_selector_widget: - startForm(REQUEST_CODE_GET_JSON, "constraints_demo", null); + startForm(REQUEST_CODE_GET_JSON, "constraints_demo", null, false); break; case R.id.generic_dialog_button: - startForm(REQUEST_CODE_GET_JSON, "generic_popup_form", null); + startForm(REQUEST_CODE_GET_JSON, "generic_popup_form", null, false); break; case R.id.validation_form_button: - startForm(REQUEST_CODE_GET_JSON, "validation_form", null); + startForm(REQUEST_CODE_GET_JSON, "validation_form", null, false); break; case R.id.expansion_panel_button: - startForm(REQUEST_CODE_GET_JSON, "expansion_panel_form", null); + startForm(REQUEST_CODE_GET_JSON, "expansion_panel_form", null, false); break; case R.id.repeating_group_button: - startForm(REQUEST_CODE_GET_JSON, "repeating_group", null); + startForm(REQUEST_CODE_GET_JSON, "repeating_group", null, false); break; case R.id.multiselect_list: - startForm(REQUEST_CODE_GET_JSON, "multi_select_list_form", null); + startForm(REQUEST_CODE_GET_JSON, "multi_select_list_form", null, false); break; default: break; diff --git a/sample/src/main/resources/anc_register_en_US.properties b/sample/src/main/resources/anc_register_en_US.properties new file mode 100644 index 000000000..8a06efeef --- /dev/null +++ b/sample/src/main/resources/anc_register_en_US.properties @@ -0,0 +1,35 @@ +anc_register.step1.title=ANC Registration +anc_register.step1.wom_image=Take a picture of the woman +anc_register.step1.anc_id.hint=ANC ID +anc_register.step1.anc_id.scanButtonText=Scan QR Code +anc_register.step1.anc_id.numeric.err=Please enter a valid ANC ID +anc_register.step1.anc_id.required.err=Please enter the Woman's ANC ID +anc_register.step1.first_name.hint=First name +anc_register.step1.first_name.required.err=Please enter the first name +anc_register.step1.first_name.regex.err=Please enter a valid name +anc_register.step1.last_name.hint=Last name +anc_register.step1.last_name.required.err=Please enter the last name +anc_register.step1.first_name.regex.err=Please enter a valid name +anc_register.step1.dob_entered.hint=Date of birth (DOB) +anc_register.step1.dob_entered.required.err=Please enter the date of birth +anc_register.step1.dob_unknown.option.text=DOB unknown? +anc_register.step1.age_entered.hint=Age +anc_register.step1.age_entered.constraint.err=Age must be a number +anc_register.step1.age_entered.constraint.err=Age must be equal to or greater than 10 +anc_register.step1.age_entered.constraint.err=Age must be equal or less than 49 +anc_register.step1.age_entered.required.err=Please enter the woman's age +anc_register.step1.home_address.hint=Home address +anc_register.step1.home_address.required.err=Please enter the woman's home address +anc_register.step1.phone_number.hint=Mobile phone number +anc_register.step1.age_entered.constraint.err=Phone number must be numeric +anc_register.step1.home_address.required.err=Please specify the woman's phone number +anc_register.step1.reminders.label=Woman wants to receive reminders during pregnancy +anc_register.step1.reminders.label_info_text=Does she want to receive reminders for care and messages regarding her health throughout her pregnancy? +anc_register.step1.reminders.option.text=Yes +anc_register.step1.reminders.option.text=No +anc_register.step1.reminders.required.err=Please select whether the woman has agreed to receiving reminder notifications +anc_register.step1.alt_name.hint=Alternate contact name +anc_register.step1.alt_name.regex.err=Please enter a valid name +anc_register.step1.alt_phone_number.hint=Alternate contact phone number +anc_register.step1.alt_name.regex.err=Phone number must be numeric + diff --git a/sample/src/main/resources/anc_register_fr.properties b/sample/src/main/resources/anc_register_fr.properties new file mode 100644 index 000000000..6ab630b67 --- /dev/null +++ b/sample/src/main/resources/anc_register_fr.properties @@ -0,0 +1,35 @@ +anc_register.step1.title=Enregistrement CPN +anc_register.step1.wom_image=Prenez une photo de la femme +anc_register.step1.anc_id.hint=ID CPN +anc_register.step1.anc_id.scanButtonText=Scanner le code QR +anc_register.step1.anc_id.numeric.err=Entrez un ID CPN valide SVP +anc_register.step1.anc_id.required.err=Entrez un ID CPN SVP +anc_register.step1.first_name.hint=Prénom +anc_register.step1.first_name.required.err=Entrez le prénom SVP +anc_register.step1.first_name.regex.err=Entrez un prénom valide SVP +anc_register.step1.last_name.hint=Nom +anc_register.step1.last_name.required.err=Entrez le nom SVP +anc_register.step1.first_name.regex.err=Entrez un nom valide SVP +anc_register.step1.dob_entered.hint=Date de naissance (DDN) +anc_register.step1.dob_entered.required.err=Entrez la DDN SVP +anc_register.step1.dob_unknown.option.text=DDN inconnue? +anc_register.step1.age_entered.hint=Âge +anc_register.step1.age_entered.constraint.err=L'âge doit être un nombre +anc_register.step1.age_entered.constraint.err=L'âge doit être supérieur ou égal à 10 +anc_register.step1.age_entered.constraint.err=L'âge doit être égal ou inférieur à 49 +anc_register.step1.age_entered.required.err=Veuillez entrer l'âge de la femme +anc_register.step1.home_address.hint=Adresse du domicile +anc_register.step1.home_address.required.err=Veuillez entrer l'adresse du domicile de la femme +anc_register.step1.phone_number.hint=Numéro de téléphone +anc_register.step1.age_entered.constraint.err=Le numéro de téléphone doit être numérique +anc_register.step1.home_address.required.err=Veuillez préciser le numéro de téléphone de la femme +anc_register.step1.reminders.label=La femme veut recevoir des rappels pendant la grossesse +anc_register.step1.reminders.label_info_text=Veut-elle recevoir des rappels de soins et des messages concernant sa santé tout au long de sa grossesse? +anc_register.step1.reminders.option.text=Oui +anc_register.step1.reminders.option.text=Non +anc_register.step1.reminders.required.err=Veuillez indiquer si la femme a accepté de recevoir des notifications de rappel +anc_register.step1.alt_name.hint=Nom de contact alternatif +anc_register.step1.alt_name.regex.err=Entrer un nom valide +anc_register.step1.alt_phone_number.hint=Autre numéro de téléphone de contact +anc_register.step1.alt_name.regex.err=Le numéro de téléphone doit être numérique + From ee81b40245c7aa5b5388f337aa22fc165a443848 Mon Sep 17 00:00:00 2001 From: Vincent Karuri Date: Mon, 2 Mar 2020 17:30:07 +0300 Subject: [PATCH 18/20] Code clean-up --- .../com/vijay/jsonwizard/interactors/JsonFormInteractor.java | 1 - .../java/com/vijay/jsonwizard/interfaces/FormWidgetFactory.java | 1 + ...NativeFomLangUtilsTest.java => NativeFormLangUtilsTest.java} | 2 +- 3 files changed, 2 insertions(+), 2 deletions(-) rename android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/{NativeFomLangUtilsTest.java => NativeFormLangUtilsTest.java} (98%) diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/interactors/JsonFormInteractor.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/interactors/JsonFormInteractor.java index 91fadad1c..ca66bea1b 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/interactors/JsonFormInteractor.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/interactors/JsonFormInteractor.java @@ -68,7 +68,6 @@ public JsonFormInteractor(@Nullable Map additionalWid } } - public static JsonFormInteractor getInstance(@Nullable Map additionalWidgetsMap) { if (INSTANCE == null) { INSTANCE = new JsonFormInteractor(additionalWidgetsMap); diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/interfaces/FormWidgetFactory.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/interfaces/FormWidgetFactory.java index 4872ec7fb..f4207de53 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/interfaces/FormWidgetFactory.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/interfaces/FormWidgetFactory.java @@ -13,6 +13,7 @@ * Created by vijay on 24-05-2015. */ public interface FormWidgetFactory { + List getViewsFromJson(String stepName, Context context, JsonFormFragment formFragment, JSONObject jsonObject, CommonListener listener, boolean popup) throws Exception; List getViewsFromJson(String stepName, Context context, JsonFormFragment formFragment, JSONObject jsonObject, CommonListener listener) throws Exception; diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/NativeFomLangUtilsTest.java b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/NativeFormLangUtilsTest.java similarity index 98% rename from android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/NativeFomLangUtilsTest.java rename to android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/NativeFormLangUtilsTest.java index 91b9f7ebc..71f8a2400 100644 --- a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/NativeFomLangUtilsTest.java +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/NativeFormLangUtilsTest.java @@ -19,7 +19,7 @@ /** * Created by Vincent Karuri on 20/02/2020 */ -public class NativeFomLangUtilsTest { +public class NativeFormLangUtilsTest { @Test public void testJsonFormTranslationShouldTranslateForm() { From dd9c85fd8ba8cbedc1e7cc60ada916561d47c52e Mon Sep 17 00:00:00 2001 From: Vincent Karuri Date: Mon, 9 Mar 2020 11:03:49 +0300 Subject: [PATCH 19/20] Fix failing test --- .../java/com/vijay/jsonwizard/utils/NativeFormLangUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/NativeFormLangUtils.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/NativeFormLangUtils.java index 9e1b1780c..66f0906d8 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/NativeFormLangUtils.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/NativeFormLangUtils.java @@ -72,7 +72,7 @@ public static String getTranslatedString(String str) { * @return */ public static String getTranslationsFileName(String str) { - Pattern propertiesFileNamePattern = Pattern.compile("\"?properties_file_name\"?:\"([a-zA-Z_0-9\\.]+)\""); + Pattern propertiesFileNamePattern = Pattern.compile("\"?properties_file_name\"?: ?\"([a-zA-Z_0-9\\.]+)\""); Matcher matcher = propertiesFileNamePattern.matcher(str); return matcher.find() ? matcher.group(1) : ""; } From 198d9ceff13775210bb17e93d9c28986127f6089 Mon Sep 17 00:00:00 2001 From: Vincent Karuri Date: Fri, 13 Mar 2020 16:44:27 +0300 Subject: [PATCH 20/20] Bump lib version --- gradle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index 3e858c2d3..a7799d056 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -VERSION_NAME=1.7.24-SNAPSHOT +VERSION_NAME=1.7.25-SNAPSHOT VERSION_CODE=1 GROUP=org.smartregister POM_SETTING_DESCRIPTION=OpenSRP Client Native Form Json Wizard