如何将 postProcess 与 i18next 一起使用?

Att*_*que 1 internationalization i18next react-native react-i18next

这篇文章中我读到您可以添加后处理函数i18next

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

并在初始化时添加:

i18n.init({ postProcess: 'myProcessorsName' });
Run Code Online (Sandbox Code Playgroud)

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

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

Att*_*que 5

文档中我认为您可以创建一个后处理模块并将其添加到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"]
    })
}
Run Code Online (Sandbox Code Playgroud)