Функция t не работает правильно в последних версиях i18 - PullRequest
0 голосов
/ 31 января 2019

Приведенный ниже код (перехватчик локализации для навыка Alexa) прекрасно работает с версией i18next 10.5.0, но не работает в последних версиях.Он получает сообщение о том, что функция t не распознана, и кажется, что он неправильно привязывается.

Я не могу понять, почему это происходит (я не знаю, что было обновлено в i18next).Кто-нибудь может пролить свет на это?

// 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);
    }
  }
}

1 Ответ

0 голосов
/ 15 февраля 2019

Похоже, что t должен рассматриваться как продолжение в Promise в последних версиях, поэтому вот решение:

// 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);
        });
    },
};
...