lib: externalized messages support
@nodejs/ctc @nodejs/collaborators ...
externalizing the error and output messages in lib
- the approach taken here is derived from the template string based mechanism described here
- this is an alternative approach to the ICU based approach that I am working on in https://github.com/nodejs/node/pull/3413
This introduces a new internal/messages module that exports a template string tag function. The externalized messages are in internal/messages_en.js for now but can be moved to a different location.
Within the lib src, we can use this mechanism using tagged template strings:
const I18N = require('internal/messages');
const foo = 'testing';
throw new Error(I18N`Example error: ${foo}`);
In the messages_en.js
file, we'd have something like:
{
...,
"Example error: {0}": {
"key": "ERR000009"
},
...
}
The template string would expand into:
ERR000009: Example Error: testing
Later on, when we get into actually translating messages, we would have other message_{locale}.js files (or however else we'd like to separate it out) that would look something like:
{
...,
"Example error: {0}": {
"key": "ERR000009",
"value": "Ejemplo de error: {0}"
}
I'm not yet convinced this approach is ideal, but it's functional. There are a few gotchas with template strings currently that are less than ideal (e.g. line wrapping issues, lookup is a bit inefficient).
Getting this out now, tho, to solicit input on the approach.
This is still a work in progress