对于带有变量的key,可以使用${}来将key作为字符串模板。例如:
const i18next = require('i18next');
const scanner = require('i18next-scanner');
const variable = 'world';
const key = `hello_${variable}`;
i18next
.init({
lng: 'en',
debug: true,
resources: {}
});
scanner({
// other config options ...
keySeparator: false, // by default: '.'
sort: false, // by default: true
func: {
list: ['t'], // by default ['i18next.t']
extensions: ['.js', '.jsx'] // by default: ['.js']
},
trans: {
component: 'Trans', // by default: 'i18n'
i18nKey: 'i18nKey', // by default: 'i18nKey'
defaultValues: [], // added: see next section
// fine-grained fallbacks for keysPerNamespaces
fallbackKey: (ns, value) => value,
// added: for pluralization support
fallbackPlural: true
}
}, (err, keys) => {
// Here, if key = "hello_${world}", it would return "hello_${world}" as key string
console.log(keys);
});