No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

hace 3 años
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
  2. import _defineProperty from "@babel/runtime/helpers/defineProperty";
  3. function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
  4. function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
  5. import { useState, useEffect, useContext } from 'react';
  6. import { getI18n, getDefaults, ReportNamespaces, getHasUsedI18nextProvider, I18nContext } from './context';
  7. import { warnOnce, loadNamespaces, hasLoadedNamespace } from './utils';
  8. export function useTranslation(ns) {
  9. var props = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  10. // assert we have the needed i18nInstance
  11. var i18nFromProps = props.i18n;
  12. var ReactI18nContext = useContext(I18nContext);
  13. var _ref = getHasUsedI18nextProvider() ? ReactI18nContext || {} : {},
  14. i18nFromContext = _ref.i18n,
  15. defaultNSFromContext = _ref.defaultNS;
  16. var i18n = i18nFromProps || i18nFromContext || getI18n();
  17. if (i18n && !i18n.reportNamespaces) i18n.reportNamespaces = new ReportNamespaces();
  18. if (!i18n) {
  19. warnOnce('You will need pass in an i18next instance by using initReactI18next');
  20. var retNotReady = [function (k) {
  21. return k;
  22. }, {}, false];
  23. retNotReady.t = function (k) {
  24. return k;
  25. };
  26. retNotReady.i18n = {};
  27. retNotReady.ready = false;
  28. return retNotReady;
  29. }
  30. var i18nOptions = _objectSpread({}, getDefaults(), {}, i18n.options.react);
  31. var _props$useSuspense = props.useSuspense,
  32. useSuspense = _props$useSuspense === void 0 ? i18nOptions.useSuspense : _props$useSuspense; // prepare having a namespace
  33. var namespaces = ns || defaultNSFromContext || i18n.options && i18n.options.defaultNS;
  34. namespaces = typeof namespaces === 'string' ? [namespaces] : namespaces || ['translation']; // report namespaces as used
  35. if (i18n.reportNamespaces.addUsedNamespaces) i18n.reportNamespaces.addUsedNamespaces(namespaces); // are we ready? yes if all namespaces in first language are loaded already (either with data or empty object on failed load)
  36. var ready = (i18n.isInitialized || i18n.initializedStoreOnce) && namespaces.every(function (n) {
  37. return hasLoadedNamespace(n, i18n);
  38. }); // binding t function to namespace (acts also as rerender trigger)
  39. function getT() {
  40. return {
  41. t: i18n.getFixedT(null, i18nOptions.nsMode === 'fallback' ? namespaces : namespaces[0])
  42. };
  43. }
  44. var _useState = useState(getT()),
  45. _useState2 = _slicedToArray(_useState, 2),
  46. t = _useState2[0],
  47. setT = _useState2[1]; // seems we can't have functions as value -> wrap it in obj
  48. useEffect(function () {
  49. var isMounted = true;
  50. var bindI18n = i18nOptions.bindI18n,
  51. bindI18nStore = i18nOptions.bindI18nStore; // if not ready and not using suspense load the namespaces
  52. // in side effect and do not call resetT if unmounted
  53. if (!ready && !useSuspense) {
  54. loadNamespaces(i18n, namespaces, function () {
  55. if (isMounted) setT(getT());
  56. });
  57. }
  58. function boundReset() {
  59. if (isMounted) setT(getT());
  60. } // bind events to trigger change, like languageChanged
  61. if (bindI18n && i18n) i18n.on(bindI18n, boundReset);
  62. if (bindI18nStore && i18n) i18n.store.on(bindI18nStore, boundReset); // unbinding on unmount
  63. return function () {
  64. isMounted = false;
  65. if (bindI18n && i18n) bindI18n.split(' ').forEach(function (e) {
  66. return i18n.off(e, boundReset);
  67. });
  68. if (bindI18nStore && i18n) bindI18nStore.split(' ').forEach(function (e) {
  69. return i18n.store.off(e, boundReset);
  70. });
  71. };
  72. }, [namespaces.join()]); // re-run effect whenever list of namespaces changes
  73. var ret = [t.t, i18n, ready];
  74. ret.t = t.t;
  75. ret.i18n = i18n;
  76. ret.ready = ready; // return hook stuff if ready
  77. if (ready) return ret; // not yet loaded namespaces -> load them -> and return if useSuspense option set false
  78. if (!ready && !useSuspense) return ret; // not yet loaded namespaces -> load them -> and trigger suspense
  79. throw new Promise(function (resolve) {
  80. loadNamespaces(i18n, namespaces, function () {
  81. setT(getT());
  82. resolve();
  83. });
  84. });
  85. }