Skip to content

Commit

Permalink
[mekomsolutions#221] Support translating AMPATH Form Names via ampath…
Browse files Browse the repository at this point in the history
…formtranslations domain
  • Loading branch information
Ruhanga committed Mar 29, 2023
1 parent 8d143ca commit 32a63f0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@
import java.io.File;
import java.util.Map;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.LocaleUtils;
import org.apache.commons.lang3.StringUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.openmrs.Form;
import org.openmrs.FormResource;
import org.openmrs.api.FormService;
import org.openmrs.api.context.Context;
import org.openmrs.messagesource.PresentationMessage;
import org.openmrs.module.initializer.Domain;
import org.openmrs.module.initializer.InitializerMessageSource;
import org.openmrs.module.initializer.api.ConfigDirUtil;
import org.openmrs.module.initializer.api.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import java.nio.charset.StandardCharsets;

Expand All @@ -20,6 +26,10 @@ public class AmpathFormsTranslationsLoader extends BaseFileLoader {
@Autowired
private FormService formService;

@Autowired
@Qualifier("initializer.InitializerMessageSource")
private InitializerMessageSource msgSource;

@Override
protected Domain getDomain() {
return Domain.AMPATH_FORMS_TRANSLATIONS;
Expand Down Expand Up @@ -71,5 +81,20 @@ protected void load(File file) throws Exception {
formResource.setDatatypeClassname("org.openmrs.customdatatype.datatype.LongFreeTextDatatype");
formResource.setValue(jsonTranslationsString);
formService.saveFormResource(formResource);

String formNameTranslation = (String) jsonTranslationsDefinition.get("form_name_translation");
if (!StringUtils.isBlank(formNameTranslation)) {
msgSource.addPresentation(new PresentationMessage(
"ui.i18n." + Utils.unProxy(form.getClass().getSimpleName()) + ".name." + form.getUuid(),
LocaleUtils.toLocale(language), formNameTranslation, null));
msgSource.addPresentation(new PresentationMessage(
"org.openmrs." + Utils.unProxy(form.getClass().getSimpleName()) + "." + form.getUuid(),
LocaleUtils.toLocale(language), formNameTranslation, null));
}
}

@Override
public ConfigDirUtil getDirUtil() {
return new ConfigDirUtil(iniz.getConfigDirPath(), iniz.getChecksumsDirPath(), getDomainName(), true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.junit.rules.ExpectedException;
import org.openmrs.FormResource;
import org.openmrs.api.FormService;
import org.openmrs.api.context.Context;
import org.openmrs.module.initializer.DomainBaseModuleContextSensitiveTest;
import org.openmrs.module.initializer.api.loaders.AmpathFormsLoader;
import org.openmrs.module.initializer.api.loaders.AmpathFormsTranslationsLoader;
Expand All @@ -20,6 +21,7 @@
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.Locale;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class AmpathFormsTranslationsLoaderIntegrationTest extends DomainBaseModuleContextSensitiveTest {
Expand Down Expand Up @@ -71,6 +73,12 @@ public void load_shouldLoadAFormTranslationsFileWithAllAttributesSpecifiedAsForm
Assert.assertEquals("Autre", actualObj.get("translations").get("Other").getTextValue());
Assert.assertEquals("Enfant", actualObj.get("translations").get("Child").getTextValue());

// verify form name translation
Assert.assertEquals("Formulaire d'essai 1", Context.getMessageSourceService()
.getMessage("ui.i18n.Form.name." + formResource.getForm().getUuid(), null, Locale.CANADA_FRENCH));
Assert.assertEquals("Formulaire d'essai 1", Context.getMessageSourceService()
.getMessage("org.openmrs.Form." + formResource.getForm().getUuid(), null, Locale.CANADA_FRENCH));

}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"uuid" : "c5bf3efe-3798-4052-8dcb-09aacfcbabdc",
"form" : "Test Form 1",
"form_name_translation": "Formulaire d'essai 1",
"description" : "French Translations",
"language" : "fr",
"translations" : {
Expand Down

0 comments on commit 32a63f0

Please sign in to comment.