nabekou29 / js-i18n.nvim

A Neovim plugin for the JavaScript library i18next
MIT License
18 stars 2 forks source link

`keyPrefix` Option Not Working with Nested Structures in `t` Function #22

Closed fragov closed 3 weeks ago

fragov commented 1 month ago

Foremost, HUGE thanks for your great plugin and the time you've invested in it, as well as for applying my suggestions. 👍🏼

The keyPrefix option in the t function appears to be inconsistently handled for translations with nested structures. According to the expected behavior, setting keyPrefix should prepend the specified prefix to keys passed to the t function, facilitating the access of nested translation values. Here’s an example to illustrate the issue:

Translation:

{
    "key": "value of key",
    "look": {
        "deep": "value of look deep"
    }
}

Working Example:

const { t } = useTranslation('translation');
t('look.deep');  // Outputs the expected nested translation

Non-Working Examples: However, when attempting to use the keyPrefix option inside the t function call or rely on the initially set prefix for nested keys, the function fails to retrieve the nested values:

// Attempt 1: Specifying `keyPrefix` again inside the `t` function
const { t } = useTranslation('translation');
t('deep', { keyPrefix: 'look' });  // Fails to output the nested translation

// Attempt 2: Relying on the initial `keyPrefix` setting
const { t } = useTranslation('translation', { keyPrefix: 'look' });
t('deep');  // Fails to output the nested translation

Both attempts fail to work as expected.

https://www.i18next.com/translation-function/essentials

fragov commented 1 month ago

I think it fails for any t function with options, as second parameter. This is also not working:

{
  "dayWithCount_one": "{{count}} day",
  "dayWithCount_other": "{{count}} days",
}

const { t } = useTranslation('common');
t('dayWithCount', { count: 2 }); // warn about missing translation

And gd is not working for this translation (but it worked for t('deep') from previous examples).

nabekou29 commented 1 month ago

@fragov Thank you for using the plugin and for reporting this issue.

I planning to add support for keyPrefix and namespace in a future update.

nabekou29 commented 3 weeks ago

@fragov KeyPrefix support has been implemented in version 0.4.0. Please update your plugin to check! If you encounter any bugs or have further feedback, please report it.