From 7e0e4f33e879f8f6d6c11920dca8665838c146a9 Mon Sep 17 00:00:00 2001 From: Richard Lindhout Date: Fri, 3 Jul 2020 19:12:46 +0200 Subject: [PATCH] Work around limitations of Typescript with an extra tuple --- example/src/translate.ts | 151 +++++++++++++++++++++------------------ 1 file changed, 81 insertions(+), 70 deletions(-) diff --git a/example/src/translate.ts b/example/src/translate.ts index 2a51098..1cb94f8 100644 --- a/example/src/translate.ts +++ b/example/src/translate.ts @@ -23,84 +23,94 @@ type TranslationsObject = { type SubscriberFunc = (n:number) => any; +type val = (...params: any[]) => T +type val1 = T +type TranslationInput = { + [group: string]: { + [key: string]: val | val1 + }, +} -export function createTranslations(t: TGroup, po: Options): TranslationsObject { - // subscribers with callbacks for external updates - let sb: SubscriberFunc[] = []; - - let o = po - // empty map for easier translations - let et:any = {} - - // convert - // { - // homeScreen:{ - // loginButton:{ - // nl: 'Inloggen, - // en: 'Sign in', - // } - // } - // } - // - // to =>> based on the current language - // - // homeScreen:{ - // loginButton:'Sign in', - // } - // } - - function gen() { - // loop translations groups - Object.keys(t).forEach(g => { - // easierTranslation map with the group in the key - et[g] = {} - - // loop translations inside group - // @ts-ignore - Object.keys(t[g]).forEach(k => { - // e.g. 'Sign in' +export function createTranslations(){ + return function >(t: TGroup, po: Options): TranslationsObject { + // subscribers with callbacks for external updates + let sb: SubscriberFunc[] = []; + + let o = po + // empty map for easier translations + let et: any = {} + + // convert + // { + // homeScreen:{ + // loginButton:{ + // nl: 'Inloggen, + // en: 'Sign in', + // } + // } + // } + // + // to =>> based on the current language + // + // homeScreen:{ + // loginButton:'Sign in', + // } + // } + + function gen() { + // loop translations groups + Object.keys(t).forEach(g => { + // easierTranslation map with the group in the key + et[g] = {} + + // loop translations inside group // @ts-ignore - let tv = t[g][k] - - // map them in easier translation map - et[g][k] = tv instanceof Function ? (...p: any) => { - let z = (tv as Function)(...p) - return z[o.language] || z[o.fallback] - } : tv[o.language] || tv[o.fallback] + Object.keys(t[g]).forEach(k => { + // e.g. 'Sign in' + // @ts-ignore + let tv = t[g][k] + + // map them in easier translation map + et[g][k] = tv instanceof Function ? (...p: any) => { + let z = (tv as Function)(...p) + return z[o.language] || z[o.fallback] + } : tv[o.language] || tv[o.fallback] + }) }) - }) - } + } - function setOptions(op: Options) { - o = op - gen() + function setOptions(op: Options) { + o = op + gen() - // call subscribers - sb.forEach((c: any) => c((p:number) => p + 1)); - } + // call subscribers + sb.forEach((c: any) => c((p: number) => p + 1)); + } + + // use hook + function use(): any { + let [, s] = R.useState(0); - // use hook - function use(): any { - let [, s] = R.useState(0); + // subscribe to changes + R.useEffect(() => { + sb.push(s); + return () => { + sb = sb.filter((f) => f !== s); + }; + }, [s]); - // subscribe to changes - R.useEffect(() => { - sb.push(s); - return () => { - sb = sb.filter((f) => f !== s); - }; - }, [s]); + // return easier translations object + return et; + } - // return easier translations object - return et; + gen() + return { + translations: et, + getOptions: () => o, + setOptions, + use, + }; } - gen() - return { - translations: et, - getOptions: () => o, - setOptions, - use, - }; } // first describe which languages are allowed/required (Typescript) @@ -122,7 +132,8 @@ function getBestLanguage(): typeof availableLanguages[number] | typeof fallback // create a translation object with your translations -const translate = createTranslations({ + +const translate = createTranslations()({ appScreen:{ yesText: { nl: 'Ja',