The code below (a localization interceptor for an Alexa skill) works fine with i18next version 10.5.0 but doesn't work in the latest versions. It get a message of function t not being recognized, it would seem t is not binding correctly.
I can't find why this is happening (I don't know what was updated in i18next). Can anybody shed some light on this?
// This request interceptor will bind a translation function 't' to the requestAttributes object
const LocalizationInterceptor = {
process(handlerInput) {
const localizationClient = i18n.use(sprintf).init({
lng: handlerInput.requestEnvelope.request.locale,
fallbackLng: 'en',
overloadTranslationOptionHandler: sprintf.overloadTranslationOptionHandler,
resources: languageStrings,
returnObjects: true
});
const attributes = handlerInput.attributesManager.getRequestAttributes();
attributes.t = function (...args) {
return localizationClient.t(...args);
}
}
}
Seems like t has to be handled as a continuation in a Promise in the latest versions so here's the solution:
// This request interceptor will bind a translation function 't' to the requestAttributes.
const LocalizationRequestInterceptor = {
process(handlerInput) {
i18n.use(sprintf).init({
lng: handlerInput.requestEnvelope.request.locale,
resources: languageStrings,
overloadTranslationOptionHandler: sprintf.overloadTranslationOptionHandler,
}).then((t) => {
const attributes = handlerInput.attributesManager.getRequestAttributes();
attributes.t = (...args) => t(...args);
});
},
};