I am working on an application where we have a number of languages. We are not using a package like 'i18n' but instead -- since the number of string literals isn't so large -- using a system like this:
// constants
const GREETING = 'greeting'
// es.js -- a language object
const aLanguageObject = {[GREETING] : '¿Cómo estás?'}
// helper function
const getText(selector) = currentLanguageObject[selector]
// usage file
const greetingPhrase = getText(GREETING);
where currentLanguageObject
is one of es.js
, en.js
, etc, that is, one of the languageObject
s already defined (we select based on a process.env
value)
We'd like to be able to make use of tagged template literals to interpolate values into these template literals saved as constants.
I've tried to make tagged template literals work with our system like this:
// en.js
const aLanguageObject = {[CONFIRM_NUMBER] : `is the number ${0} correct?`}
and then use tagged templates like this
const theNumber = 12;
const myTag = (strings, numberExp) => `${strings[0]}${theNumber}${strings[1]}`;
const numberConfirmString = myTag`${getText(CONFIRM_NUMBER)})`;
but numberConfirmString is undefined
.
Is there a way to "fill in" template literals saved as constants?