Skip to content

Commit

Permalink
Release 0.0.3 and working typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardLindhout committed Jul 3, 2020
1 parent 7e0e4f3 commit 9171765
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 242 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type TranslationLanguages = {
}

// create a translation object with your translations
const translate = createTranslations<TranslationLanguages>({
const translate = createTranslations<TranslationLanguages>()({
homeScreen:{
signIn: {
nl: 'yes',
Expand Down Expand Up @@ -119,7 +119,7 @@ function getBestLanguage(): typeof availableLanguages[number] | typeof fallback
return availableLanguages.find(al => deviceLanguage.startsWith(al)) || fallback
}

const translate = createTranslations<TranslationLanguages>({
const translate = createTranslations<TranslationLanguages>()({
// ........translations
}, {
language: getBestLanguage(),
Expand All @@ -145,7 +145,7 @@ const fallback = 'en'
function getBestLanguage(): typeof availableLanguages[number] | typeof fallback {
return availableLanguages.find(al => deviceLanguage.startsWith(al)) || fallback
}
const translate = createTranslations<TranslationLanguages>({
const translate = createTranslations<TranslationLanguages>()({
// ........translations
}, {
language: getBestLanguage(),
Expand Down
39 changes: 22 additions & 17 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
declare type val<T> = (...params: any[]) => T;
declare type val1<T> = T;
declare type Translations<T> = {
[group: string]: {
[key: string]: val<T> | val1<T>;
declare type CopyFunction<TFn, TR> = TFn extends (...a: infer A) => any ? (...a: A) => TR : string;
declare type ValueOf<T> = T[keyof T];
declare type Translations<TGroup> = {
[group in keyof TGroup]: {
[key in keyof ValueOf<TGroup>]: CopyFunction<ValueOf<TGroup>[key], string>;
};
};
declare type EasierTranslations<T> = {
declare type Options<TValue> = {
language: keyof TValue;
fallback: keyof TValue;
};
declare type TranslationsObject<TGroup, TValue> = {
translations: Translations<TGroup>;
use: () => Translations<TGroup>;
setOptions: (options: Options<TValue>) => any;
getOptions: () => Options<TValue>;
};
declare type val<T> = (...params: any[]) => T;
declare type val1<T> = T;
declare type TranslationInput<TValue> = {
[group: string]: {
[key: string]: val<string> | val1<string>;
[key: string]: val<TValue> | val1<TValue>;
};
};
declare type Options<T> = {
language: keyof T;
fallback: keyof T;
};
declare type TranslationsObject<T> = {
translations: EasierTranslations<T>;
use: () => EasierTranslations<T>;
setOptions: (options: Options<T>) => any;
getOptions: () => Options<T>;
declare type TranslationLanguagesInput = {
[language: string]: string;
};
export declare function createTranslations<T>(t: Translations<T>, po: Options<T>): TranslationsObject<T>;
export declare function createTranslations<TValue extends TranslationLanguagesInput>(): <TGroup extends TranslationInput<TValue>>(t: TGroup, po: Options<TValue>) => TranslationsObject<TGroup, TValue>;
export {};
72 changes: 37 additions & 35 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,45 @@
Object.defineProperty(exports, "__esModule", { value: true });
exports.createTranslations = void 0;
const R = require("react");
function createTranslations(t, po) {
let sb = [];
let o = po;
let et = {};
function gen() {
Object.keys(t).forEach(g => {
et[g] = {};
Object.keys(t[g]).forEach(k => {
let tv = t[g][k];
et[g][k] = tv instanceof Function ? (...p) => {
let z = tv(...p);
return z[o.language] || z[o.fallback];
} : tv[o.language] || tv[o.fallback];
function createTranslations() {
return function (t, po) {
let sb = [];
let o = po;
let et = {};
function gen() {
Object.keys(t).forEach(g => {
et[g] = {};
Object.keys(t[g]).forEach(k => {
let tv = t[g][k];
et[g][k] = tv instanceof Function ? (...p) => {
let z = tv(...p);
return z[o.language] || z[o.fallback];
} : tv[o.language] || tv[o.fallback];
});
});
});
}
function setOptions(op) {
o = op;
}
function setOptions(op) {
o = op;
gen();
sb.forEach((c) => c((p) => p + 1));
}
function use() {
let [, s] = R.useState(0);
R.useEffect(() => {
sb.push(s);
return () => {
sb = sb.filter((f) => f !== s);
};
}, [s]);
return et;
}
gen();
sb.forEach((c) => c(p => p + 1));
}
function use() {
let [, s] = R.useState(0);
R.useEffect(() => {
sb.push(s);
return () => {
sb = sb.filter((f) => f !== s);
};
}, [s]);
return et;
}
gen();
return {
translations: et,
getOptions: () => o,
setOptions,
use,
return {
translations: et,
getOptions: () => o,
setOptions,
use,
};
};
}
exports.createTranslations = createTranslations;
121 changes: 3 additions & 118 deletions example/src/translate.ts
Original file line number Diff line number Diff line change
@@ -1,117 +1,4 @@
import * as R from "react";

type CopyFunction<TFn, TR> = TFn extends (...a: infer A) => any ? (...a:A) => TR: string
type ValueOf<T> = T[keyof T];

type Translations<TGroup> = {
[group in keyof TGroup]: {
[key in keyof ValueOf<TGroup>]: CopyFunction<ValueOf<TGroup>[key], string>
}
}

type Options<TValue> = {
language: keyof TValue,
fallback: keyof TValue,
}

type TranslationsObject<TGroup, TValue> = {
translations: Translations<TGroup>,
use: () => Translations<TGroup>;
setOptions: (options: Options<TValue>) => any;
getOptions: () => Options<TValue>,
}


type SubscriberFunc = (n:number) => any;
type val<T> = (...params: any[]) => T
type val1<T> = T
type TranslationInput<TValue> = {
[group: string]: {
[key: string]: val<TValue> | val1<TValue>
},
}

export function createTranslations<TValue extends object>(){
return function <TGroup extends TranslationInput<TValue>>(t: TGroup, po: Options<TValue>): TranslationsObject<TGroup, TValue> {
// 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'
// @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<TValue>) {
o = op
gen()

// call subscribers
sb.forEach((c: any) => c((p: number) => p + 1));
}

// use hook
function use(): any {
let [, s] = R.useState<number>(0);

// subscribe to changes
R.useEffect(() => {
sb.push(s);
return () => {
sb = sb.filter((f) => f !== s);
};
}, [s]);

// return easier translations object
return et;
}

gen()
return {
translations: et,
getOptions: () => o,
setOptions,
use,
};
}
}
import {createTranslations} from 'react-ridge-translations'

// first describe which languages are allowed/required (Typescript)
type TranslationLanguages = {
Expand All @@ -128,17 +15,15 @@ function getBestLanguage(): typeof availableLanguages[number] | typeof fallback
return availableLanguages.find(al => deviceLanguage.startsWith(al)) || fallback
}




// create a translation object with your translations

const translate = createTranslations<TranslationLanguages>()({
appScreen:{
yesText: {
nl: 'Ja',
fr: 'Oui',
en: 'Yes',
yes: '',
why: '???',
},
welcomeText: ({ firstName }: { firstName: string }) => ({
nl: `Hallo ${firstName}`,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-ridge-translations",
"version": "0.0.2",
"version": "0.0.3",
"description": "react-ridge-translations is a type safe translation library without the struggle",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
Loading

0 comments on commit 9171765

Please sign in to comment.