Skip to content

Commit

Permalink
Merge pull request #1640 from dimagi/make-form-title-translatable
Browse files Browse the repository at this point in the history
Make form title translatable
  • Loading branch information
avazirna authored Dec 9, 2024
2 parents 6ec3433 + 4b90b5b commit 5e97d38
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public enum SubmitStatus {
@Column(name = "menu_session_id", updatable = false)
private String menuSessionId;

@Column(updatable = false)
@Setter
private String title;

@Column(name = "onequestionperscreen", updatable = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ public NewFormResponse getResponse(NewSessionRequestBean bean, String postUrl) t
bean.getRestoreAsCaseId(),
null,
formplayerRemoteInstanceFetcher,
bean.getWindowWidth()
bean.getWindowWidth(),
null
);

NewFormResponse response = getResponse(formSession);
Expand Down
28 changes: 25 additions & 3 deletions src/main/java/org/commcare/formplayer/session/FormSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import com.fasterxml.jackson.databind.ObjectMapper;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.commcare.core.interfaces.RemoteInstanceFetcher;
Expand All @@ -27,6 +28,7 @@
import org.commcare.session.CommCareSession;
import org.commcare.session.SessionFrame;
import org.commcare.suite.model.StackFrameStep;
import org.commcare.suite.model.Text;
import org.commcare.util.CommCarePlatform;
import org.commcare.util.engine.CommCareConfigEngine;
import org.javarosa.core.model.FormDef;
Expand All @@ -36,6 +38,7 @@
import org.javarosa.core.model.instance.FormInstance;
import org.javarosa.core.model.instance.TreeReference;
import org.javarosa.core.model.utils.DateUtils;
import org.javarosa.core.services.locale.Localization;
import org.javarosa.core.services.storage.StorageManager;
import org.javarosa.core.util.UnregisteredLocaleException;
import org.javarosa.engine.FunctionExtensions;
Expand Down Expand Up @@ -90,6 +93,7 @@ public class FormSession {
private boolean suppressAutosync;
private boolean shouldSkipFullFormValidation;
private String windowWidth;
private Text formTitleText;

public FormSession(SerializableFormSession session,
RestoreFactory restoreFactory,
Expand All @@ -108,7 +112,9 @@ public FormSession(SerializableFormSession session,

this.sandbox = restoreFactory.getSandbox();
this.windowWidth = windowWidth;

if (commCareSession != null && commCareSession.getCurrentEntry() != null) {
this.formTitleText = commCareSession.getCurrentEntry().getText();
}
this.formDef = formDefinitionService.getFormDef(this.session);

loadInstanceXml(this.formDef, session.getInstanceXml());
Expand Down Expand Up @@ -150,12 +156,14 @@ public FormSession(UserSqlSandbox sandbox,
String caseId,
@Nullable SessionFrame sessionFrame,
RemoteInstanceFetcher instanceFetcher,
String windowWidth) throws Exception {
String windowWidth,
Text formTitleText) throws Exception {
// use this.formDef to mutate (e.g., inject instance content, set callout handler)
this.formDef = formDef;
this.formTitleText = formTitleText;
this.session = new SerializableFormSession(
domain, appId, TableBuilder.scrubName(username), asUser, caseId,
postUrl, menuSessionId, formDef.getTitle(), oneQuestionPerScreen,
postUrl, menuSessionId, getLocalizedFormTitle(), oneQuestionPerScreen,
locale, inPromptMode, sessionData, functionContext
);
this.session.setFormDefinition(serializableFormDefinition);
Expand Down Expand Up @@ -183,6 +191,18 @@ public FormSession(UserSqlSandbox sandbox,
}
}

public void updateFormTitle() {
session.setTitle(getLocalizedFormTitle());
}

private String getLocalizedFormTitle() {
if (formTitleText == null) {
return formDef.getTitle();
} else {
return formTitleText.evaluate();
}
}

@Trace
private void setupJavaRosaObjects() {
formEntryModel = new FormEntryModel(formDef, FormEntryModel.REPEAT_STRUCTURE_LINEAR);
Expand Down Expand Up @@ -634,6 +654,8 @@ public void setIsAtFirstIndex(boolean isAtFirstIndex) {
public void changeLocale(String locale) {
session.setInitLang(locale);
formEntryController.setLanguage(locale);
Localization.setLocale(locale);
updateFormTitle();
}

public SerializableFormSession getSerializableSession() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ public FormSession getFormEntrySession(FormSendCalloutHandler formSendCalloutHan
session.getDomain(), sessionData, postUrl, session.getLocale(), session.getId(), null,
oneQuestionPerScreen, session.getAsUser(), session.getAppId(), null, formSendCalloutHandler,
storageFactory, false, null, new SessionFrame(sessionWrapper.getFrame()),
instanceFetcher, getWindowWidth());
instanceFetcher, getWindowWidth(), sessionWrapper.getCurrentEntry().getText());
}

public SessionWrapper getSessionWrapper() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ public void testClearCachesAfterFormSubmission() throws Exception {
queryData,
NewFormResponse.class);
}
assertEquals("Close", formResponse.getTitle());
assertEquals("Close again", formResponse.getTitle());

ExternalDataInstanceSource source = getInstanceSourceFromSession(
formResponse.getSessionId(), VirtualInstances.getRemoteReference("results"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public void testNavigationToFormEntryWithMultipleQueries() throws Exception {
CommandListResponseBean.class);

assertEquals(1, menuResponse.getCommands().length);
assertEquals("Dedupe Form", menuResponse.getCommands()[0].getDisplayText());
assertEquals("Followup Form", menuResponse.getCommands()[0].getDisplayText());

// verify the second query block to fetch the remote case was executed as well as the 3rd
// query block
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,17 @@ public void testInFormLocalization() throws Exception {

assert newFormResponse.getTree().length == 2;

assert newFormResponse.getTitle().equals("English Form 1");

assert newFormResponse.getTree()[0].getCaption().equals("I'm English");

assert newFormResponse.getTree()[1].getCaption().equals("English rules");

FormEntryResponseBean formResponse = this.changeLanguage("es",
newFormResponse.getSessionId());

assert formResponse.getTitle().equals("Spanish Form 1");

assert formResponse.getTree()[0].getCaption().equals("I'm Spanish");

assert formResponse.getTree()[1].getCaption().equals("No Spanish rules");
Expand All @@ -85,6 +89,8 @@ public void testFormLocalization() throws Exception {

assert newFormResponse.getTree().length == 2;

assert newFormResponse.getTitle().equals("English Form 1");

assert newFormResponse.getTree()[0].getCaption().equals("I'm English");

assert newFormResponse.getTree()[1].getCaption().equals("English rules");
Expand All @@ -94,6 +100,8 @@ public void testFormLocalization() throws Exception {

assert newFormResponseSpanish.getTree().length == 2;

assert newFormResponseSpanish.getTitle().equals("Spanish Form 1");

assert newFormResponseSpanish.getTree()[0].getCaption().equals("I'm Spanish");

assert newFormResponseSpanish.getTree()[1].getCaption().equals("No Spanish rules");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ forms.m0f0=Registration
forms.m1f0=Visit
forms.m1f1=Close
forms.m2f0=Visit after post
forms.m2f1=Close
forms.m2f1=Close again
forms.m2f2=Close Again
homescreen.title=Case Claim
lang.current=en
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ cchq.reports_last_updated_on=Reports last updated on
en=English
forms.m0f0=Registration Form
forms.m1f0=Followup Form
forms.m2f0=Dedupe Form
forms.m2f0=Followup Form
homescreen.title=Data Registry Test
lang.current=en
m0.case_long.case_name_1.header=Name
Expand Down

0 comments on commit 5e97d38

Please sign in to comment.