React native 如何在i18next中使用后处理?
据我所知,您可以将后处理功能添加到React native 如何在i18next中使用后处理?,react-native,internationalization,i18next,react-i18next,React Native,Internationalization,I18next,React I18next,据我所知,您可以将后处理功能添加到i18next: i18n.addPostProcessor("myProcessorsName", function(value, key, options) { return 'some post processed data based on translated value'; }); 并在初始化过程中添加: i18n.init({ postProcess: 'myProcessorsName' }); 但是我得到一个错误addPostPro
i18next
:
i18n.addPostProcessor("myProcessorsName", function(value, key, options)
{
return 'some post processed data based on translated value';
});
并在初始化过程中添加:
i18n.init({ postProcess: 'myProcessorsName' });
但是我得到一个错误addPostProcessor
不是一个函数
那么,我如何才能将后处理功能添加并使用到i18next
?从我的想法可以创建一个后处理模块,并使用use()
将其添加到i18next
实例中
在本例中,后处理模块将大写返回的任何字符串的第一个字母:
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"]
})
}
根据我的计算,您可以创建一个后期处理模块,并使用use()
将其添加到i18next
实例中
在本例中,后处理模块将大写返回的任何字符串的第一个字母:
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"]
})
}