introduce TranslatedString type to require an string but exclude non-translated strings
This commit is contained in:
parent
590cda1dd3
commit
4da4a1b33e
@ -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/);
|
||||
|
Loading…
Reference in New Issue
Block a user