Skip to content

Instantly share code, notes, and snippets.

@hediet

hediet/I18n.ts

Created Aug 20, 2019
Embed
What would you like to do?
I18n.ts
interface FormatDescriptor {
id: string;
defaultTemplate?: string;
}
export type Formatted =
| { kind: 'text'; value: string }
| { kind: 'sequence'; items: Formatted[] }
| { kind: 'object'; items: Record<string, Formatted> };
export interface I18nService {
format<TData extends Record<string, unknown>>(
descriptor: FormatDescriptor,
data: TData
): Formatted;
}
const service: I18nService = null!;
const count = 10;
service.format(
{
id: 'items',
defaultTemplate: `There {
pluralCat($count).match({
singular: "is one {$Link("item")}",
other: "are {numToWords($count)} {$Link("items")}"
})
}`,
},
{ count, Link: (text: string) => '' }
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.