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

Allow JSX in translations #21

Open
Lukasz-pluszczewski opened this issue Apr 8, 2024 · 1 comment
Open

Allow JSX in translations #21

Lukasz-pluszczewski opened this issue Apr 8, 2024 · 1 comment

Comments

@Lukasz-pluszczewski
Copy link

Is your feature request related to a problem? Please describe.
When the translation is JSX (see below), it works fine but validation here logs an error as if the translation is missing.

const translations = {
  foo: {
    'en_US': <div>Foo</div>,
    'pl_PL': <div>Fuu</div>,
  },
}

To avoid the warning you can create a variable translation like this:

const translations = {
  foo: () => ({
    'en_US': <div>Foo</div>,
    'pl_PL': <div>Fuu</div>,
  }),
}

Describe the solution you'd like
JSX seems to work fine, so I would suggest allowing JSX translations and:

  • improving the validation to prevent unnecessary console warnings when translation is not a string (e.g. allowing non-string values that are valid react elements using React.isValidElement() function).
  • updating readme with JSX translation examples
@Lukasz-pluszczewski
Copy link
Author

Lukasz-pluszczewski commented Apr 8, 2024

The workaround with variable translation is not perfect, as the type of translated.foo is string instead of a function when the translation function does not return a string:

const translations = {
  foo: () => ({
    'en_US': <div>Foo</div>,
    'pl_PL': <div>Fuu</div>,
  }),
}

translated.foo() // TS2349: This expression is not callable. Type String has no call signatures.

This hack works, but it's not pretty

const translations = {
  foo: () => ({
    'en_US': (<div>Foo</div>) as unknown as string,
    'pl_PL': (<div>Fuu</div>) as unknown as string,
  }),
}

translated.foo() // works as expected

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