When working with typescript, the usual way to do string interpolation is like this:
const name = "Jack"
const message = `Hello ${name}`;
This method allows for type checking, ensuring that if "name" does not exist, a compilation error will be triggered. However, in my application, I have multiple languages which necessitates a different approach (currently using Mustache for string formatting, but open to alternatives):
// strings are sourced externally, such as from a JSON file:
strings = {
"message": "Hello {{name}}"
}
const name = "Jack";
const message = Mustache.render(strings.message, { name });
While this solution works, I am seeking a way to implement static checks for used parameters (e.g., triggering a compilation error if the parameter name
is missing in any of the language instances within strings.message
). Is there a method to achieve this? I came across a library that parses SQL in a type-safe manner:
https://github.com/codemix/ts-sql
Could a similar approach be applied for handling string interpolation in multiple languages? Any advice or existing tools/libraries/practices available for this scenario?