Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to translate global constant js file with multiple export const? #1343

Closed
tmarcus68 opened this issue Jul 15, 2021 · 4 comments
Closed
Labels

Comments

@tmarcus68
Copy link

tmarcus68 commented Jul 15, 2021

I'm trying to dynamic translate my GlobalConstant.js which is not a component or function. I understand that I've to use an API event listener to dynamic changes but I am not sure how should I implement it into my code. Also, I can't even show the default en text.

Any advice is appreciated. TIA

Sample of the API event listener

i18next.on('languageChanged init', () => {
    // what to put here?
});

Code as below

i18n.js

import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import LanguageDetector from 'i18next-browser-languagedetector';
import HttpApi from 'i18next-http-backend';

i18n
  .use(initReactI18next)
  .use(LanguageDetector)
  .use(HttpApi)
  .init({
    supportedLngs: ['en', 'jp'],
    fallbackLng: 'en',
    detection: {
      order: ['cookie', 'htmlTag', 'localStorage', 'path', 'subdomain'],
      caches: ['cookie'],
    },
    backend: {
      loadPath: '/assets/locales/{{lng}}/translation.json',
    },
    debug: true,
  });

export default i18n;

en/translation.json

{
"firstName": "First Name"
// other codes...
}

jp/translation.json

{
"firstName": "名"
// other codes...
}

GlobalConstant.js

import i18n from '../i18n';

export const label = [
  {
    index: 0,
    name: 'firstName',
    label: i18n.t('firstName')
  },
  {
    index: 1,
    name: 'lastName',
    label: i18n.t('lastName')
  },
];

export const month = [
  i18n.t('jan')
  i18n.t('feb')
  i18n.t('mar')
  i18n.t('apr')
  i18n.t('may')
  i18n.t('jun')
  i18n.t('jul')
  i18n.t('aug')
  i18n.t('sep')
  i18n.t('oct')
  i18n.t('nov')
  i18n.t('dec')
];

Component.js

import * as Constants from '../../utils/GlobalConstant";

export function Component() {
let namingLabel = [];

// other codes...

namingLabel.push(Constants.label);

// other codes...
}

Errors

i18next: hasLoadedNamespace: i18next was not initialized undefined
i18next.js:22 i18next::translator: key "firstName" for languages "en" won't get resolved as namespace "translation" was not yet loaded This means something IS WRONG in your setup. You access the t function before i18next.init / i18next.loadNamespace / i18next.changeLanguage was done. Wait for the callback or Promise to resolve before accessing it!!!
i18next::translator: missingKey undefined translation firstName firstName

On the front end it will just show "firstName" instead of "First Name"

@jamuhl
Copy link
Member

jamuhl commented Jul 15, 2021

#1306

@jamuhl
Copy link
Member

jamuhl commented Jul 15, 2021

#1236 (comment)

@jamuhl
Copy link
Member

jamuhl commented Jul 15, 2021

or #909 (comment)

@stale
Copy link

stale bot commented Jul 22, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Jul 22, 2021
@stale stale bot closed this as completed Jul 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants