Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#248] Support for loading-time L10n of metadata names and descriptions based on Context's locale. #249

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.openmrs.module.initializer.api;

import org.openmrs.OpenmrsMetadata;
import org.openmrs.messagesource.MessageSourceService;

/**
* Extends the BaseLineProcessor by adding methods to retrieve name and description based
* on the configured name and description and translations of these if appropriate.
*/
public abstract class BaseMetadataLineProcessor<T extends OpenmrsMetadata> extends BaseLineProcessor<T> {

public static final String TRANSLATE = "_translate_";

protected MessageSourceService messageSourceService;

public BaseMetadataLineProcessor(MessageSourceService messageSourceService) {
super();
this.messageSourceService = messageSourceService;
}

/**
* @return the name defined in the current line, which can indicate if it should come from a message
* code
*/
public String getName(T metadata, CsvLine line) {
String name = line.getName(true);
if (TRANSLATE.equals(name)) {
String code = "ui.i18n." + metadata.getClass().getSimpleName() + ".name." + metadata.getUuid();
name = messageSourceService.getMessage(code);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What locale is going to be used here? My question is at a functional level, OpenMRS' default locale I would assume?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on the functioning of the unit tests, it is whatever is returned from Context.getLocale(), which will end up as LocaleUtility.getDefaultLocale(). This will ultimately be whatever is in the GP named "default_locale", and if this is either unset or can't be retrieved, will fall back to "en_GB"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does that describe the feature appropriately:

Support for loading-time L10n of metadata names and descriptions based on Context's locale.

?
It would need to be documented well of course, but is that functional intent?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but it raises a good point. We might want to change this and also be more explicit in the code. Because if someone were to decide to re-run an initializer load themselves (eg. we have an admin feature to allow this for development), then this will pick up their locale, rather than a standard system locale. So maybe the translation here should explicitly pass in the locale that is returned from LocaleUtility.getDefaultLocale(), so that this will always behave consistently in the same system. Then, the documentation can describe this as "...based on the system default locale (default_locale global property)" or something.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mseaton we all left this PR and thread hanging, what's your take? Should we resume reviewing it, or should be close it for now?

}
return name;
}

/**
* @return the description defined in the current line, which can indicate if it should come from a
* message code
*/
public String getDescription(T metadata, CsvLine line) {
String description = line.get(HEADER_DESC);
if (TRANSLATE.equals(description)) {
String code = "ui.i18n." + metadata.getClass().getSimpleName() + ".description." + metadata.getUuid();
description = messageSourceService.getMessage(code);
}
return description;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
import org.openmrs.Privilege;
import org.openmrs.api.EncounterService;
import org.openmrs.api.UserService;
import org.openmrs.module.initializer.api.BaseLineProcessor;
import org.openmrs.messagesource.MessageSourceService;
import org.openmrs.module.initializer.api.BaseMetadataLineProcessor;
import org.openmrs.module.initializer.api.CsvLine;
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;

@Component
public class EncounterTypeLineProcessor extends BaseLineProcessor<EncounterType> {
public class EncounterTypeLineProcessor extends BaseMetadataLineProcessor<EncounterType> {

protected static String HEADER_VIEW_PRIV = "view privilege";

Expand All @@ -25,17 +26,18 @@ public class EncounterTypeLineProcessor extends BaseLineProcessor<EncounterType>

@Autowired
public EncounterTypeLineProcessor(@Qualifier("encounterService") EncounterService encounterService,
@Qualifier("userService") UserService userService) {
super();
@Qualifier("userService") UserService userService,
@Qualifier("messageSourceService") MessageSourceService messageSourceService) {
super(messageSourceService);
this.service = encounterService;
this.userService = userService;
}

@Override
public EncounterType fill(EncounterType type, CsvLine line) throws IllegalArgumentException {

type.setName(line.getName(true));
type.setDescription(line.get(HEADER_DESC));
type.setName(getName(type, line));
type.setDescription(getDescription(type, line));
{
String privilegeId = line.get(HEADER_VIEW_PRIV);
if (!StringUtils.isEmpty(privilegeId)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,42 @@ public void load_shouldLoadEncounterTypesAccordingToCsvFiles() {
Context.getMessageSourceService().getMessage("org.openmrs.EncounterType." + uuid, null, localeKm));
}
}

@Test
public void load_shouldLoadEncounterTypesWithTranslatedNameAndDescriptionInEnglish() {

// Replay
loader.load();
{
EncounterType et = es.getEncounterTypeByUuid("e9be7307-6395-11ee-a0a0-0242ac120002");
Assert.assertNotNull(et);
Assert.assertEquals("English Encounter Type Name", et.getName());
Assert.assertEquals("English Encounter Type Description", et.getDescription());
}
{
EncounterType et = es.getEncounterTypeByUuid("d9fe17f4-639c-11ee-a0a0-0242ac120002");
Assert.assertNotNull(et);
Assert.assertEquals("English display", et.getName());
}
}

@Test
public void load_shouldLoadEncounterTypesWithTranslatedNameAndDescriptionInFrench() {

Context.setLocale(Locale.FRENCH);

// Replay
loader.load();
{
EncounterType et = es.getEncounterTypeByUuid("e9be7307-6395-11ee-a0a0-0242ac120002");
Assert.assertNotNull(et);
Assert.assertEquals("French Encounter Type Name", et.getName());
Assert.assertEquals("French Encounter Type Description", et.getDescription());
}
{
EncounterType et = es.getEncounterTypeByUuid("d9fe17f4-639c-11ee-a0a0-0242ac120002");
Assert.assertNotNull(et);
Assert.assertEquals("French display", et.getName());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
Uuid,Void/Retire,Name,Description,View privilege,Edit privilege,display:en,display:km_KH,_order:1000
,,Triage Encounter,An encounter for triaging patients.,,,Triage Encounter (translated),ទ្រីយ៉ាហ្គេនស៊ើរ,
aaa1a367-3047-4833-af27-b30e2dac9028,,Medical History Encounter,An interview about the patient medical history.,,,Medical History Encounter (translated),ប្រវត្តិសាស្រ្តវេជ្ជសាស្រ្ត,
439559c2-a3a4-4a25-b4b2-1a0299e287ee,,X-ray Encounter,An encounter during wich X-rays are performed on the patient.,Can: View X-ray encounter,Can: Edit X-ray encounter,,,
400d7e07-6de6-40ac-8611-dcce12408e71,,Foo Encounter,An foo encounter that should not be created.,Can: View foo encounter,,,,
bed6f0f6-ab07-481f-929f-3d26e6cb1138,TRUE,,,,,,,
,,Oncology Encounter,A new description for the oncology encounter.,Can: View oncology encounter,,,,
Uuid,Void/Retire,Name,Description,View privilege,Edit privilege,display:en,display:km_KH,_order:1000,display:fr
,,Triage Encounter,An encounter for triaging patients.,,,Triage Encounter (translated),ទ្រីយ៉ាហ្គេនស៊ើរ,,
aaa1a367-3047-4833-af27-b30e2dac9028,,Medical History Encounter,An interview about the patient medical history.,,,Medical History Encounter (translated),ប្រវត្តិសាស្រ្តវេជ្ជសាស្រ្ត,,
439559c2-a3a4-4a25-b4b2-1a0299e287ee,,X-ray Encounter,An encounter during wich X-rays are performed on the patient.,Can: View X-ray encounter,Can: Edit X-ray encounter,,,,
400d7e07-6de6-40ac-8611-dcce12408e71,,Foo Encounter,An foo encounter that should not be created.,Can: View foo encounter,,,,,
bed6f0f6-ab07-481f-929f-3d26e6cb1138,TRUE,,,,,,,,
,,Oncology Encounter,A new description for the oncology encounter.,Can: View oncology encounter,,,,,
e9be7307-6395-11ee-a0a0-0242ac120002,,_translate_,_translate_,,,,,,
d9fe17f4-639c-11ee-a0a0-0242ac120002,,_translate_,,,,English display,,,French display
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ metadata.healthcenter=Health center
metadata.healthcenter.description=This is the description of a health centre.
metadata.healthcenter.onlyInEnglish=Only defined in English
englishAndSpanishOnly=English
shouldOverride=Not Overridden
shouldOverride=Not Overridden
ui.i18n.EncounterType.name.e9be7307-6395-11ee-a0a0-0242ac120002=English Encounter Type Name
ui.i18n.EncounterType.description.e9be7307-6395-11ee-a0a0-0242ac120002=English Encounter Type Description
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
metadata.healthcenter=Clinique
metadata.healthcenter.description=Ceci est la description d'une clinique.
metadata.healthcenter.description.named=Ceci est la description de la clinique {0}.
greeting=Bonjour from Iniz
greeting=Bonjour from Iniz
ui.i18n.EncounterType.name.e9be7307-6395-11ee-a0a0-0242ac120002=French Encounter Type Name
ui.i18n.EncounterType.description.e9be7307-6395-11ee-a0a0-0242ac120002=French Encounter Type Description
Loading