Skip to content

Instantly share code, notes, and snippets.

@hediet
Created August 20, 2019 13:26
Show Gist options
  • Save hediet/4599b13332d43beec8b47f2c8c2de266 to your computer and use it in GitHub Desktop.
Save hediet/4599b13332d43beec8b47f2c8c2de266 to your computer and use it in GitHub Desktop.
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