Skip to content

Commit

Permalink
Translate election date and add to election packages (#4487)
Browse files Browse the repository at this point in the history
  • Loading branch information
arsalansufi authored Jan 8, 2024
1 parent a2eac72 commit 48c7fac
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
17 changes: 16 additions & 1 deletion apps/design/backend/src/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
District,
DistrictId,
Election,
ElectionStringKey,
ElectionType,
LanguageCode,
Parties,
Expand Down Expand Up @@ -317,10 +318,11 @@ test('Election package export', async () => {
// Check overall structure of zip file

const zip = await JsZip.loadAsync(electionPackageContents);
expect(Object.keys(zip.files)).toEqual([
expect(Object.keys(zip.files).sort()).toEqual([
'appStrings.json',
'election.json',
'systemSettings.json',
'vxElectionStrings.json',
]);

// Check appStrings.json
Expand Down Expand Up @@ -360,6 +362,19 @@ test('Election package export', async () => {
}
}

// Check vxElectionStrings.json

const vxElectionStrings = safeParseJson(
await assertDefined(zip.file('vxElectionStrings.json')).async('text'),
UiStringsPackageSchema
).unsafeUnwrap();

for (const languageCode of Object.values(LanguageCode)) {
expect(
vxElectionStrings[languageCode]?.[ElectionStringKey.ELECTION_DATE]
).toBeDefined();
}

// Check election.json

const electionDefinition = safeParseElectionDefinition(
Expand Down
25 changes: 25 additions & 0 deletions apps/design/backend/src/worker/generate_election_package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ import { assertDefined } from '@votingworks/basics';
import { layOutAllBallotStyles } from '@votingworks/hmpb-layout';
import {
BallotType,
Election,
ElectionPackageFileName,
ElectionStringKey,
getDisplayElectionHash,
Id,
LanguageCode,
safeParseJson,
UiStringsPackage,
} from '@votingworks/types';
import { format } from '@votingworks/utils';

import { PORT } from '../globals';
import { GoogleCloudTranslator } from '../language_and_audio/translator';
Expand Down Expand Up @@ -55,6 +58,22 @@ async function translateAppStrings(
return appStrings;
}

function translateVxElectionStrings(election: Election): UiStringsPackage {
const electionDate = new Date(election.date);
const vxElectionStrings: UiStringsPackage = {};
for (const languageCode of Object.values(LanguageCode)) {
vxElectionStrings[languageCode] = {};
const electionDateInLanguage = format.localeLongDate(
electionDate,
languageCode
);
assertDefined(vxElectionStrings[languageCode])[
ElectionStringKey.ELECTION_DATE
] = electionDateInLanguage;
}
return vxElectionStrings;
}

export async function generateElectionPackage(
{ translator, workspace }: WorkerContext,
{ electionId }: { electionId: Id }
Expand All @@ -72,6 +91,12 @@ export async function generateElectionPackage(
JSON.stringify(appStrings, null, 2)
);

const vxElectionStrings = translateVxElectionStrings(election);
zip.file(
ElectionPackageFileName.VX_ELECTION_STRINGS,
JSON.stringify(vxElectionStrings, null, 2)
);

const { electionDefinition } = layOutAllBallotStyles({
election,
// Ballot type and ballot mode shouldn't change the election definition, so it doesn't matter
Expand Down

0 comments on commit 48c7fac

Please sign in to comment.