commit c9fdc5e9a1973c1fbd79b1a78f3366b86d61ce6c
parent 590073c50f2561ee10538fe0cfda5378a501bbd7
Author: Sebastian <sebasjm@gmail.com>
Date: Tue, 29 Oct 2024 12:52:50 -0300
add the context function
Diffstat:
1 file changed, 18 insertions(+), 1 deletion(-)
diff --git 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,