introduce TranslatedString type to require an string but exclude non-translated strings

This commit is contained in:
Sebastian 2023-01-04 15:45:38 -03:00
parent 590cda1dd3
commit 4da4a1b33e
No known key found for this signature in database
GPG Key ID: BE4FF68352439FC1

View File

@ -10,7 +10,7 @@ export let jed: any = undefined;
* Set up jed library for internationalization, * Set up jed library for internationalization,
* based on browser language settings. * 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("_", "-"); lang = lang.replace("_", "-");
if (!strings[lang]) { if (!strings[lang]) {
@ -28,10 +28,13 @@ export function internalSetStrings(langStrings: any): void {
jed = new jedLib.Jed(langStrings); jed = new jedLib.Jed(langStrings);
} }
declare const __translated: unique symbol;
export type TranslatedString = string & { [__translated]: true };
/** /**
* Convert template strings to a msgid * Convert template strings to a msgid
*/ */
function toI18nString(stringSeq: ReadonlyArray<string>): string { function toI18nString(stringSeq: ReadonlyArray<string>): TranslatedString {
let s = ""; let s = "";
for (let i = 0; i < stringSeq.length; i++) { for (let i = 0; i < stringSeq.length; i++) {
s += stringSeq[i]; s += stringSeq[i];
@ -39,7 +42,7 @@ function toI18nString(stringSeq: ReadonlyArray<string>): string {
s += `%${i + 1}$s`; s += `%${i + 1}$s`;
} }
} }
return s; return s as TranslatedString;
} }
/** /**
@ -48,7 +51,7 @@ function toI18nString(stringSeq: ReadonlyArray<string>): string {
export function singular( export function singular(
stringSeq: TemplateStringsArray, stringSeq: TemplateStringsArray,
...values: any[] ...values: any[]
): string { ): TranslatedString {
const s = toI18nString(stringSeq); const s = toI18nString(stringSeq);
const tr = jed const tr = jed
.translate(s) .translate(s)
@ -63,10 +66,10 @@ export function singular(
export function translate( export function translate(
stringSeq: TemplateStringsArray, stringSeq: TemplateStringsArray,
...values: any[] ...values: any[]
): any[] { ): TranslatedString[] {
const s = toI18nString(stringSeq); const s = toI18nString(stringSeq);
if (!s) return []; if (!s) return [];
const translation: string = jed.ngettext(s, s, 1); const translation: TranslatedString = jed.ngettext(s, s, 1);
return replacePlaceholderWithValues(translation, values); return replacePlaceholderWithValues(translation, values);
} }
@ -83,7 +86,7 @@ export function Translate({
const c = [].concat(children); const c = [].concat(children);
const s = stringifyArray(c); const s = stringifyArray(c);
if (!s) return []; if (!s) return [];
const translation: string = jed.ngettext(s, s, 1); const translation: TranslatedString = jed.ngettext(s, s, 1);
if (debug) { if (debug) {
console.log("looking for ", s, "got", translation); console.log("looking for ", s, "got", translation);
} }
@ -104,12 +107,12 @@ export function getJsonI18n<K extends string>(
export function getTranslatedArray(array: Array<any>) { export function getTranslatedArray(array: Array<any>) {
const s = stringifyArray(array); const s = stringifyArray(array);
const translation: string = jed.ngettext(s, s, 1); const translation: TranslatedString = jed.ngettext(s, s, 1);
return replacePlaceholderWithValues(translation, array); return replacePlaceholderWithValues(translation, array);
} }
function replacePlaceholderWithValues( function replacePlaceholderWithValues(
translation: string, translation: TranslatedString,
childArray: Array<any>, childArray: Array<any>,
): Array<any> { ): Array<any> {
const tr = translation.split(/%(\d+)\$s/); const tr = translation.split(/%(\d+)\$s/);