Created
August 20, 2019 13:26
-
-
Save hediet/4599b13332d43beec8b47f2c8c2de266 to your computer and use it in GitHub Desktop.
I18n.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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