[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[taler-wallet-core] 02/02: introduce TranslatedString type to require an
From: |
gnunet |
Subject: |
[taler-wallet-core] 02/02: introduce TranslatedString type to require an string but exclude non-translated strings |
Date: |
Wed, 04 Jan 2023 19:46:43 +0100 |
This is an automated email from the git hooks/post-receive script.
sebasjm pushed a commit to branch master
in repository wallet-core.
commit 4da4a1b33e4c12ecabfb37947d84949d41db31e1
Author: Sebastian <sebasjm@gmail.com>
AuthorDate: Wed Jan 4 15:45:38 2023 -0300
introduce TranslatedString type to require an string but exclude
non-translated strings
---
packages/taler-util/src/i18n.ts | 21 ++++++++++++---------
1 file 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/);
--
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.