从捆绑的组件中访问相同的I18nContext

huangapple go评论61阅读模式
英文:

Access to the same I18nContext from bundled components

问题

我尝试使用useTranslation钩子来访问捆绑组件内的i18n实例,但我收到了undefined。我期望收到i18n实例对象。

英文:

as mentioned in the title, I have a bundled UI kit and I would like to enable translation within the UI kit, as some of its molecule components require translations. I have tried to use the useTranslation hook to access the i18n config object, but it returns undefined. I also attempted to include it as an external in my ViteJS bundler, but I still get undefined i18n. I would like to ask if anyone else has had the same requirements and how they were able to achieve this. Thank you very much.

I tried using the useTranslation hook to access the i18n instance inside bundled components, but I received undefined. I'm expecting to receive the i18n instance object.

答案1

得分: 1

I was testing my UI-kit with the yarn link command, which links the node_modules folder into my main project. This created two react-i18next Provider instances which prevented the bundled code from accessing the main Provider.

To resolve my issues, I used yalc to install my package locally. Unlike yarn/npm, yalc only copies the files specified in package.json, instead of symlinking all the folders.

For everyone who has the same issue even when using yalc on a local environment, check if the react-i18next entry is included in the bundler's externals.

英文:

I was testing my UI-kit with the yarn link command, which links the node_modules folder into my main project. This created two react-i18next Provider instances which prevented the bundled code from accessing the main Provider.

To resolve my issues, I used yalc to install my package locally. Unlike yarn/npm, yalc only copies the files specified in package.json, instead of symlinking all the folders.

For everyone have the same issue even using yalc on local environment. Control if react-i18next entry is put on bundler externals

huangapple
  • 本文由 发表于 2023年2月18日 09:37:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75490651.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定