Coder Social home page Coder Social logo

Comments (2)

thekip avatar thekip commented on June 15, 2024

Read the first note in this paragraph.

https://lingui.dev/tutorials/react-patterns#translations-outside-react-components

from js-lingui.

thekip avatar thekip commented on June 15, 2024

Here is an issue with an example itself. The app component doesn't get re-rendered since nothing changed from React's point of view.

So the solution would be splitting to root component with provider and children components to be able to use useLingui hook and let LinguiProvider re-trigger render of components subtree

function App() {
  useEffect(() => {
    loadCatalog("en")
  }, [])

  return (
    <I18nProvider i18n={i18n}>
      <AppContent />
    </I18nProvider>
  )
}

function AppContent() {
  const [count, setCount] = useState(0)

  const { i18n } = useLingui()

  return (
    <div className="App">
      <div>
        <a href="https://vitejs.dev" target="_blank">
          <img src={viteLogo} className="logo" alt="Vite logo" />
        </a>
        <a href="https://reactjs.org" target="_blank">
          <img src={reactLogo} className="logo react" alt="React logo" />
        </a>
        <a href="https://lingui.dev" target="_blank">
          <img src={linguiLogo} className="logo react" alt="Lingui logo" />
        </a>
      </div>
      <h1>Vite + React + Lingui</h1>
      <div className="card">
        <button onClick={() => setCount((count) => count + 1)}>
          <Plural value={count} one="# month" other="# months" />
        </button>
        <p>
          <Trans>
            Edit <code>src/App.tsx</code> and save to test HMR
          </Trans>
        </p>
        <p>
          <button
            style={{ marginRight: "20px" }}
            onClick={() => loadCatalog("pl")}
          >
            Polish
          </button>
          <button onClick={() => loadCatalog("en")}>English</button>
        </p>
      </div>
      <p className="read-the-docs">
        <Trans>Click on the Vite and React logos to learn more</Trans>
        <br />

        {t(i18n)`Click on the Vite and React logos to learn more`}

        {/* or */}
        {i18n._(msg`Click on the Vite and React logos to learn more`)}

        {/* or in >[email protected], export underscore from useLingui hook https://github.com/lingui/js-lingui/pull/1721*/}
        {_(msg`Click on the Vite and React logos to learn more`)}
      </p>
    </div>
  )
}

export default App

from js-lingui.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.