Skip to content

Commit

Permalink
feat: localise validation messages
Browse files Browse the repository at this point in the history
  • Loading branch information
alaa-yahia committed Aug 27, 2024
1 parent 8584bee commit 2ac85b8
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 20 deletions.
57 changes: 55 additions & 2 deletions i18n/en.pot
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,64 @@ msgstr ""
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1)\n"
"POT-Creation-Date: 2024-06-24T08:00:25.810Z\n"
"PO-Revision-Date: 2024-06-24T08:00:25.811Z\n"
"POT-Creation-Date: 2024-08-27T05:56:11.774Z\n"
"PO-Revision-Date: 2024-08-27T05:56:11.774Z\n"

msgid "Couldn't find a monthly period for weekly period id \"{{periodId}}\""
msgstr "Couldn't find a monthly period for weekly period id \"{{periodId}}\""

msgid "Couldn't find a weekly period for weekly period id \"{{periodId}}\""
msgstr "Couldn't find a weekly period for weekly period id \"{{periodId}}\""

msgid "Couldn't handle unknown period id \"{{periodId}}\""
msgstr "Couldn't handle unknown period id \"{{periodId}}\""

msgid "Could not find a period type for period id \"{{periodId}}\""
msgstr "Could not find a period type for period id \"{{periodId}}\""

msgid "Could not determine a period type for period id \"{{periodId}}\""
msgstr "Could not determine a period type for period id \"{{periodId}}\""

msgid "Couldn't find a period type for weekly period id \"{{periodId}}\""
msgstr "Couldn't find a period type for weekly period id \"{{periodId}}\""

msgid "Bi-Week"
msgstr "Bi-Week"

msgid "Week"
msgstr "Week"

msgid "Date string is invalid, received \"{{dateString}}\""
msgstr "Date string is invalid, received \"{{dateString}}\""

msgid ""
"Date string format does not match the specified format. Expected {{format}} "
"but got {{detectedFormat}}."
msgstr ""
"Date string format does not match the specified format. Expected {{format}} "
"but got {{detectedFormat}}."

msgid "Unrecognized date, received \"{{date}}\""
msgstr "Unrecognized date, received \"{{date}}\""

msgid "Year {{year}} is out of range."
msgstr "Year {{year}} is out of range."

msgid "Month {{month}} is out of range | 1 <= {{month}} <= 12."
msgstr "Month {{month}} is out of range | 1 <= {{month}} <= 12."

msgid "Day {{day}} is out of range | 1 <= {{day}} <= {{daysInMonth}}."
msgstr "Day {{day}} is out of range | 1 <= {{day}} <= {{daysInMonth}}."

msgid "Date is not given"
msgstr "Date is not given"

msgid "Date {{dateString}} is less than the minimum allowed date {{minDateString}}."
msgstr "Date {{dateString}} is less than the minimum allowed date {{minDateString}}."

msgid ""
"Date {{dateString}} is greater than the maximum allowed date "
"{{maxDateString}}."
msgstr ""
"Date {{dateString}} is greater than the maximum allowed date "
"{{maxDateString}}."
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import i18n from '@dhis2/d2-i18n'
import { dhis2CalendarsMap } from '../../constants/dhis2CalendarsMap'
import { SupportedCalendar } from '../../types'
import { fromAnyDate, getCustomCalendarIfExists } from '../../utils/index'
Expand Down Expand Up @@ -70,7 +71,10 @@ const createFixedPeriodFromPeriodId: ParseFixedPeriodId = ({

if (!foundNextYear) {
throw new Error(
`Couldn't find a monthly period for weekly period id "${periodId}"`
i18n.t(
`Couldn't find a monthly period for weekly period id "{{periodId}}"`,
{ periodId }
)
)
}

Expand Down Expand Up @@ -110,7 +114,10 @@ const createFixedPeriodFromPeriodId: ParseFixedPeriodId = ({
}

throw new Error(
`Couldn't find a weekly period for weekly period id "${periodId}"`
i18n.t(
`Couldn't find a weekly period for weekly period id "{{periodId}}"`,
{ periodId }
)
)
}

Expand All @@ -123,7 +130,9 @@ const createFixedPeriodFromPeriodId: ParseFixedPeriodId = ({
return buildDailyFixedPeriod({ date, locale, calendar })
}

throw new Error(`Couldn't handle unknown period id "${periodId}"`)
throw new Error(
i18n.t(`Couldn't handle unknown period id "{{periodId}}"`, { periodId })
)
}

export default createFixedPeriodFromPeriodId
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import i18n from '@dhis2/d2-i18n'
import monthNumbers from '../month-numbers'
import {
regexMonthlyStandardPeriodId,
Expand Down Expand Up @@ -64,7 +65,10 @@ const getMonthlyFixedPeriodTypeForPeriodId: GetMonthlyFixedPeriodTypeForPeriodId
}

throw new Error(
`Could not find a period type for period id "${periodId}"`
i18n.t(
`Could not find a period type for period id "{{periodId}}"`,
{ periodId }
)
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import i18n from '@dhis2/d2-i18n'
import {
regexBiWeeklyPeriodId,
regexWeeklyOffsetPeriodId,
Expand All @@ -22,15 +23,21 @@ const getWeeklyFixedPeriodTypeForPeriodId = (periodId: string) => {

if (!periodTypes.includes(periodType)) {
throw new Error(
`Could not determine a period type for period id "${periodId}"`
i18n.t(
`Could not determine a period type for period id "{{periodId}}"`,
{ periodId }
)
)
}

return periodType
}

throw new Error(
`Couldn't find a period type for weekly period id "${periodId}"`
i18n.t(
`Couldn't find a period type for weekly period id "{{periodId}}"`,
{ periodId }
)
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import i18n from '@dhis2/d2-i18n'
import monthNumbers from '../month-numbers'
import {
regexYearlyStandardPeriodId,
Expand Down Expand Up @@ -27,7 +28,11 @@ const getYearlyFixedPeriodTypeForPeriodId = (periodId: string): PeriodType => {
return `FY${month}` as PeriodType
}

throw new Error(`Could not find a period type for period id "${periodId}"`)
throw new Error(
i18n.t(`Could not find a period type for period id "{{periodId}}"`, {
periodId,
})
)
}

export default getYearlyFixedPeriodTypeForPeriodId
12 changes: 10 additions & 2 deletions src/utils/extract-date-parts-from-date-string.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import i18n from '@dhis2/d2-i18n'
import { dateStringRegExp } from './date-string-regexp'

export function extractDatePartsFromDateString(
Expand All @@ -7,7 +8,11 @@ export function extractDatePartsFromDateString(
const parts = dateString.match(dateStringRegExp)

if (!parts) {
throw new Error(`Date string is invalid, received "${dateString}"`)
throw new Error(
i18n.t(`Date string is invalid, received "{{dateString}}"`, {
dateString,
})
)
}

let yearStr, monthStr, dayStr, detectedFormat
Expand All @@ -28,7 +33,10 @@ export function extractDatePartsFromDateString(

if (format && detectedFormat !== format.toUpperCase()) {
throw new Error(
`Date string format does not match the specified format. Expected ${format}, but got ${detectedFormat}`
i18n.t(
`Date string format does not match the specified format. Expected {{format}} but got {{detectedFormat}}.`,
{ format, detectedFormat }
)
)
}

Expand Down
3 changes: 2 additions & 1 deletion src/utils/from-any-date.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import i18n from '@dhis2/d2-i18n'
import { Temporal } from '@js-temporal/polyfill'
import { SupportedCalendar } from '../types'
import fromDateString from './from-date-string'
Expand Down Expand Up @@ -30,7 +31,7 @@ const fromAnyDate: FromAnyDate = ({ date, calendar }) => {
})
}

throw new Error(`Unrecognized date, received "${date}"`)
throw new Error(i18n.t(`Unrecognized date, received "{{date}}"`, { date }))
}

export default fromAnyDate
35 changes: 27 additions & 8 deletions src/utils/validate-date-string.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import i18n from '@dhis2/d2-i18n'
import { Temporal } from '@js-temporal/polyfill'
import { dhis2CalendarsMap } from '../constants/dhis2CalendarsMap'
import { NEPALI_CALENDAR_DATA } from '../custom-calendars/nepaliCalendarData'
Expand All @@ -10,14 +11,17 @@ function validateNepaliDate(year: number, month: number, day: number) {
if (!nepaliYearData) {
return {
isValid: false,
errorMessage: `Year ${year} is out of range.`,
errorMessage: i18n.t(`Year {{year}} is out of range.`, { year }),
}
}

if (month < 1 || month > 12) {
return {
isValid: false,
errorMessage: `Month ${month} is out of range: 1 <= ${month} <= 12.`,
errorMessage: i18n.t(
`Month {{month}} is out of range | 1 <= {{month}} <= 12.`,
{ month }
),
}
}

Expand All @@ -26,7 +30,10 @@ function validateNepaliDate(year: number, month: number, day: number) {
if (day < 1 || day > daysInMonth) {
return {
isValid: false,
errorMessage: `Day ${day} is out of range: 1 <= ${day} <= ${daysInMonth}.`,
errorMessage: i18n.t(
`Day {{day}} is out of range | 1 <= {{day}} <= {{daysInMonth}}.`,
{ day }
),
}
}

Expand Down Expand Up @@ -66,7 +73,7 @@ export function validateDateString(
try {
// Will throw if the format of the date is incorrect
if (!dateString) {
throw new Error(`Date is not given`)
throw new Error(i18n.t(`Date is not given`))
}
const dateParts = extractDatePartsFromDateString(dateString, format)

Expand Down Expand Up @@ -100,10 +107,16 @@ export function validateDateString(
if (Temporal.PlainDate.compare(date, minDate) < 0) {
if (strictValidation) {
throw new Error(
`Date ${dateString} is less than the minimum allowed date ${minDateString}.`
i18n.t(
`Date {{dateString}} is less than the minimum allowed date {{minDateString}}.`,
{ dateString, minDateString }
)
)
} else {
warningMessage = `Date ${dateString} is less than the minimum allowed date ${minDateString}.`
warningMessage = i18n.t(
`Date {{dateString}} is less than the minimum allowed date {{minDateString}}.`,
{ dateString, minDateString }
)
}
}
}
Expand All @@ -118,10 +131,16 @@ export function validateDateString(
if (Temporal.PlainDate.compare(date, maxDate) > 0) {
if (strictValidation) {
throw new Error(
`Date ${dateString} is greater than the maximum allowed date ${maxDateString}.`
i18n.t(
`Date {{dateString}} is greater than the maximum allowed date {{maxDateString}}.`,
{ dateString, maxDateString }
)
)
} else {
warningMessage = `Date ${dateString} is greater than the maximum allowed date ${maxDateString}.`
warningMessage = i18n.t(
`Date {{dateString}} is greater than the maximum allowed date {{maxDateString}}.`,
{ dateString, maxDateString }
)
}
}
}
Expand Down

0 comments on commit 2ac85b8

Please sign in to comment.