Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions packages/use-i18n/src/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@ import IntlTranslationFormat from 'intl-messageformat'

// Deeply inspired by https://github.com/formatjs/formatjs/blob/7406e526a9c5666cee22cc2316dad1fa1d88697c/packages/intl-messageformat/src/core.ts

// TS does not include non standard API
// Intl.ListFormat in at TC39 stage 4 and is widely adopted in browsers
// So we expose homegrown types
// https://github.com/tc39/proposal-intl-list-format
export interface IntlListFormatOptions {
localeMatcher?: 'best fit' | 'lookup'
type?: 'conjunction' | 'disjunction' | 'unit'
style?: 'long' | 'short' | 'narrow'
}

declare abstract class IntlListFormat {
constructor(locales?: string | string[], options?: IntlListFormatOptions);

format: (items: string[]) => string;
}

interface BaseFormatters {
getNumberFormat(
...args: ConstructorParameters<typeof Intl.NumberFormat>
Expand All @@ -14,8 +30,8 @@ interface BaseFormatters {
...args: ConstructorParameters<typeof Intl.PluralRules>
): Intl.PluralRules
getListFormat(
...args: ConstructorParameters<typeof Intl.ListFormat>
): Intl.ListFormat
...args: ConstructorParameters<typeof IntlListFormat>
): IntlListFormat
}

function createFastMemoizeCache<V>(): Cache<string, V> {
Expand All @@ -40,8 +56,10 @@ const baseFormatters: BaseFormatters = {
cache: createFastMemoizeCache<Intl.DateTimeFormat>(),
strategy: strategies.variadic,
}),
getListFormat: memoize((...args) => new Intl.ListFormat(...args), {
cache: createFastMemoizeCache<Intl.ListFormat>(),
// @ts-expect-error we assume Intl.ListFormat exists in our current context
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
getListFormat: memoize((...args) => new Intl.ListFormat(...args) as IntlListFormat, {
cache: createFastMemoizeCache<IntlListFormat>(),
strategy: strategies.variadic,
}),
getNumberFormat: memoize((...args) => new Intl.NumberFormat(...args), {
Expand Down
8 changes: 5 additions & 3 deletions packages/use-i18n/src/usei18n.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ import ReactDOM from 'react-dom'
import 'intl-pluralrules'
import dateFormat, { FormatDateOptions } from './formatDate'
import unitFormat, { FormatUnitOptions } from './formatUnit'
import formatters from './formatters'
import formatters, { IntlListFormatOptions } from './formatters'

const LOCALE_ITEM_STORAGE = 'locale'

type PrimitiveType = string | number | boolean | null | undefined | Date;

type Translations = Record<string, string> & { prefix?: string }
type TranslationsByLocales = Record<string, Translations>
type TranslateFn = (key: string, context?: Record<string, PrimitiveType>) => string
Expand Down Expand Up @@ -59,7 +61,7 @@ interface Context {
dateFnsLocale?: Locale,
datetime: (date: Date | number, options?: Intl.DateTimeFormatOptions) => string,
formatDate: (value: Date | number | string, options: FormatDateOptions) => string,
formatList: (listFormat: string[], options?: Intl.ListFormatOptions) => string,
formatList: (listFormat: string[], options?: IntlListFormatOptions) => string,
formatNumber: (numb: number, options?: Intl.NumberFormatOptions) => string,
formatUnit: (value: number, options: FormatUnitOptions) => string,
loadTranslations: (namespace: string, load?: LoadTranslationsFn) => Promise<string>,
Expand Down Expand Up @@ -213,7 +215,7 @@ const I18nContextProvider = ({
)

const formatList = useCallback(
(listFormat: string[], options?: Intl.ListFormatOptions) =>
(listFormat: string[], options?: IntlListFormatOptions) =>
formatters.getListFormat(currentLocale, options).format(listFormat),
[currentLocale],
)
Expand Down
5 changes: 0 additions & 5 deletions types/global.d.ts

This file was deleted.

15 changes: 0 additions & 15 deletions types/intl.d.ts

This file was deleted.