我正在使用 i18next 翻译一个 React 应用程序,我想知道是否有一种方法可以使用 t() 函数访问嵌套的动态键,如
roles[RACE_DRIVER]?.title
。
包含我的翻译的 ENTranslation.js 文件是这样的
// ENTranslation.js
roles: {
[RACE_DRIVER]: {
title: 'Race driver',
titleShort: 'Driver',
description: 'This is a driver description'
},
[TEAM_MANAGER]: {
title: 'Team manager',
titleShort: 'Manager',
description: 'This is a team description'
},
我的配置是
// i18n.js
const resources = {
en: {
translation: ENTranslation
},
de: {
translation: DETranslation
},
};
i18next
.use(initReactI18next)
.init({
resources,
lng: 'en',
fallbackLng: 'en',
interpolation: {
escapeValue: false,
},
debug: true,
});
export default i18next;
我期待 t(roles[RACE_DRIVER]?.title) 返回 'Race driver' 但它没有。请问我该如何处理?