How to use Translation outside a React component?

See original GitHub issue

Hi, I’m trying to add translation to a object that is outside a React Component, is this possible?

Current code

import React from 'react'
import { useTranslation } from 'react-i18next'
import { Dimmer, Loader, Segment, Item } from 'semantic-ui-react'
import i18next from 'i18next';

// Images
import BankidImage from '../../../assets/img/bankid-logo.png' // Images
import SwishImage from '../../../assets/img/swish.webp' // Images

const webdata = {
  bankid: {
    image: BankidImage,
    textStarted: i18next.t('DimmerBankid.text'),
    textComplete: i18next.t('DimmerBankid.complete')
  },
  swish: {
    image: SwishImage,
    textStarted:
      'Öppna Swish-appen för att slutföra transaktionen. Kontrollera belopp, mottagare och acceptera betalningen. Klart!',
    textComplete: 'Betalning är genomförd. Tack!'
  }
}

export function DimmerLoading({ active, type, action }) {
  return (
    <Dimmer active={active} inverted>
      <Loader inverted>
        <Segment>
          <Item.Group>
            <Item>
              <Item.Image size="tiny" src={webdata[type].image} />
              <Item.Content verticalAlign="middle">
                {action ? webdata[type].textComplete : webdata[type].textStarted}
              </Item.Content>
            </Item>
          </Item.Group>
        </Segment>
      </Loader>
    </Dimmer>
  )
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

11reactions
jamuhlcommented, Jan 18, 2021

Something like:

import i18n from '../../../i18n';

const data =  {};

function updateTranslations () {
    data.code =   i18n.t('key1');
    data.message =  i18n.t('key2');
}

// i18next seems ready -> initial fill translations
if (i18n.isInitialized) {
  updateTranslations();
}

// reset translations to new values on language change
i18next.on('languageChanged', function(lng) {
    updateTranslations();
});

// we loaded some translation file? reset needed?!?
// https://www.i18next.com/overview/api#store-events
i18next.on('loaded', function(lng) {
    updateTranslations();
});

export default data;
0reactions
Danielgomespcommented, Jan 28, 2022

Just like that:

import i18n from `./i18n`

i18n.t(...);
Read more comments on GitHub >

github_iconTop Results From Across the Web

Translation outside React Component · Issue #909 - GitHub
Outside of react components just use the i18next instance: import i18n from `./i18n` i18n.t( ...
Read more >
i18next translation outside component - Stack Overflow
I'm using create-react-app and it's default settings for folders reference, maybe this is the key problem, but I can't find out why and...
Read more >
Step by step guide - react-i18next documentation
The Trans component is the best way to translate a JSX tree in one translation. This enables you to eg. easily translate text...
Read more >
How to use Translation outside a React component?
Hi, I'm trying to add translation to a object that is outside a React Component, is this possible? Current code import React from...
Read more >
Common i18n patterns in React — LinguiJS documentation
Using jsx macros is the most straightforward way how to translate your React components. <Trans> handles translations of messages including variables and other ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found