react-i18next 14.1.3 → 15.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/es/Trans.js CHANGED
@@ -2,29 +2,28 @@ import { useContext } from 'react';
2
2
  import { nodesToString, Trans as TransWithoutContext } from './TransWithoutContext.js';
3
3
  import { getI18n, I18nContext } from './context.js';
4
4
  export { nodesToString };
5
- export function Trans(_ref) {
6
- let {
7
- children,
8
- count,
9
- parent,
10
- i18nKey,
11
- context,
12
- tOptions = {},
13
- values,
14
- defaults,
15
- components,
16
- ns,
17
- i18n: i18nFromProps,
18
- t: tFromProps,
19
- shouldUnescape,
20
- ...additionalProps
21
- } = _ref;
5
+ export function Trans({
6
+ children,
7
+ count,
8
+ parent,
9
+ i18nKey,
10
+ context,
11
+ tOptions = {},
12
+ values,
13
+ defaults,
14
+ components,
15
+ ns,
16
+ i18n: i18nFromProps,
17
+ t: tFromProps,
18
+ shouldUnescape,
19
+ ...additionalProps
20
+ }) {
22
21
  const {
23
22
  i18n: i18nFromContext,
24
23
  defaultNS: defaultNSFromContext
25
24
  } = useContext(I18nContext) || {};
26
25
  const i18n = i18nFromProps || i18nFromContext || getI18n();
27
- const t = tFromProps || i18n && i18n.t.bind(i18n);
26
+ const t = tFromProps || i18n?.t.bind(i18n);
28
27
  return TransWithoutContext({
29
28
  children,
30
29
  count,
@@ -35,7 +34,7 @@ export function Trans(_ref) {
35
34
  values,
36
35
  defaults,
37
36
  components,
38
- ns: ns || t && t.ns || defaultNSFromContext || i18n && i18n.options && i18n.options.defaultNS,
37
+ ns: ns || t?.ns || defaultNSFromContext || i18n?.options?.defaultNS,
39
38
  i18n,
40
39
  t: tFromProps,
41
40
  shouldUnescape,
@@ -5,14 +5,14 @@ import { getDefaults } from './defaults.js';
5
5
  import { getI18n } from './i18nInstance.js';
6
6
  const hasChildren = (node, checkLength) => {
7
7
  if (!node) return false;
8
- const base = node.props ? node.props.children : node.children;
8
+ const base = node.props?.children ?? node.children;
9
9
  if (checkLength) return base.length > 0;
10
10
  return !!base;
11
11
  };
12
12
  const getChildren = node => {
13
13
  if (!node) return [];
14
- const children = node.props ? node.props.children : node.children;
15
- return node.props && node.props.i18nIsDynamicList ? getAsArray(children) : children;
14
+ const children = node.props?.children ?? node.children;
15
+ return node.props?.i18nIsDynamicList ? getAsArray(children) : children;
16
16
  };
17
17
  const hasValidReactChildren = children => Array.isArray(children) && children.every(isValidElement);
18
18
  const getAsArray = data => Array.isArray(data) ? data : [data];
@@ -27,7 +27,7 @@ export const nodesToString = (children, i18nOptions) => {
27
27
  if (!children) return '';
28
28
  let stringNode = '';
29
29
  const childrenArray = getAsArray(children);
30
- const keepArray = i18nOptions.transSupportBasicHtmlNodes && i18nOptions.transKeepBasicHtmlNodesFor ? i18nOptions.transKeepBasicHtmlNodesFor : [];
30
+ const keepArray = i18nOptions?.transSupportBasicHtmlNodes ? i18nOptions.transKeepBasicHtmlNodesFor ?? [] : [];
31
31
  childrenArray.forEach((child, childIndex) => {
32
32
  if (isString(child)) {
33
33
  stringNode += `${child}`;
@@ -91,7 +91,7 @@ const renderNodes = (children, targetString, i18n, i18nOptions, combinedTOpts, s
91
91
  const renderInner = (child, node, rootReactNode) => {
92
92
  const childs = getChildren(child);
93
93
  const mappedChildren = mapAST(childs, node.children, rootReactNode);
94
- return hasValidReactChildren(childs) && mappedChildren.length === 0 || child.props && child.props.i18nIsDynamicList ? childs : mappedChildren;
94
+ return hasValidReactChildren(childs) && mappedChildren.length === 0 || child.props?.i18nIsDynamicList ? childs : mappedChildren;
95
95
  };
96
96
  const pushTranslatedJSX = (child, inner, mem, i, isVoid) => {
97
97
  if (child.dummy) {
@@ -117,7 +117,7 @@ const renderNodes = (children, targetString, i18n, i18nOptions, combinedTOpts, s
117
117
  const reactNodes = getAsArray(reactNode);
118
118
  const astNodes = getAsArray(astNode);
119
119
  return astNodes.reduce((mem, node, i) => {
120
- const translationContent = node.children && node.children[0] && node.children[0].content && i18n.services.interpolator.interpolate(node.children[0].content, opts, i18n.language);
120
+ const translationContent = node.children?.[0]?.content && i18n.services.interpolator.interpolate(node.children[0].content, opts, i18n.language);
121
121
  if (node.type === 'tag') {
122
122
  let tmp = reactNodes[parseInt(node.name, 10)];
123
123
  if (rootReactNode.length === 1 && !tmp) tmp = rootReactNode[0][node.name];
@@ -185,23 +185,22 @@ const renderNodes = (children, targetString, i18n, i18nOptions, combinedTOpts, s
185
185
  }], ast, getAsArray(children || []));
186
186
  return getChildren(result[0]);
187
187
  };
188
- export function Trans(_ref) {
189
- let {
190
- children,
191
- count,
192
- parent,
193
- i18nKey,
194
- context,
195
- tOptions = {},
196
- values,
197
- defaults,
198
- components,
199
- ns,
200
- i18n: i18nFromProps,
201
- t: tFromProps,
202
- shouldUnescape,
203
- ...additionalProps
204
- } = _ref;
188
+ export function Trans({
189
+ children,
190
+ count,
191
+ parent,
192
+ i18nKey,
193
+ context,
194
+ tOptions = {},
195
+ values,
196
+ defaults,
197
+ components,
198
+ ns,
199
+ i18n: i18nFromProps,
200
+ t: tFromProps,
201
+ shouldUnescape,
202
+ ...additionalProps
203
+ }) {
205
204
  const i18n = i18nFromProps || getI18n();
206
205
  if (!i18n) {
207
206
  warnOnce('You will need to pass in an i18next instance by using i18nextReactModule');
@@ -210,9 +209,9 @@ export function Trans(_ref) {
210
209
  const t = tFromProps || i18n.t.bind(i18n) || (k => k);
211
210
  const reactI18nextOptions = {
212
211
  ...getDefaults(),
213
- ...(i18n.options && i18n.options.react)
212
+ ...i18n.options?.react
214
213
  };
215
- let namespaces = ns || t.ns || i18n.options && i18n.options.defaultNS;
214
+ let namespaces = ns || t.ns || i18n.options?.defaultNS;
216
215
  namespaces = isString(namespaces) ? [namespaces] : namespaces || ['translation'];
217
216
  const nodeAsString = nodesToString(children, reactI18nextOptions);
218
217
  const defaultValue = defaults || nodeAsString || reactI18nextOptions.transEmptyNodeValue || i18nKey;
@@ -220,7 +219,7 @@ export function Trans(_ref) {
220
219
  hashTransKey
221
220
  } = reactI18nextOptions;
222
221
  const key = i18nKey || (hashTransKey ? hashTransKey(nodeAsString || defaultValue) : nodeAsString || defaultValue);
223
- if (i18n.options && i18n.options.interpolation && i18n.options.interpolation.defaultVariables) {
222
+ if (i18n.options?.interpolation?.defaultVariables) {
224
223
  values = values && Object.keys(values).length > 0 ? {
225
224
  ...values,
226
225
  ...i18n.options.interpolation.defaultVariables
@@ -256,6 +255,6 @@ export function Trans(_ref) {
256
255
  });
257
256
  }
258
257
  const content = renderNodes(components || children, translation, i18n, reactI18nextOptions, combinedTOpts, shouldUnescape);
259
- const useAsParent = parent !== undefined ? parent : reactI18nextOptions.defaultTransParent;
258
+ const useAsParent = parent ?? reactI18nextOptions.defaultTransParent;
260
259
  return useAsParent ? createElement(useAsParent, additionalProps, content) : content;
261
260
  }
@@ -1,13 +1,12 @@
1
1
  import { useTranslation } from './useTranslation.js';
2
- export function Translation(props) {
3
- const {
4
- ns,
5
- children,
6
- ...options
7
- } = props;
2
+ export const Translation = ({
3
+ ns,
4
+ children,
5
+ ...options
6
+ }) => {
8
7
  const [t, i18n, ready] = useTranslation(ns, options);
9
8
  return children(t, {
10
9
  i18n,
11
10
  lng: i18n.language
12
11
  }, ready);
13
- }
12
+ };
@@ -10,13 +10,15 @@ export class ReportNamespaces {
10
10
  }
11
11
  addUsedNamespaces(namespaces) {
12
12
  namespaces.forEach(ns => {
13
- if (!this.usedNamespaces[ns]) this.usedNamespaces[ns] = true;
13
+ this.usedNamespaces[ns] ??= true;
14
14
  });
15
15
  }
16
- getUsedNamespaces = () => Object.keys(this.usedNamespaces);
16
+ getUsedNamespaces() {
17
+ return Object.keys(this.usedNamespaces);
18
+ }
17
19
  }
18
20
  export const composeInitialProps = ForComponent => async ctx => {
19
- const componentsInitialProps = ForComponent.getInitialProps ? await ForComponent.getInitialProps(ctx) : {};
21
+ const componentsInitialProps = (await ForComponent.getInitialProps?.(ctx)) ?? {};
20
22
  const i18nInitialProps = getInitialProps();
21
23
  return {
22
24
  ...componentsInitialProps,
@@ -25,7 +27,7 @@ export const composeInitialProps = ForComponent => async ctx => {
25
27
  };
26
28
  export const getInitialProps = () => {
27
29
  const i18n = getI18n();
28
- const namespaces = i18n.reportNamespaces ? i18n.reportNamespaces.getUsedNamespaces() : [];
30
+ const namespaces = i18n.reportNamespaces?.getUsedNamespaces() ?? [];
29
31
  const ret = {};
30
32
  const initialI18nStore = {};
31
33
  i18n.languages.forEach(l => {
@@ -9,8 +9,7 @@ let defaultOptions = {
9
9
  useSuspense: true,
10
10
  unescape
11
11
  };
12
- export const setDefaults = function () {
13
- let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
12
+ export const setDefaults = (options = {}) => {
14
13
  defaultOptions = {
15
14
  ...defaultOptions,
16
15
  ...options
@@ -1 +1 @@
1
- {"type":"module","version":"14.1.3"}
1
+ {"type":"module","version":"15.0.1"}
package/dist/es/useSSR.js CHANGED
@@ -1,7 +1,6 @@
1
1
  import { useContext } from 'react';
2
2
  import { getI18n, I18nContext } from './context.js';
3
- export const useSSR = function (initialI18nStore, initialLanguage) {
4
- let props = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
3
+ export const useSSR = (initialI18nStore, initialLanguage, props = {}) => {
5
4
  const {
6
5
  i18n: i18nFromProps
7
6
  } = props;
@@ -9,7 +8,7 @@ export const useSSR = function (initialI18nStore, initialLanguage) {
9
8
  i18n: i18nFromContext
10
9
  } = useContext(I18nContext) || {};
11
10
  const i18n = i18nFromProps || i18nFromContext || getI18n();
12
- if (i18n.options && i18n.options.isClone) return;
11
+ if (i18n.options?.isClone) return;
13
12
  if (initialI18nStore && !i18n.initializedStoreOnce) {
14
13
  i18n.services.resourceStore.data = initialI18nStore;
15
14
  i18n.options.ns = Object.values(initialI18nStore).reduce((mem, lngResources) => {
@@ -10,8 +10,7 @@ const usePrevious = (value, ignore) => {
10
10
  };
11
11
  const alwaysNewT = (i18n, language, namespace, keyPrefix) => i18n.getFixedT(language, namespace, keyPrefix);
12
12
  const useMemoizedT = (i18n, language, namespace, keyPrefix) => useCallback(alwaysNewT(i18n, language, namespace, keyPrefix), [i18n, language, namespace, keyPrefix]);
13
- export const useTranslation = function (ns) {
14
- let props = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
13
+ export const useTranslation = (ns, props = {}) => {
15
14
  const {
16
15
  i18n: i18nFromProps
17
16
  } = props;
@@ -34,7 +33,7 @@ export const useTranslation = function (ns) {
34
33
  retNotReady.ready = false;
35
34
  return retNotReady;
36
35
  }
37
- if (i18n.options.react && i18n.options.react.wait !== undefined) warnOnce('It seems you are still using the old wait option, you may migrate to the new useSuspense behaviour.');
36
+ if (i18n.options.react?.wait) warnOnce('It seems you are still using the old wait option, you may migrate to the new useSuspense behaviour.');
38
37
  const i18nOptions = {
39
38
  ...getDefaults(),
40
39
  ...i18n.options.react,
@@ -44,9 +43,9 @@ export const useTranslation = function (ns) {
44
43
  useSuspense,
45
44
  keyPrefix
46
45
  } = i18nOptions;
47
- let namespaces = ns || defaultNSFromContext || i18n.options && i18n.options.defaultNS;
46
+ let namespaces = ns || defaultNSFromContext || i18n.options?.defaultNS;
48
47
  namespaces = isString(namespaces) ? [namespaces] : namespaces || ['translation'];
49
- if (i18n.reportNamespaces.addUsedNamespaces) i18n.reportNamespaces.addUsedNamespaces(namespaces);
48
+ i18n.reportNamespaces.addUsedNamespaces?.(namespaces);
50
49
  const ready = (i18n.isInitialized || i18n.initializedStoreOnce) && namespaces.every(n => hasLoadedNamespace(n, i18n, i18nOptions));
51
50
  const memoGetT = useMemoizedT(i18n, props.lng || null, i18nOptions.nsMode === 'fallback' ? namespaces : namespaces[0], keyPrefix);
52
51
  const getT = () => memoGetT;
@@ -79,11 +78,11 @@ export const useTranslation = function (ns) {
79
78
  const boundReset = () => {
80
79
  if (isMounted.current) setT(getNewT);
81
80
  };
82
- if (bindI18n && i18n) i18n.on(bindI18n, boundReset);
83
- if (bindI18nStore && i18n) i18n.store.on(bindI18nStore, boundReset);
81
+ if (bindI18n) i18n?.on(bindI18n, boundReset);
82
+ if (bindI18nStore) i18n?.store.on(bindI18nStore, boundReset);
84
83
  return () => {
85
84
  isMounted.current = false;
86
- if (bindI18n && i18n) bindI18n.split(' ').forEach(e => i18n.off(e, boundReset));
85
+ if (i18n) bindI18n?.split(' ').forEach(e => i18n.off(e, boundReset));
87
86
  if (bindI18nStore && i18n) bindI18nStore.split(' ').forEach(e => i18n.store.off(e, boundReset));
88
87
  };
89
88
  }, [i18n, joinedNS]);
package/dist/es/utils.js CHANGED
@@ -1,21 +1,15 @@
1
- export function warn() {
2
- if (console && console.warn) {
3
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
4
- args[_key] = arguments[_key];
5
- }
1
+ export const warn = (...args) => {
2
+ if (console?.warn) {
6
3
  if (isString(args[0])) args[0] = `react-i18next:: ${args[0]}`;
7
4
  console.warn(...args);
8
5
  }
9
- }
6
+ };
10
7
  const alreadyWarned = {};
11
- export function warnOnce() {
12
- for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
13
- args[_key2] = arguments[_key2];
14
- }
8
+ export const warnOnce = (...args) => {
15
9
  if (isString(args[0]) && alreadyWarned[args[0]]) return;
16
10
  if (isString(args[0])) alreadyWarned[args[0]] = new Date();
17
11
  warn(...args);
18
- }
12
+ };
19
13
  const loadedClb = (i18n, cb) => () => {
20
14
  if (i18n.isInitialized) {
21
15
  cb();
@@ -39,36 +33,15 @@ export const loadLanguages = (i18n, lng, ns, cb) => {
39
33
  });
40
34
  i18n.loadLanguages(lng, loadedClb(i18n, cb));
41
35
  };
42
- const oldI18nextHasLoadedNamespace = function (ns, i18n) {
43
- let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
44
- const lng = i18n.languages[0];
45
- const fallbackLng = i18n.options ? i18n.options.fallbackLng : false;
46
- const lastLng = i18n.languages[i18n.languages.length - 1];
47
- if (lng.toLowerCase() === 'cimode') return true;
48
- const loadNotPending = (l, n) => {
49
- const loadState = i18n.services.backendConnector.state[`${l}|${n}`];
50
- return loadState === -1 || loadState === 2;
51
- };
52
- if (options.bindI18n && options.bindI18n.indexOf('languageChanging') > -1 && i18n.services.backendConnector.backend && i18n.isLanguageChangingTo && !loadNotPending(i18n.isLanguageChangingTo, ns)) return false;
53
- if (i18n.hasResourceBundle(lng, ns)) return true;
54
- if (!i18n.services.backendConnector.backend || i18n.options.resources && !i18n.options.partialBundledLanguages) return true;
55
- if (loadNotPending(lng, ns) && (!fallbackLng || loadNotPending(lastLng, ns))) return true;
56
- return false;
57
- };
58
- export const hasLoadedNamespace = function (ns, i18n) {
59
- let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
36
+ export const hasLoadedNamespace = (ns, i18n, options = {}) => {
60
37
  if (!i18n.languages || !i18n.languages.length) {
61
38
  warnOnce('i18n.languages were undefined or empty', i18n.languages);
62
39
  return true;
63
40
  }
64
- const isNewerI18next = i18n.options.ignoreJSONStructure !== undefined;
65
- if (!isNewerI18next) {
66
- return oldI18nextHasLoadedNamespace(ns, i18n, options);
67
- }
68
41
  return i18n.hasLoadedNamespace(ns, {
69
42
  lng: options.lng,
70
43
  precheck: (i18nInstance, loadNotPending) => {
71
- if (options.bindI18n && options.bindI18n.indexOf('languageChanging') > -1 && i18nInstance.services.backendConnector.backend && i18nInstance.isLanguageChangingTo && !loadNotPending(i18nInstance.isLanguageChangingTo, ns)) return false;
44
+ if (options.bindI18n?.indexOf('languageChanging') > -1 && i18nInstance.services.backendConnector.backend && i18nInstance.isLanguageChangingTo && !loadNotPending(i18nInstance.isLanguageChangingTo, ns)) return false;
72
45
  }
73
46
  });
74
47
  };
@@ -3,12 +3,11 @@ import { useSSR } from './useSSR.js';
3
3
  import { composeInitialProps } from './context.js';
4
4
  import { getDisplayName } from './utils.js';
5
5
  export const withSSR = () => function Extend(WrappedComponent) {
6
- function I18nextWithSSR(_ref) {
7
- let {
8
- initialI18nStore,
9
- initialLanguage,
10
- ...rest
11
- } = _ref;
6
+ function I18nextWithSSR({
7
+ initialI18nStore,
8
+ initialLanguage,
9
+ ...rest
10
+ }) {
12
11
  useSSR(initialI18nStore, initialLanguage);
13
12
  return createElement(WrappedComponent, {
14
13
  ...rest
@@ -1,36 +1,32 @@
1
1
  import { createElement, forwardRef as forwardRefReact } from 'react';
2
2
  import { useTranslation } from './useTranslation.js';
3
3
  import { getDisplayName } from './utils.js';
4
- export const withTranslation = function (ns) {
5
- let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
6
- return function Extend(WrappedComponent) {
7
- function I18nextWithTranslation(_ref) {
8
- let {
9
- forwardedRef,
10
- ...rest
11
- } = _ref;
12
- const [t, i18n, ready] = useTranslation(ns, {
13
- ...rest,
14
- keyPrefix: options.keyPrefix
15
- });
16
- const passDownProps = {
17
- ...rest,
18
- t,
19
- i18n,
20
- tReady: ready
21
- };
22
- if (options.withRef && forwardedRef) {
23
- passDownProps.ref = forwardedRef;
24
- } else if (!options.withRef && forwardedRef) {
25
- passDownProps.forwardedRef = forwardedRef;
26
- }
27
- return createElement(WrappedComponent, passDownProps);
4
+ export const withTranslation = (ns, options = {}) => function Extend(WrappedComponent) {
5
+ function I18nextWithTranslation({
6
+ forwardedRef,
7
+ ...rest
8
+ }) {
9
+ const [t, i18n, ready] = useTranslation(ns, {
10
+ ...rest,
11
+ keyPrefix: options.keyPrefix
12
+ });
13
+ const passDownProps = {
14
+ ...rest,
15
+ t,
16
+ i18n,
17
+ tReady: ready
18
+ };
19
+ if (options.withRef && forwardedRef) {
20
+ passDownProps.ref = forwardedRef;
21
+ } else if (!options.withRef && forwardedRef) {
22
+ passDownProps.forwardedRef = forwardedRef;
28
23
  }
29
- I18nextWithTranslation.displayName = `withI18nextTranslation(${getDisplayName(WrappedComponent)})`;
30
- I18nextWithTranslation.WrappedComponent = WrappedComponent;
31
- const forwardRef = (props, ref) => createElement(I18nextWithTranslation, Object.assign({}, props, {
32
- forwardedRef: ref
33
- }));
34
- return options.withRef ? forwardRefReact(forwardRef) : I18nextWithTranslation;
35
- };
24
+ return createElement(WrappedComponent, passDownProps);
25
+ }
26
+ I18nextWithTranslation.displayName = `withI18nextTranslation(${getDisplayName(WrappedComponent)})`;
27
+ I18nextWithTranslation.WrappedComponent = WrappedComponent;
28
+ const forwardRef = (props, ref) => createElement(I18nextWithTranslation, Object.assign({}, props, {
29
+ forwardedRef: ref
30
+ }));
31
+ return options.withRef ? forwardRefReact(forwardRef) : I18nextWithTranslation;
36
32
  };