i18next - 使用上下文时动态添加性别
i18next - dynamically add gender when context is used
我有以下内容:
t('translations::How are you?', { context: this.props.me.gender })
我目前正在使用 i18next-scanner 自动生成 JSON 命名空间文件。我遇到的问题是当我使用上下文时,我得到:
"How are you?": "How are you?" // fallback
"How are you?_": "How are you?" // context
我真正希望发生的是每当在 i18n 中使用 context 时,我希望生成 male
和 female
所以:
"How are you?": "How are you?"
"How are you?_male": "How are you?"
"How are you?_female": "How are you?"
我还没有找到动态添加这些键的好方法。我可以尝试什么?
我查看了libs源代码,从测试来看,这个lib似乎不支持这个。
但是,从 this code 开始,如果您的 options.context
是一个函数,它将被调用,这意味着您将能够修改它。
检查此代码:https://codesandbox.io/s/laughing-ptolemy-vd1xb
如您所见,我已经成功更改了上下文类型,但库不支持您的案例(基于 this line)它只添加了一个上下文。
它可以是一个非常简单的 PR :]
编辑
我已将此功能添加到 i18next-scanner,现在您可以指定 contextDefaultValues
- 将在动态上下文值的情况下使用的值列表。
// i18next-scanner.config.js
module.exports = {
input: [
'app/**/*.{js,jsx}',
// Use ! to filter out files or directories
'!app/**/*.spec.{js,jsx}',
'!app/i18n/**',
'!**/node_modules/**',
],
output: './',
options: {
...
contextDefaultValues: ['male', 'female'] // <--- specify this option
...
},
...
};
我有以下内容:
t('translations::How are you?', { context: this.props.me.gender })
我目前正在使用 i18next-scanner 自动生成 JSON 命名空间文件。我遇到的问题是当我使用上下文时,我得到:
"How are you?": "How are you?" // fallback
"How are you?_": "How are you?" // context
我真正希望发生的是每当在 i18n 中使用 context 时,我希望生成 male
和 female
所以:
"How are you?": "How are you?"
"How are you?_male": "How are you?"
"How are you?_female": "How are you?"
我还没有找到动态添加这些键的好方法。我可以尝试什么?
我查看了libs源代码,从测试来看,这个lib似乎不支持这个。
但是,从 this code 开始,如果您的 options.context
是一个函数,它将被调用,这意味着您将能够修改它。
检查此代码:https://codesandbox.io/s/laughing-ptolemy-vd1xb
如您所见,我已经成功更改了上下文类型,但库不支持您的案例(基于 this line)它只添加了一个上下文。
它可以是一个非常简单的 PR :]
编辑
我已将此功能添加到 i18next-scanner,现在您可以指定 contextDefaultValues
- 将在动态上下文值的情况下使用的值列表。
// i18next-scanner.config.js
module.exports = {
input: [
'app/**/*.{js,jsx}',
// Use ! to filter out files or directories
'!app/**/*.spec.{js,jsx}',
'!app/i18n/**',
'!**/node_modules/**',
],
output: './',
options: {
...
contextDefaultValues: ['male', 'female'] // <--- specify this option
...
},
...
};