How to keep strings out of server code


#1

Hy,

I’m starting to use log messages in the server.
The server messages use template scripts. I would like all these messages to defined in a single file, avoiding duplicated messages and making them all more standard. However, since there are variables included in the messages, that doesn’t seem like a simple task.

Example:
NOW:
let date = new Date();
let userName= “John”;
console.log(Started at ${date});
console.log(Goodbye ${userName});

WISH:
code file:
let date = new Date();
let userName= “John”;
console.log(MSG.start);
console.log(MSG.bye);

msg file:
const MSG= { start: “Started at ${date}”,
bye: “Goodbye ${userName}” }

This seem similar to a i18n problem. However i18n usually does not work on the server.

Any thoughts?

Thanks,


#2

We used a strategy similar to this: https://github.com/TAPevents/tap-i18n
We load up language files in json format from private folder at start time on server and push into db

The language file looks like this:

{
  "msg": "Complete on __"
}

And we would replace __ with the string at run time

You can also do this

{
  "msg": "Completed by __user__ on __time__"
}

And pass object to rendering method

#3

Yes, I was thinking more in something similar to Java format, but my question was more in the term if something (a package, an API) already exists for that.

Seems not!

Thanks!