question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

t in attribute not updating when activating language change

See original GitHub issue

Describe the bug t in attribute not updating when activating language change

To Reproduce Steps to reproduce the behavior, possibly with minimal code sample, e.g: translate.zip

import logo from "./logo.svg";
import "./App.css";

import { ThemeProvider } from "styled-components";
import {
  themeBaseLight,
  ButtonSmall,
  ButtonLink,
} from "@cmctechnology/phoenix-stockbroking-web-design";
import { i18n } from "@lingui/core";
import { I18nProvider } from "@lingui/react";
import { messages as en } from "./locales/en/messages.js";
import { messages as pt } from "./locales/pt/messages.js";
import { Trans, t } from "@lingui/macro";

i18n.load("en", en);
i18n.load("pt", pt);
i18n.activate("en");

function App() {
  return (
    <I18nProvider i18n={i18n}>
      <a href="#" onClick={() => i18n.activate("en")}>
        en
      </a>
      |
      <a href="#" onClick={() => i18n.activate("pt")}>
        pt
      </a>
      <br />
      <Trans>Here is a button</Trans>
      <br />
      <MyButton label={<Trans>I Change</Trans>} />
      <MyButton label={t`I Don't`} />
    </I18nProvider>
  );
}

const MyButton = ({ label }) => {
  return (
    <button>
      <span>{label}</span>
    </button>
  );
};

export default App;

Expected behavior I would expect t`` to change when activating language change.

Additional context Using <Trans/> in the attribute works.

  • jsLingui version lingui --version 3.8.10
  • Babel version npm list @babel/core 7.12.3
  • Your Babel config (e.g. .babelrc) or framework you use (Create React App, Meteor, etc.) No .babelrc, straght create-react-app

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
cdaycmccommented, May 6, 2021

I found the issue comparing my code to yours. Moving the I18nProvider to a higher component updated the translations as required for t().

Note: no useEffect is required for this to work.

import "./App.css";
import { i18n } from "@lingui/core";
import { I18nProvider } from "@lingui/react";
import { messages as en } from "./locales/en/messages.js";
import { messages as pt } from "./locales/pt/messages.js";
import { Trans, t } from "@lingui/macro";

i18n.load("en", en);
i18n.load("pt", pt);
i18n.activate("en");

function App() {
  return (
    <I18nProvider i18n={i18n}>
      <AApp></AApp>
    </I18nProvider>
  );
}

const AApp = () => {
  return (
    <>
      <a
        href="#"
        onClick={() => {
          i18n.activate("en");
        }}
      >
        en
      </a>
      |
      <a
        href="#"
        onClick={() => {
          i18n.activate("pt");
        }}
      >
        pt
      </a>
      <br />
      <Trans>Here is a button</Trans>
      <br />
      <MyButton label={<Trans>I Change</Trans>} />
      <MyButton label={t`I Dont`} />
      <Trans>I Dont</Trans>
      <br />
      <span>
        <Trans>
          But here is something that has <a href="#">a link</a> in the middle of
          the text.
        </Trans>
        xx
      </span>
    </>
  );
};

const MyButton = ({ label }) => {
  return (
    <button>
      <span>{label}</span>
    </button>
  );
};

export default App;
0reactions
semoalcommented, May 6, 2021

In the previous code you pasted, it’s also wrong…

  const activate = useEffect(() => {
    i18n.activate("en");
  });

This causes an infinite loop because you’re not specyfing to only render on mount.

  const activate = useEffect(() => {
    i18n.activate("en");
  }, []);

This is the correct code, and must work as expected

Read more comments on GitHub >

github_iconTop Results From Across the Web

Declaring language in HTML - W3C
Instead, move the attribute containing text in a different language to another element, as shown in this example, where the a element inherits ......
Read more >
jQuery's data() attribute does not update when element data ...
The short answer is: don't use jQuery .data() for dynamically set data attributes, unless you can guarantee that data attributes are always ...
Read more >
AutoCAD title block attributes do not update automatically after ...
Recreate block with the the same name af original name (command BLOCK). Save drawing and check in back to Vault. Try editing properties...
Read more >
<html> element must have a lang attribute | Axe Rules
Screen readers can switch between these language libraries easily, but only if the documents specify which language(s) to read and when. If the...
Read more >
Okta is not updating users attributes in some applications
For example if the user's last name has been changed from Smith to Anderson in Okta, we will send the new last name...
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