1

这篇文章中,我读到您可以将后处理功能添加到i18next

i18n.addPostProcessor("myProcessorsName", function(value, key, options) 
{
   return 'some post processed data based on translated value';
});

并在初始化期间添加它:

i18n.init({ postProcess: 'myProcessorsName' });

但我得到一个错误addPostProcessor不是函数。

那么如何添加和使用后处理功能i18next呢?

4

1 回答 1

3

文档中我认为您可以创建一个后期处理模块并将其添加到i18next带有use().

在此示例中,后处理模块将大写返回的任何字符串的第一个字母:

import i18next from "i18next";
import { initReactI18next } from "react-i18next";

(...)

const CapitalizeFirstLetter = (str) => {
  return str.length ? str.charAt(0).toUpperCase() + str.slice(1) : str
}

const initTranslations = () => {
    i18next
    .use({
      type: 'postProcessor',
      name: 'capitalize',
      process: function (value, key, options, translator) {
        return CapitalizeFirstLetter(value);
      }
    })
    .use(initReactI18next) // passes i18n down to react-i18next
    .init({
      postProcess: ["capitalize"]
    })
}
于 2019-09-05T08:55:49.300 回答