Initial commit
This commit is contained in:
21
packages/i18n/package.json
Normal file
21
packages/i18n/package.json
Normal file
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"name": "@dwellops/i18n",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"exports": {
|
||||
".": {
|
||||
"types": "./src/index.ts",
|
||||
"default": "./src/index.ts"
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
"typecheck": "tsc --noEmit"
|
||||
},
|
||||
"devDependencies": {
|
||||
"typescript": "catalog:",
|
||||
"@types/node": "catalog:",
|
||||
"next-intl": "catalog:",
|
||||
"@dwellops/config": "workspace:*"
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
10
packages/i18n/tsconfig.json
Normal file
10
packages/i18n/tsconfig.json
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"extends": "@dwellops/config/tsconfig/base.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "./dist",
|
||||
"rootDir": "./src",
|
||||
"noEmit": true,
|
||||
"lib": ["ES2022", "DOM"]
|
||||
},
|
||||
"include": ["src"]
|
||||
}
|
||||
1
packages/i18n/tsconfig.tsbuildinfo
Normal file
1
packages/i18n/tsconfig.tsbuildinfo
Normal file
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user