diff options
Diffstat (limited to 'packages/taler-util/src/i18n.ts')
-rw-r--r-- | packages/taler-util/src/i18n.ts | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/packages/taler-util/src/i18n.ts b/packages/taler-util/src/i18n.ts index f43f543ea..3c6f5ac57 100644 --- a/packages/taler-util/src/i18n.ts +++ b/packages/taler-util/src/i18n.ts @@ -60,6 +60,18 @@ export function singular( return tr; } +function withContext(ctx: string): typeof singular { + return function (t: TemplateStringsArray, ...v: any[]): TranslatedString { + const s = toI18nString(t); + const tr = jed + .translate(s) + .withContext(ctx) + .ifPlural(1, s) + .fetch(...v); + return tr; + }; +} + /** * Internationalize a string template without serializing */ @@ -79,14 +91,18 @@ export function translate( export function Translate({ children, debug, + context: ctx, }: { children: any; debug?: boolean; + context?: string; }): any { const c = [].concat(children); const s = stringifyArray(c); if (!s) return []; - const translation: TranslatedString = jed.ngettext(s, s, 1); + const translation: TranslatedString = ctx + ? jed.npgettext(ctx, s, s, 1) + : jed.ngettext(s, s, 1); if (debug) { console.log("looking for ", s, "got", translation); } @@ -156,6 +172,7 @@ function stringifyArray(children: Array<any>): string { export const i18n = { str: singular, + ctx: withContext, singular, Translate, translate, |