Skip to content

Dynamic values are pretty much impossible #1694

@lukescott

Description

@lukescott

Describe the bug
The id is stored in the localization file. If you remove it manually, it falls back to id in i18n._. If your message is a variable, Lingui refuses to use it.

To Reproduce

{i18n._({
  id: "address.label",
  message: label,
})}

OR

{i18n._({
  id: "address.label",
  message:"",
}) || label}

In either case the extractor sees message as "", so it falls back to id.

Then if you manually remove it from the file (which it will store again on next run because it sees it as empty), this line falls back to id again:

https://github.com/lingui/js-lingui/blob/main/packages/core/src/i18n.ts#L234

Expected behavior
I expect to be able to use a dynamic value as a fallback.

Additional context

I have pre-localization values that are user provided. They are basically custom text field labels. For this particular id the localization value will always be empty "". When it is empty it should fall back to the pre-localization value (without error).

What we are planning to do is provide a localization section where the user can override specific labels for a specific localization. The pre-localization is essentially what we would consider the user's "base language".

Proposed Solution

Do not fall back to id for extract, and do not fall back to id in i18n._.

This should be possible but currently isn't because i18n._ always returns a non "" value.

{i18n._({
  id: "id1",
} || i18n._({
  id: "id2",
})}

Perhaps consider checking for undefined instead of falsy so you could do something like this as a compromise:

{i18n._({
  id: "id1",
  message: "",
} || i18n._({
  id: "id2",
  message: "",
}) || "my own debug message"}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions