From e00cfb6926269861241af8826faab6c5bedad0fd Mon Sep 17 00:00:00 2001 From: Sam Becker Date: Wed, 14 May 2025 09:14:16 -0500 Subject: [PATCH] Simplify/document i18n contributor experience --- src/i18n/date.ts | 13 ------------- src/i18n/index.ts | 31 +++++++++++++++++++++++++------ src/utility/date.ts | 2 +- 3 files changed, 26 insertions(+), 20 deletions(-) delete mode 100644 src/i18n/date.ts diff --git a/src/i18n/date.ts b/src/i18n/date.ts deleted file mode 100644 index e596ff6b..00000000 --- a/src/i18n/date.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { enUS, id, ptBR, pt } from 'date-fns/locale'; -import { APP_LOCALE } from '@/app/config'; - -const getDateFnLocale = (locale: string) => { - switch (locale) { - case 'id-id': return id; - case 'pt-pt': return pt; - case 'pt-br': return ptBR; - default: return enUS; - } -}; - -export const DATE_FN_LOCALE = getDateFnLocale(APP_LOCALE); diff --git a/src/i18n/index.ts b/src/i18n/index.ts index 372c8a5c..e85b54ae 100644 --- a/src/i18n/index.ts +++ b/src/i18n/index.ts @@ -1,4 +1,6 @@ import US_EN from './locales/us-en'; +import { enUS, id, ptBR, pt } from 'date-fns/locale'; +import { APP_LOCALE } from '@/app/config'; export type I18N = typeof US_EN; @@ -6,7 +8,15 @@ export type I18NDeepPartial = { [key in keyof I18N]?: Partial; } -export const LOCALE_TEXT: Record< +/** + * Translation steps for contributors: + * 1. Create new file in the `src/i18n/locales` modeled on `us-en.ts`. + * 2. Add import to `localeTextImports` + * 3. Add date-fn locale to `getDateFnLocale` + * 4. Add translation credit to `README.md` if desired + */ + +const localeTextImports: Record< string, () => Promise > = { @@ -15,19 +25,28 @@ export const LOCALE_TEXT: Record< 'id-id': () => import('./locales/id-id').then(m => m.default), }; -export const getTextForLocale = async ( - locale = '', -): Promise => { +const getDateFnLocale = (locale: string) => { + switch (locale) { + case 'id-id': return id; + case 'pt-pt': return pt; + case 'pt-br': return ptBR; + default: return enUS; + } +}; + +export const getTextForLocale = async (locale: string): Promise => { const text = US_EN; - Object.entries(await LOCALE_TEXT[locale.toLocaleLowerCase()]?.() ?? {}) + Object.entries(await localeTextImports[locale.toLocaleLowerCase()]?.() ?? {}) .forEach(([key, value]) => { // Fall back to English for missing keys text[key as keyof I18N] = { - ...text[key as keyof I18N] as any, + ...text[key as keyof I18N], ...value as any, }; }); return text; }; + +export const DATE_FN_LOCALE = getDateFnLocale(APP_LOCALE); diff --git a/src/utility/date.ts b/src/utility/date.ts index ca7709f2..87bef9cd 100644 --- a/src/utility/date.ts +++ b/src/utility/date.ts @@ -1,7 +1,7 @@ import { parseISO, parse, format } from 'date-fns'; import { formatInTimeZone } from 'date-fns-tz'; import { Timezone } from './timezone'; -import { DATE_FN_LOCALE } from '@/i18n/date'; +import { DATE_FN_LOCALE } from '@/i18n'; const DATE_STRING_FORMAT_TINY = 'dd MMM yy'; const DATE_STRING_FORMAT_TINY_PLACEHOLDER = '00 000 00';