I am working on some localization module, I have enabled TypesScript Json modules for this, currently I am trying to achieve the following usage:
base.json:
{
"base": "",
"greeting": "{{name}}",
"extras": {
"array": ["{{city}}", 0]
}
}
en.json / nl.json
{
"base": "hello",
"greeting": "hello {{name}}",
"extras": {
"array": ["I live in {{city}}", 7]
}
}
I want all files to implement the base so I did the following
import base from "./locale/base.json";
import en from "./locale/en.json";
import nl from "./locale/nl.json";
type Language = keyof typeof availableLanguages;
const availableLanguages: Record<"en" | "nl", typeof base> = {
en,
nl,
} as const;
export const setLanguage = <T extends Language>(lang: T) => {
return availableLanguages[lang];
};
This is the bare minimum I got to work, however I want to get this usage:
const t = setLanguage("en")
t.base // hello
t.greeting({ name: "Daniell" }) // hello Daniell
t.extras.array({ city: "My city" }) // ["I live in My city", 7]
I was wondering how I can achieve something like this, I could use some help with specifically:
- If I were to generate typings from my base.json by extracting the mustache notations to it's own interface, how I should I structure those to map them to each path and would this be the best approach?
- How should I type the interface for the strings / arrays? since the example below wouldn't give me any intellisense
interface String {
(...args: any): any;
}