Initial commit
This commit is contained in:
31
packages/i18n/src/format.ts
Normal file
31
packages/i18n/src/format.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
/**
|
||||
* Formats a date using the Intl.DateTimeFormat API.
|
||||
*
|
||||
* @param date - The date to format.
|
||||
* @param locale - The locale to use.
|
||||
* @param options - Optional Intl.DateTimeFormatOptions.
|
||||
* @returns The formatted date string.
|
||||
*/
|
||||
export function formatDate(
|
||||
date: Date | string,
|
||||
locale: string,
|
||||
options?: Intl.DateTimeFormatOptions,
|
||||
): string {
|
||||
const d = typeof date === 'string' ? new Date(date) : date;
|
||||
return new Intl.DateTimeFormat(locale, options).format(d);
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats a number as a currency string.
|
||||
*
|
||||
* @param amount - The amount to format.
|
||||
* @param currency - The ISO 4217 currency code.
|
||||
* @param locale - The locale to use.
|
||||
* @returns The formatted currency string.
|
||||
*/
|
||||
export function formatCurrency(amount: number, currency: string, locale: string): string {
|
||||
return new Intl.NumberFormat(locale, {
|
||||
style: 'currency',
|
||||
currency,
|
||||
}).format(amount);
|
||||
}
|
||||
11
packages/i18n/src/index.ts
Normal file
11
packages/i18n/src/index.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
/**
|
||||
* @dwellops/i18n — shared internationalization utilities.
|
||||
*
|
||||
* Translation files live alongside components as `translations.json`.
|
||||
* A shared `common.json` holds truly cross-cutting strings.
|
||||
* The `scripts/aggregate-translations.ts` script compiles them into
|
||||
* per-locale message files for next-intl.
|
||||
*/
|
||||
|
||||
export * from './locales';
|
||||
export * from './format';
|
||||
17
packages/i18n/src/locales.ts
Normal file
17
packages/i18n/src/locales.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
/** All supported locale codes. */
|
||||
export const locales = ['en'] as const;
|
||||
|
||||
/** The default locale. */
|
||||
export const defaultLocale = 'en' as const;
|
||||
|
||||
/** Union type of all supported locale strings. */
|
||||
export type Locale = (typeof locales)[number];
|
||||
|
||||
/**
|
||||
* Returns true if the provided string is a supported locale.
|
||||
*
|
||||
* @param value - The string to check.
|
||||
*/
|
||||
export function isLocale(value: string): value is Locale {
|
||||
return (locales as readonly string[]).includes(value);
|
||||
}
|
||||
Reference in New Issue
Block a user