简体   繁体   English

i18next - 使用上下文时动态添加性别

[英]i18next - dynamically add gender when context is used

I have the following:我有以下几点:

t('translations::How are you?', { context: this.props.me.gender })

I'm currently using i18next-scanner to auto generate a JSON namespace file.我目前正在使用i18next-scanner自动生成一个 JSON 命名空间文件。 The problem I am having is when I use context, I get:我遇到的问题是当我使用上下文时,我得到:

"How are you?": "How are you?"     // fallback
"How are you?_": "How are you?"    // context

What I'd really like to happen is whenever context is used in i18n, I'd like male and female to be generated like so:我真正希望发生的是,每当在 i18n 中使用上下文时,我都希望像这样生成malefemale

"How are you?": "How are you?"
"How are you?_male": "How are you?"
"How are you?_female": "How are you?"

I haven't found a good way to dynamically add these keys.我还没有找到动态添加这些键的好方法。 What things can I try?我可以尝试什么?

I'v reviewed the libs source code, from the tests, it looks like this lib is not supports this.我已经查看了 libs 源代码,从测试来看,这个库似乎不支持这个。

But, from this code , if your options.context is a function, it will be invoked, that means that you will able to modify it.但是,从这段代码中,如果您的options.context是一个函数,它将被调用,这意味着您将能够修改它。 Check this code: https://codesandbox.io/s/laughing-ptolemy-vd1xb检查此代码: https : //codesandbox.io/s/laughing-ptolemy-vd1xb

As you can see, I've managed to change the context type, but the lib is not supporting your case (base on this line ) it adds only one context.如您所见,我已设法更改上下文类型,但该库不支持您的情况(基于此行)它只添加了一个上下文。

It can be a really simple PR :]它可以是一个非常简单的 PR :]

Edit I've added this functionality toi18next-scanner , now you can specify contextDefaultValues - a list of values that will be used in a case of dynamic context values.编辑我已将此功能添加到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  
        ...
    },
    ...
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM