Using i18next in enums (TypeScript)

See original GitHub issue

Hello, I’m using React with react-i18next plugin and I want to translate value of enums. Is it possible?

Code:

enum catSize {
    SMALL = 'Small',
    MEDIUM = 'Medium',
    LARGE = 'Large',
}

and I’d like to convert this to something like:

enum catSize {
    SMALL = t('sizes.small'),
    MEDIUM = t('sizes.medium'),
    LARGE = t('sizes.large'),
}

but obviously it won’t work because we don’t have access to t function which is provided by HOC. Is there any trick to achive translations outside render function and use it in my case?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:17 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
jamuhlcommented, Aug 23, 2022

beside that…why does the enum need translated values at all? just move the translation to the rendering

1reaction
jamuhlcommented, Jul 18, 2018

in that case you can also just use i18n.t directly if you prefer…https://react.i18next.com/#what-is-react-i-18-next see first hint

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript: is there a way to parameterize an enum
Now obviously it won't work with react-i18next . I wonder is there a way to parameterize the enum's value using Generics so we...
Read more >
TypeScript - react-i18next documentation
TypeScript definitions for react-i18next can be extended by using Type Augmentation and Merging Interfaces. So the first step is creating a declaration file ......
Read more >
enum-i18n - npm
Enum with support for translated member descriptions. ... Start using enum-i18n in your project by running `npm i enum-i18n`.
Read more >
Angular i18n: Translate Enums | by Piotr Lewandowski
5 practices translating enums in Angular with built-in i18n.
Read more >
Angular: Translate Enums (i18n) - Medium
Angular: Translate Enums (i18n) ... One of the biggest flaw of built-in Angular translation engine is it only supports translations known during ...
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