Simplify/document i18n contributor experience

This commit is contained in:
Sam Becker 2025-05-14 09:14:16 -05:00
parent a788ea9554
commit e00cfb6926
3 changed files with 26 additions and 20 deletions

View File

@ -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);

View File

@ -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<I18N[key]>;
}
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<I18NDeepPartial | undefined>
> = {
@ -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<I18N> => {
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<I18N> => {
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);

View File

@ -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';