-
Notifications
You must be signed in to change notification settings - Fork 44
chore: [DHIS2-22063] Block pushes with hardcoded custom terminology in pot file #4729
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
Open
henrikmv
wants to merge
22
commits into
hv/feat/DHIS2-21635_display-custom-teminology
Choose a base branch
from
hv/chore/DHIS2-22063_custom-terminology-check-script
base: hv/feat/DHIS2-21635_display-custom-teminology
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+151
−4
Open
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
fae84f7
feat: add script to verify custom terminology in i18n files
henrikmv c467348
fix: update suggestions for plural custom labels in terminology verif…
henrikmv 92404d8
Merge branch 'hv/feat/DHIS2-21878_plural-custom-terminology-note-rela…
henrikmv d68ff83
fix: sonar qube
henrikmv 6d3fff4
feat: add i18n verification step to CI and pre push
henrikmv 1b7db8d
Merge branch 'hv/feat/DHIS2-21878_plural-custom-terminology-note-rela…
henrikmv 5f01004
fix: sonar qube
henrikmv 11c1a89
fix: (review) devin comments
henrikmv 7a64066
Merge branch 'hv/feat/DHIS2-21878_plural-custom-terminology-note-rela…
henrikmv e412d62
Merge branch 'hv/feat/DHIS2-21878_plural-custom-terminology-note-rela…
henrikmv 6b8a7f0
Merge branch 'hv/feat/DHIS2-21878_plural-custom-terminology-note-rela…
henrikmv fe44d3a
Merge remote-tracking branch 'origin/hv/feat/DHIS2-21635_display-cust…
henrikmv ea12524
Merge remote-tracking branch 'origin/hv/feat/DHIS2-21635_display-cust…
henrikmv bba852b
feat: update custom terminology checks for tracked entity attributes …
henrikmv ccbb92e
Merge branch 'hv/feat/DHIS2-21635_display-custom-teminology' into hv/…
henrikmv 0ba1d13
Merge remote-tracking branch 'origin/hv/feat/DHIS2-21635_display-cust…
henrikmv 3e6d486
fix: update allow list
henrikmv 7dd5dbf
fix: simplify error message in terminology verification
henrikmv f54aa00
fix: improve msgid extraction logic in terminology verification
henrikmv 2efdd28
fix: (review) remove sorting in terminology verification
henrikmv 58d9cf9
fix: (SonarQube) use modern syntax for accessing last entry in msgid …
henrikmv c63e67b
Merge branch 'hv/feat/DHIS2-21635_display-custom-teminology' into hv/…
henrikmv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| yarn linter:check && yarn tsc:check | ||
| yarn linter:check && yarn tsc:check && yarn i18n:verify |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| /* | ||
| * Verifies that i18n/en.pot contains no msgid where custom terminology | ||
| * appears outside a {{...}}-template placeholder. | ||
| * | ||
| * Custom terms (enrollment, event, program stage, note, relationship, attribute, | ||
| * organisation unit, follow-up) needs to be inside {{...}}-template placeholder | ||
| * to be overridden per program at runtime. | ||
|
|
||
| * Runs with `yarn i18n:verify` | ||
| */ | ||
| /* eslint-disable no-console */ | ||
|
|
||
| import { readFileSync } from 'node:fs'; | ||
| import path from 'node:path'; | ||
| import { fileURLToPath } from 'node:url'; | ||
|
|
||
| const POT = fileURLToPath(new URL('../i18n/en.pot', import.meta.url)); | ||
|
|
||
| const CUSTOM_TERMS = [ | ||
| { words: ['enrollment', 'enrolment'], suggestion: '{{enrollmentLabel}}' }, | ||
| { words: ['enrollments', 'enrolments'], suggestion: '{{enrollmentsLabel}}' }, | ||
| { words: ['event'], suggestion: '{{eventLabel}}' }, | ||
| { words: ['events'], suggestion: '{{eventsLabel}}' }, | ||
| { words: ['program stage'], suggestion: '{{programStageLabel}}' }, | ||
| { words: ['program stages'], suggestion: '{{programStagesLabel}}' }, | ||
| { words: ['note'], suggestion: '{{noteLabel}}' }, | ||
| { words: ['notes'], suggestion: '{{notesLabel}}' }, | ||
| { words: ['relationship'], suggestion: '{{relationshipLabel}}' }, | ||
| { words: ['relationships'], suggestion: '{{relationshipsLabel}}' }, | ||
| { words: ['attribute'], suggestion: '{{attributeLabel}}' }, | ||
| { words: ['attributes'], suggestion: '{{attributesLabel}}' }, | ||
| { words: ['organisation unit', 'org unit', 'organization unit', 'registering unit'], suggestion: '{{orgUnitLabel}}' }, | ||
| { words: ['follow-up', 'followup', 'follow up'], suggestion: '{{followUpLabel}}' }, | ||
| ]; | ||
|
|
||
| const FALLBACKS = new Set([ | ||
| 'enrollment', 'enrollments', | ||
| 'event', 'events', | ||
| 'program stage', 'program stages', | ||
| 'note', 'notes', | ||
| 'relationship', 'relationships', | ||
| 'attribute', 'attributes', | ||
| 'organisation unit', 'follow-up', | ||
| ]); | ||
|
|
||
| const ALLOWLIST = new Set([ | ||
| // "event program" = DHIS2 programType, not user's event terminology | ||
| 'This is not an event program or the metadata is corrupt. See log for details.', | ||
| // "event program" = DHIS2 programType, not user's event terminology | ||
| '{{programName}} is an event program and does not have {{enrollmentsLabel}}.', | ||
| // NoSelectionsInfoBox — only renders when no program is selected, so no program-specific label to use | ||
| 'Choose a program and organisation unit to see existing data and create new records.', | ||
| // ProgramList — program picker shows generic label rather than any single program's custom label | ||
| 'Some programs are being filtered by the chosen organisation unit', | ||
| ]); | ||
|
|
||
| function extractStrings(potContents) { | ||
| const header = /^(msgid|msgstr(?:\[\d+\])?) "(.*)"$/; | ||
| const continuation = /^"(.*)"$/; | ||
|
|
||
| const entries = []; | ||
| potContents.split('\n').forEach((line, idx) => { | ||
| const h = line.match(header); | ||
| if (h) { | ||
| entries.push({ kind: h[1], value: h[2], line: idx + 1 }); | ||
| return; | ||
| } | ||
| const c = line.match(continuation); | ||
| if (c && entries.length) entries.at(-1).value += c[1]; | ||
| }); | ||
|
|
||
| let currentMsgid = null; | ||
| return entries.flatMap(({ kind, value, line }) => { | ||
| if (kind === 'msgid') currentMsgid = value; | ||
| return value && currentMsgid ? [{ value, line, msgid: currentMsgid, kind }] : []; | ||
| }); | ||
| } | ||
|
|
||
| function findViolations(msgid) { | ||
| const stripped = msgid.replace(/\{\{[^{}]*\}\}/g, ''); | ||
| const hits = []; | ||
| for (const { words, suggestion } of CUSTOM_TERMS) { | ||
| for (const word of words) { | ||
| const re = new RegExp(String.raw`\b${word}\b`, 'i'); | ||
| if (re.test(stripped)) { | ||
| hits.push({ word, suggestion }); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| return hits; | ||
| } | ||
|
|
||
| const DIVIDER = '━'.repeat(72); | ||
|
|
||
| function reportViolations(violations) { | ||
| const relPot = path.relative(process.cwd(), POT); | ||
| console.error(`\n${DIVIDER}\n`); | ||
| for (const v of violations) { | ||
| console.error(` ${relPot}:${v.line}`); | ||
| console.error(` msgid: "${v.msgid}"`); | ||
| if (v.value !== v.msgid) console.error(` ${v.kind.padEnd(11)} "${v.value}"`); | ||
| for (const h of v.hits) { | ||
| console.error(` ✗ "${h.word}" → use ${h.suggestion}`); | ||
| } | ||
| console.error(''); | ||
| } | ||
| console.error(`\x1b[1;31m${violations.length} custom-terminology violation(s) in en.pot.\x1b[0m\n`); | ||
| console.error('Fix by wrapping the offending word in a custom-terminology template.\n'); | ||
| console.error('If a hit is a genuine exception, add the exact msgid to the ALLOWLIST'); | ||
| console.error('in scripts/verifyCustomTerminology.mjs.'); | ||
| console.error(`\n${DIVIDER}\n`); | ||
| } | ||
|
|
||
| function main() { | ||
| const strings = extractStrings(readFileSync(POT, 'utf8')); | ||
| const violations = strings | ||
| .filter(({ msgid, value }) => !ALLOWLIST.has(msgid) && !FALLBACKS.has(value)) | ||
| .map(({ value, line, msgid, kind }) => ({ msgid, value, line, kind, hits: findViolations(value) })) | ||
| .filter(({ hits }) => hits.length > 0); | ||
|
|
||
| if (violations.length === 0) { | ||
| console.log('i18n:verify — no custom-terminology violations in en.pot'); | ||
| return; | ||
| } | ||
|
|
||
| reportViolations(violations); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| main(); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.