i18next translator missingKey en translation

5.1k Views Asked by At

I'am trying to implement i18next translation on my web-site and cannot get success with it.

i18next.init({
    debug: true,
    lng: 'en',
    resources: {
        en: {
            translation: {
                opt: {
                    page_title: 'Orders'
                },
            }
        },
        ru: {
            translation: {
                opt: {
                    page_title: 'Заказы'
                },
            }
        }
    }, function(err, t) {
        jqueryI18next.init(i18next, $);
    });

I have vocabulary for some part of my system which need to have a set of named variables.

i18next.addResources('en', 'desktop', {
    'Авиа'                          :   'Avia',
    'Ж/Д'                           :   'Train',
    'Произвольная услуга'           :   'Custom Service',

    'Текущий баланс'                :   'Current balance',
    'Оборот за предыдущий месяц'    :   'Turnover for the previous month',
    'Оборот за текущий месяц'       :   'Turnover for the current month',
    'Общий оборот'                  :   'Total turnover',
});

i18next.loadNamespaces('desktop');

There is no success with next equation:

var fincanceDict = {
    company_balance                 :   $.t('Текущий баланс'),
    company_turnover_prev_month     :   $.t('Оборот за предыдущий месяц'),
    company_turnover_current_month  :   $.t('Оборот за текущий месяц'),
    company_turnover_full           :   $.t('Общий оборот')
}

I'm getting an errors for each translation operation:

i18next.js?v=e988b1:142 i18next::translator: missingKey en translation Текущий баланс Текущий баланс
i18next.js?v=e988b1:142 i18next::translator: missingKey en translation Оборот за предыдущий месяц Оборот за предыдущий месяц
i18next.js?v=e988b1:142 i18next::translator: missingKey en translation Оборот за текущий месяц Оборот за текущий месяц
i18next.js?v=e988b1:142 i18next::translator: missingKey en translation Общий оборот Общий оборот

If I check my i18next.translator.resourceStore.data I'll get initial vocabulary for en & ru language object with translation child who own a set of nested key/value pairs with no extension I did.

I fill that I missing something, I have try to change namespace to default and back to desktop neither is helped.

2

There are 2 best solutions below

0
On

loadNamespace -> does load a namespace via backend (eg. xhr-backend): https://www.i18next.com/api.html#loadnamespaces

translation is the default namespace that's why you can access translations in that without prepending it in t function. https://www.i18next.com/configuration-options.html#languages-namespaces-resources

you can create a "fixedT" function

const desktopT = i18next.getFixedT(null, 'desktop');
desktopT('Текущий баланс');
0
On

I was googling around and found somethingm that resolve my problem with no description. I should use namespace value inside i18next.t / $.t functions:

i18next.t('namespace:key')

or

$.t('namespace:key')

That is how I resolved my problem:

$.t('desktop:Текущий баланс')

I was wondering what i18next.loadNamespaces function did if I should to set translation namespace manually each time.