aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian <sebasjm@gmail.com>2023-01-04 15:45:38 -0300
committerSebastian <sebasjm@gmail.com>2023-01-04 15:46:01 -0300
commit4da4a1b33e4c12ecabfb37947d84949d41db31e1 (patch)
treed73694810a4bf94a5b72e3f30e54b11a7257a9a1
parent590cda1dd366cf3347654119f04c8a4529698b2f (diff)
introduce TranslatedString type to require an string but exclude non-translated strings
-rw-r--r--packages/taler-util/src/i18n.ts21
1 files changed, 12 insertions, 9 deletions
diff --git a/packages/taler-util/src/i18n.ts b/packages/taler-util/src/i18n.ts
index 001735325..f43f543ea 100644
--- a/packages/taler-util/src/i18n.ts
+++ b/packages/taler-util/src/i18n.ts
@@ -10,7 +10,7 @@ export let jed: any = undefined;
* Set up jed library for internationalization,
* based on browser language settings.
*/
-export function setupI18n(lang: string, strings: { [s: string]: any }): any {
+export function setupI18n(lang: string, strings: { [s: string]: any }): void {
lang = lang.replace("_", "-");
if (!strings[lang]) {
@@ -28,10 +28,13 @@ export function internalSetStrings(langStrings: any): void {
jed = new jedLib.Jed(langStrings);
}
+declare const __translated: unique symbol;
+export type TranslatedString = string & { [__translated]: true };
+
/**
* Convert template strings to a msgid
*/
-function toI18nString(stringSeq: ReadonlyArray<string>): string {
+function toI18nString(stringSeq: ReadonlyArray<string>): TranslatedString {
let s = "";
for (let i = 0; i < stringSeq.length; i++) {
s += stringSeq[i];
@@ -39,7 +42,7 @@ function toI18nString(stringSeq: ReadonlyArray<string>): string {
s += `%${i + 1}$s`;
}
}
- return s;
+ return s as TranslatedString;
}
/**
@@ -48,7 +51,7 @@ function toI18nString(stringSeq: ReadonlyArray<string>): string {
export function singular(
stringSeq: TemplateStringsArray,
...values: any[]
-): string {
+): TranslatedString {
const s = toI18nString(stringSeq);
const tr = jed
.translate(s)
@@ -63,10 +66,10 @@ export function singular(
export function translate(
stringSeq: TemplateStringsArray,
...values: any[]
-): any[] {
+): TranslatedString[] {
const s = toI18nString(stringSeq);
if (!s) return [];
- const translation: string = jed.ngettext(s, s, 1);
+ const translation: TranslatedString = jed.ngettext(s, s, 1);
return replacePlaceholderWithValues(translation, values);
}
@@ -83,7 +86,7 @@ export function Translate({
const c = [].concat(children);
const s = stringifyArray(c);
if (!s) return [];
- const translation: string = jed.ngettext(s, s, 1);
+ const translation: TranslatedString = jed.ngettext(s, s, 1);
if (debug) {
console.log("looking for ", s, "got", translation);
}
@@ -104,12 +107,12 @@ export function getJsonI18n<K extends string>(
export function getTranslatedArray(array: Array<any>) {
const s = stringifyArray(array);
- const translation: string = jed.ngettext(s, s, 1);
+ const translation: TranslatedString = jed.ngettext(s, s, 1);
return replacePlaceholderWithValues(translation, array);
}
function replacePlaceholderWithValues(
- translation: string,
+ translation: TranslatedString,
childArray: Array<any>,
): Array<any> {
const tr = translation.split(/%(\d+)\$s/);