withTranslation.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.withTranslation = void 0;
  6. var _react = require("react");
  7. var _useTranslation = require("./useTranslation.js");
  8. var _utils = require("./utils.js");
  9. const withTranslation = (ns, options = {}) => function Extend(WrappedComponent) {
  10. function I18nextWithTranslation({
  11. forwardedRef,
  12. ...rest
  13. }) {
  14. const [t, i18n, ready] = (0, _useTranslation.useTranslation)(ns, {
  15. ...rest,
  16. keyPrefix: options.keyPrefix
  17. });
  18. const passDownProps = {
  19. ...rest,
  20. t,
  21. i18n,
  22. tReady: ready
  23. };
  24. if (options.withRef && forwardedRef) {
  25. passDownProps.ref = forwardedRef;
  26. } else if (!options.withRef && forwardedRef) {
  27. passDownProps.forwardedRef = forwardedRef;
  28. }
  29. return (0, _react.createElement)(WrappedComponent, passDownProps);
  30. }
  31. I18nextWithTranslation.displayName = `withI18nextTranslation(${(0, _utils.getDisplayName)(WrappedComponent)})`;
  32. I18nextWithTranslation.WrappedComponent = WrappedComponent;
  33. const forwardRef = (props, ref) => (0, _react.createElement)(I18nextWithTranslation, Object.assign({}, props, {
  34. forwardedRef: ref
  35. }));
  36. return options.withRef ? (0, _react.forwardRef)(forwardRef) : I18nextWithTranslation;
  37. };
  38. exports.withTranslation = withTranslation;