Skip to content

How to use a different language in a subtree of a React app? #1715

Answered by thekip
kkeri asked this question in Q&A
Discussion options

You must be logged in to vote

The t macro refers to a global i18n object. To achieve your case you need to use few instances of it, and you actually did. What really should be changed it's just a syntax

// For use with exclusively English parts of the application.
const engI18n = setupI18n({ locale: 'en', messages: enMessages })

function MyEnglishSubtree {
  return (
    <I18nProvider i18n={engI18n}>
      <MyPage />
    </I18nProvider>
  )
}

function MyPage(...) {
  const {i18n} = useLingui()
  return (
    <AppPage title={t(i18n)`My Page`}>
      ...
    </AppPage>
  )
}

Here is a doc for t macro https://lingui.dev/ref/macro#t

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@kkeri
Comment options

Answer selected by kkeri
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants