Currently, my goal is to create a translation service that includes type checking for both tags and their corresponding placeholders. I have a TagList object that outlines the available tags along with a list of required placeholders for each translated string.
export const TagList = {
username_not_found: ['username']
};
In this setup, the key represents the tag name while the value consists of placeholders necessary in the translated text.
An example dictionary may look like this:
// Please note that the keys should be either numbers or strings, not like this unconventional format
const en: {[name: keyof (typeof TagList)]: string} = {
"username_not_found": "The user {username} does not exist"
}
The method used for translating tags functions as follows:
this.trans("username_not_found", {username: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6d1e0200080203082d08150c00270123242017110704462b2725">[email protected]</a>"});
My objective is to enable type checking and autocompletion within my IDE for placeholder objects to ensure all placeholders are correctly configured.
For instance:
// Incorrect: Missing "username" placeholder.
this.trans("username_not_found", {});
// Also incorrect: Using a nonexistent placeholder "foobar".
this.trans("username_not_found", {foobar: "42"});
// Correct:
this.trans("username_not_found", {username: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="295a46444c464748694e534a46515d474e05535452"></a>[email protected]"});
At present, I am utilizing keyof (typeof TagList)
as the argument type for tagName
. Although it currently works, I am seeking a way to deduce the structure of the second argument based on the first argument's value.
I aim to streamline the process by avoiding the need for multiple tag lists maintained simultaneously in various interfaces and objects.
Thank you for your assistance!