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

plural Macro Fails with Custom i18n Instance #2178

Open
Yurii-Yanovitsky opened this issue Feb 7, 2025 · 1 comment
Open

plural Macro Fails with Custom i18n Instance #2178

Yurii-Yanovitsky opened this issue Feb 7, 2025 · 1 comment

Comments

@Yurii-Yanovitsky
Copy link

Yurii-Yanovitsky commented Feb 7, 2025

I have an SSR application where I create a new i18n instance for each request. The locale is loaded and activated properly before rendering components. However, when I use the plural macro, I receive the following error message:

Lingui: Attempted to call a translation function without setting a locale.
Make sure to call i18n.activate(locale) before using Lingui functions.
This issue may also occur due to a race condition in your initialization logic.

Environment

  • Lingui version: 5.2.0

Code Example

import { plural } from "@lingui/core/macro";
import { useLingui } from "@lingui/react/macro";

const { i18n } = useLingui();

const message = plural(count, {
  one: "# Book",
  other: "# Books",
});

Expected Behavior

I expect plural to work with my i18n instance from context and compile into something like:

import { i18n } from "@lingui/core";

const message = i18n._({
  id: "V/M0Vc",
  message: "{count, plural, one {# Book} other {# Books}}",
  values: { count },
});

Actual Behavior
It seems like plural is using the global i18n instance instead of the one provided via context. Since the global instance is not activated with the correct locale, this leads to the error.

Would appreciate any guidance on resolving this!

@Yurii-Yanovitsky
Copy link
Author

I found a solution here: #1312. However, this is not obvious, and I believe it should be included in the documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant