@tolgee/react 4.9.1 → 4.9.3-rc.d287ae9.0
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/GlobalContextPlugin.d.ts +4 -0
- package/dist/ReactPlugin.d.ts +5 -0
- package/dist/T.d.ts +3 -5
- package/dist/TolgeeProvider.d.ts +10 -9
- package/dist/TolgeeProvider.spec.d.ts +1 -1
- package/dist/__integration/T.spec.d.ts +1 -1
- package/dist/__integration/TolgeeProvider.spec.d.ts +3 -3
- package/dist/__integration/mockTranslations.d.ts +7 -0
- package/dist/__integration/namespaces.spec.d.ts +1 -0
- package/dist/__integration/testConfig.d.ts +0 -0
- package/dist/__integration/tools.d.ts +0 -0
- package/dist/__integration/useTolgee.spec.d.ts +1 -0
- package/dist/__integration/useTranslation.spec.d.ts +1 -1
- package/dist/hooks.d.ts +1 -0
- package/dist/index.d.ts +7 -6
- package/dist/tagsTools.d.ts +1 -2
- package/dist/tolgee-react.cjs.js +149 -173
- package/dist/tolgee-react.cjs.js.map +1 -1
- package/dist/tolgee-react.cjs.min.js +1 -1
- package/dist/tolgee-react.cjs.min.js.map +1 -1
- package/dist/tolgee-react.esm.min.mjs +2 -0
- package/dist/tolgee-react.esm.min.mjs.map +1 -0
- package/dist/tolgee-react.esm.mjs +251 -0
- package/dist/tolgee-react.esm.mjs.map +1 -0
- package/dist/tolgee-react.umd.js +152 -158
- package/dist/tolgee-react.umd.js.map +1 -1
- package/dist/tolgee-react.umd.min.js +1 -1
- package/dist/tolgee-react.umd.min.js.map +1 -1
- package/dist/types.d.ts +8 -3
- package/dist/useTolgee.d.ts +2 -0
- package/dist/useTolgeeContext.d.ts +1 -3
- package/dist/useTranslate.d.ts +5 -17
- package/dist/useTranslateInternal.d.ts +6 -0
- package/lib/GlobalContextPlugin.d.ts +4 -0
- package/lib/ReactPlugin.d.ts +5 -0
- package/lib/T.d.ts +3 -5
- package/lib/TolgeeProvider.d.ts +10 -9
- package/lib/hooks.d.ts +1 -0
- package/lib/index.d.ts +7 -6
- package/lib/tagsTools.d.ts +1 -2
- package/lib/types.d.ts +8 -3
- package/lib/useTolgee.d.ts +2 -0
- package/lib/useTolgeeContext.d.ts +1 -3
- package/lib/useTranslate.d.ts +5 -17
- package/lib/useTranslateInternal.d.ts +6 -0
- package/package.json +8 -6
- package/dist/mocks/mockTolgee.d.ts +0 -26
- package/dist/tolgee-react.esm.js +0 -262
- package/dist/tolgee-react.esm.js.map +0 -1
- package/dist/useCurrentLanguage.d.ts +0 -5
- package/dist/useSetLanguage.d.ts +0 -5
- package/dist/useTranslate.spec.d.ts +0 -1
- package/lib/useCurrentLanguage.d.ts +0 -5
- package/lib/useSetLanguage.d.ts +0 -5
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { TolgeePlugin } from '@tolgee/web';
|
|
2
|
+
import type { ReactOptions, TolgeeReactContext } from './types';
|
|
3
|
+
export declare const GlobalContextPlugin: (options?: Partial<ReactOptions>) => TolgeePlugin;
|
|
4
|
+
export declare function getGlobalContext(): TolgeeReactContext | undefined;
|
package/dist/T.d.ts
CHANGED
|
@@ -1,14 +1,12 @@
|
|
|
1
|
+
import { FallbackNSTranslation, TranslateParams } from '@tolgee/web';
|
|
1
2
|
import { FunctionComponent } from 'react';
|
|
2
3
|
import { ParamsTags } from './types';
|
|
3
4
|
declare type TProps = {
|
|
4
|
-
|
|
5
|
+
params?: TranslateParams<ParamsTags>;
|
|
5
6
|
children?: string;
|
|
6
7
|
noWrap?: boolean;
|
|
7
|
-
/**
|
|
8
|
-
* @deprecated Use noWrap to disable in-context wrapping
|
|
9
|
-
*/
|
|
10
|
-
strategy?: 'ELEMENT_WRAP' | 'TEXT_WRAP' | 'NO_WRAP';
|
|
11
8
|
keyName?: string;
|
|
9
|
+
ns?: FallbackNSTranslation;
|
|
12
10
|
};
|
|
13
11
|
export declare const T: FunctionComponent<TProps>;
|
|
14
12
|
export {};
|
package/dist/TolgeeProvider.d.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import React
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { TolgeeInstance } from '@tolgee/web';
|
|
3
|
+
import { ReactOptions, TolgeeReactContext } from './types';
|
|
4
|
+
export declare const DEFAULT_REACT_OPTIONS: ReactOptions;
|
|
5
|
+
export declare const TolgeeProviderContext: React.Context<TolgeeReactContext | undefined>;
|
|
6
|
+
declare type Props = {
|
|
7
|
+
tolgee: TolgeeInstance;
|
|
8
|
+
options?: ReactOptions;
|
|
9
|
+
fallback?: React.ReactNode;
|
|
5
10
|
};
|
|
6
|
-
export declare const
|
|
7
|
-
declare type TolgeeProviderProps = TolgeeConfig & {
|
|
8
|
-
loadingFallback?: ReactNode;
|
|
9
|
-
};
|
|
10
|
-
export declare const TolgeeProvider: FunctionComponent<PropsWithChildren<TolgeeProviderProps>>;
|
|
11
|
+
export declare const TolgeeProvider: React.FC<Props>;
|
|
11
12
|
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import '@testing-library/jest-dom
|
|
1
|
+
import '@testing-library/jest-dom';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import '@testing-library/jest-dom
|
|
1
|
+
import '@testing-library/jest-dom';
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import '@testing-library/jest-dom
|
|
1
|
+
import '@testing-library/jest-dom';
|
|
2
2
|
export declare const createFetchMock: () => {
|
|
3
3
|
fetch: import("jest-fetch-mock/types").FetchMock;
|
|
4
|
-
resolveCzech:
|
|
5
|
-
resolveEnglish:
|
|
4
|
+
resolveCzech: undefined;
|
|
5
|
+
resolveEnglish: undefined;
|
|
6
6
|
};
|
|
@@ -6,11 +6,18 @@ declare const _default: {
|
|
|
6
6
|
with_tags: string;
|
|
7
7
|
with_tag: string;
|
|
8
8
|
};
|
|
9
|
+
'en:test': {
|
|
10
|
+
test: string;
|
|
11
|
+
test_english_fallback: string;
|
|
12
|
+
};
|
|
9
13
|
cs: {
|
|
10
14
|
hello_world: string;
|
|
11
15
|
peter_dogs: string;
|
|
12
16
|
with_tags: string;
|
|
13
17
|
with_tag: string;
|
|
14
18
|
};
|
|
19
|
+
'cs:test': {
|
|
20
|
+
test: string;
|
|
21
|
+
};
|
|
15
22
|
};
|
|
16
23
|
export default _default;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import '@testing-library/jest-dom';
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import '@testing-library/jest-dom';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import '@testing-library/jest-dom
|
|
1
|
+
import '@testing-library/jest-dom';
|
package/dist/hooks.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const useRerender: () => () => void;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
export
|
|
1
|
+
export { useTranslate } from './useTranslate';
|
|
2
|
+
export { TolgeeProvider, TolgeeProviderContext } from './TolgeeProvider';
|
|
3
|
+
export { T } from './T';
|
|
4
|
+
export { useTolgee } from './useTolgee';
|
|
5
|
+
export { ReactPlugin } from './ReactPlugin';
|
|
6
|
+
export * from './types';
|
|
7
|
+
export * from '@tolgee/web';
|
package/dist/tagsTools.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
-
import { TranslationParamsTags } from '@tolgee/core';
|
|
3
2
|
import { ParamsTags } from './types';
|
|
4
|
-
export declare const wrapTagHandlers: (params: ParamsTags) =>
|
|
3
|
+
export declare const wrapTagHandlers: (params: ParamsTags) => any;
|
|
5
4
|
export declare const addReactKeys: (val: React.ReactNode | React.ReactNode[] | undefined) => React.ReactNode;
|
package/dist/tolgee-react.cjs.js
CHANGED
|
@@ -3,28 +3,10 @@
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
5
|
var React = require('react');
|
|
6
|
-
var
|
|
6
|
+
var web = require('@tolgee/web');
|
|
7
7
|
|
|
8
8
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
9
9
|
|
|
10
|
-
function _interopNamespace(e) {
|
|
11
|
-
if (e && e.__esModule) return e;
|
|
12
|
-
var n = Object.create(null);
|
|
13
|
-
if (e) {
|
|
14
|
-
Object.keys(e).forEach(function (k) {
|
|
15
|
-
if (k !== 'default') {
|
|
16
|
-
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
17
|
-
Object.defineProperty(n, k, d.get ? d : {
|
|
18
|
-
enumerable: true,
|
|
19
|
-
get: function () { return e[k]; }
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
n["default"] = e;
|
|
25
|
-
return Object.freeze(n);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
10
|
var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
|
|
29
11
|
|
|
30
12
|
/******************************************************************************
|
|
@@ -68,43 +50,131 @@ function __read(o, n) {
|
|
|
68
50
|
finally { if (e) throw e.error; }
|
|
69
51
|
}
|
|
70
52
|
return ar;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function __spreadArray(to, from, pack) {
|
|
56
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
57
|
+
if (ar || !(i in from)) {
|
|
58
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
59
|
+
ar[i] = from[i];
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
71
63
|
}
|
|
72
64
|
|
|
73
|
-
var
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
var
|
|
79
|
-
|
|
80
|
-
: typeof require !== 'undefined'
|
|
81
|
-
? require('@tolgee/ui')
|
|
82
|
-
: Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require('@tolgee/ui')); }) }, config))), 1), tolgee = _a[0];
|
|
83
|
-
var _b = __read(React.useState(tolgee.initialLoading), 2), loading = _b[0], setLoading = _b[1];
|
|
84
|
-
//rerender components on forceLanguage change
|
|
85
|
-
React.useEffect(function () {
|
|
86
|
-
if (config.forceLanguage !== undefined) {
|
|
87
|
-
tolgee.properties.config.forceLanguage = config.forceLanguage;
|
|
88
|
-
tolgee.lang = config.forceLanguage;
|
|
89
|
-
}
|
|
90
|
-
}, [config.forceLanguage]);
|
|
65
|
+
var DEFAULT_REACT_OPTIONS = {
|
|
66
|
+
useSuspense: true,
|
|
67
|
+
};
|
|
68
|
+
var TolgeeProviderContext = React__default["default"].createContext(undefined);
|
|
69
|
+
var TolgeeProvider = function (_a) {
|
|
70
|
+
var tolgee = _a.tolgee, options = _a.options, children = _a.children, fallback = _a.fallback;
|
|
71
|
+
var _b = __read(React.useState(!tolgee.isLoaded()), 2), loading = _b[0], setLoading = _b[1];
|
|
91
72
|
React.useEffect(function () {
|
|
92
|
-
tolgee.run().then(function () {
|
|
73
|
+
tolgee.run().then(function () {
|
|
74
|
+
setLoading(false);
|
|
75
|
+
});
|
|
93
76
|
return function () {
|
|
94
77
|
tolgee.stop();
|
|
95
78
|
};
|
|
96
79
|
}, []);
|
|
97
|
-
|
|
80
|
+
var optionsWithDefault = __assign(__assign({}, DEFAULT_REACT_OPTIONS), options);
|
|
81
|
+
if (optionsWithDefault.useSuspense) {
|
|
82
|
+
return (React__default["default"].createElement(TolgeeProviderContext.Provider, { value: { tolgee: tolgee, options: optionsWithDefault } },
|
|
83
|
+
React__default["default"].createElement(React.Suspense, { fallback: fallback || null }, children)));
|
|
84
|
+
}
|
|
85
|
+
return (React__default["default"].createElement(TolgeeProviderContext.Provider, { value: { tolgee: tolgee, options: optionsWithDefault } }, fallback && loading ? fallback : children));
|
|
98
86
|
};
|
|
99
87
|
|
|
88
|
+
var globalContext;
|
|
89
|
+
var GlobalContextPlugin = function (options) {
|
|
90
|
+
return function (tolgee) {
|
|
91
|
+
globalContext = {
|
|
92
|
+
tolgee: tolgee,
|
|
93
|
+
options: __assign(__assign({}, DEFAULT_REACT_OPTIONS), options),
|
|
94
|
+
};
|
|
95
|
+
return tolgee;
|
|
96
|
+
};
|
|
97
|
+
};
|
|
98
|
+
function getGlobalContext() {
|
|
99
|
+
return globalContext;
|
|
100
|
+
}
|
|
101
|
+
|
|
100
102
|
var useTolgeeContext = function () {
|
|
101
|
-
var context = React.useContext(TolgeeProviderContext);
|
|
102
|
-
if (context
|
|
103
|
-
throw new Error('
|
|
103
|
+
var context = React.useContext(TolgeeProviderContext) || getGlobalContext();
|
|
104
|
+
if (!context) {
|
|
105
|
+
throw new Error("Couldn't find tolgee instance, did you forgot to use `ReactPlugin`?");
|
|
104
106
|
}
|
|
105
107
|
return context;
|
|
106
108
|
};
|
|
107
109
|
|
|
110
|
+
var useTranslateInternal = function (ns, options) {
|
|
111
|
+
var _a = useTolgeeContext(), tolgee = _a.tolgee, defaultOptions = _a.options;
|
|
112
|
+
var namespaces = web.getFallback(ns);
|
|
113
|
+
var namespacesJoined = web.getFallbackArray(namespaces).join(':');
|
|
114
|
+
var currentOptions = __assign(__assign({}, defaultOptions), options);
|
|
115
|
+
// dummy state to enable re-rendering
|
|
116
|
+
var _b = __read(React.useState(0), 2), instance = _b[0], setInstance = _b[1];
|
|
117
|
+
var forceRerender = React.useCallback(function () {
|
|
118
|
+
setInstance(function (v) { return v + 1; });
|
|
119
|
+
}, [setInstance]);
|
|
120
|
+
var subscriptionRef = React.useRef();
|
|
121
|
+
var subscriptionQueue = React.useRef([]);
|
|
122
|
+
var subscribeToKey = function (key) {
|
|
123
|
+
if (subscriptionRef.current) {
|
|
124
|
+
subscriptionRef.current.subscribeKey(key);
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
subscriptionQueue.current.push(key);
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
React.useEffect(function () {
|
|
131
|
+
subscriptionRef.current = tolgee.onKeyUpdate(forceRerender);
|
|
132
|
+
subscriptionQueue.current.forEach(function (key) {
|
|
133
|
+
subscriptionRef.current.subscribeKey(key);
|
|
134
|
+
});
|
|
135
|
+
subscriptionQueue.current = [];
|
|
136
|
+
return function () {
|
|
137
|
+
subscriptionRef.current.unsubscribe();
|
|
138
|
+
};
|
|
139
|
+
}, []);
|
|
140
|
+
var isLoaded = tolgee.isLoaded(namespaces);
|
|
141
|
+
React.useEffect(function () {
|
|
142
|
+
if (!isLoaded) {
|
|
143
|
+
subscriptionRef.current.subscribeNs(namespaces);
|
|
144
|
+
return function () {
|
|
145
|
+
subscriptionRef.current.unsubscribeNs(namespaces);
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
}, [namespacesJoined, isLoaded]);
|
|
149
|
+
React.useEffect(function () {
|
|
150
|
+
tolgee.addActiveNs(namespaces);
|
|
151
|
+
return function () { return tolgee.removeActiveNs(namespaces); };
|
|
152
|
+
}, [namespacesJoined]);
|
|
153
|
+
var t = React.useCallback(function (props) {
|
|
154
|
+
var fallbackNs = props.ns || (namespaces === null || namespaces === void 0 ? void 0 : namespaces[0]);
|
|
155
|
+
subscribeToKey({ key: props.key, ns: fallbackNs });
|
|
156
|
+
return tolgee.t(__assign(__assign({}, props), { ns: fallbackNs }));
|
|
157
|
+
}, [tolgee, instance]);
|
|
158
|
+
if ((currentOptions === null || currentOptions === void 0 ? void 0 : currentOptions.useSuspense) && !isLoaded) {
|
|
159
|
+
throw tolgee.addActiveNs(namespaces, true);
|
|
160
|
+
}
|
|
161
|
+
return { t: t, isLoading: !isLoaded };
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
var useTranslate = function (ns, options) {
|
|
165
|
+
var _a = useTranslateInternal(ns, options), tInternal = _a.t, isLoading = _a.isLoading;
|
|
166
|
+
var t = React.useCallback(function () {
|
|
167
|
+
var params = [];
|
|
168
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
169
|
+
params[_i] = arguments[_i];
|
|
170
|
+
}
|
|
171
|
+
// @ts-ignore
|
|
172
|
+
var props = web.getTranslateParams.apply(void 0, __spreadArray([], __read(params), false));
|
|
173
|
+
return tInternal(props);
|
|
174
|
+
}, [tInternal]);
|
|
175
|
+
return { t: t, isLoading: isLoading };
|
|
176
|
+
};
|
|
177
|
+
|
|
108
178
|
var wrapTagHandlers = function (params) {
|
|
109
179
|
if (!params) {
|
|
110
180
|
return undefined;
|
|
@@ -120,7 +190,9 @@ var wrapTagHandlers = function (params) {
|
|
|
120
190
|
else if (React__default["default"].isValidElement(value)) {
|
|
121
191
|
var el_1 = value;
|
|
122
192
|
result[key] = function (chunk) {
|
|
123
|
-
return __assign(__assign({}, el_1), { props: __assign(__assign({}, el_1.props), { children:
|
|
193
|
+
return __assign(__assign({}, el_1), { props: __assign(__assign({}, el_1.props), { children: el_1.props.children !== undefined
|
|
194
|
+
? el_1.props.children
|
|
195
|
+
: addReactKeys(chunk) }) });
|
|
124
196
|
};
|
|
125
197
|
}
|
|
126
198
|
else {
|
|
@@ -138,156 +210,60 @@ var addReactKeys = function (val) {
|
|
|
138
210
|
}
|
|
139
211
|
};
|
|
140
212
|
|
|
141
|
-
var useTranslate = function () {
|
|
142
|
-
var tolgee = useTolgeeContext().tolgee;
|
|
143
|
-
var isMounted = React.useRef(false);
|
|
144
|
-
// dummy state to enable re-rendering
|
|
145
|
-
var _a = __read(React.useState(0), 2), instance = _a[0], setInstance = _a[1];
|
|
146
|
-
React.useEffect(function () {
|
|
147
|
-
isMounted.current = true;
|
|
148
|
-
return function () {
|
|
149
|
-
isMounted.current = false;
|
|
150
|
-
};
|
|
151
|
-
}, []);
|
|
152
|
-
var forceRerender = React.useCallback(function () {
|
|
153
|
-
if (isMounted.current) {
|
|
154
|
-
setInstance(function (v) { return v + 1; });
|
|
155
|
-
}
|
|
156
|
-
}, [setInstance, isMounted]);
|
|
157
|
-
// cache of translations translated with this useTranslate
|
|
158
|
-
var keysRef = React.useRef([]);
|
|
159
|
-
var resetMemory = function (key) {
|
|
160
|
-
keysRef.current = key ? keysRef.current.filter(function (k) { return k !== key; }) : [];
|
|
161
|
-
};
|
|
162
|
-
React.useEffect(function () {
|
|
163
|
-
var subscription = tolgee.onTranslationChange.subscribe(function (_a) {
|
|
164
|
-
var key = _a.key;
|
|
165
|
-
if (keysRef.current.includes(key)) {
|
|
166
|
-
resetMemory(key);
|
|
167
|
-
forceRerender();
|
|
168
|
-
}
|
|
169
|
-
});
|
|
170
|
-
return function () { return subscription.unsubscribe(); };
|
|
171
|
-
}, [tolgee]);
|
|
172
|
-
React.useEffect(function () {
|
|
173
|
-
var subscription = tolgee.onLangChange.subscribe(function () {
|
|
174
|
-
if (keysRef.current.length) {
|
|
175
|
-
resetMemory();
|
|
176
|
-
forceRerender();
|
|
177
|
-
}
|
|
178
|
-
});
|
|
179
|
-
return function () { return subscription.unsubscribe(); };
|
|
180
|
-
}, [tolgee]);
|
|
181
|
-
var getTranslation = React.useCallback(function (key, params, noWrap, defaultValue) {
|
|
182
|
-
var firstRender = !keysRef.current.includes(key);
|
|
183
|
-
var translation = tolgee.instant({
|
|
184
|
-
key: key,
|
|
185
|
-
params: wrapTagHandlers(params),
|
|
186
|
-
noWrap: noWrap,
|
|
187
|
-
defaultValue: defaultValue,
|
|
188
|
-
});
|
|
189
|
-
if (firstRender) {
|
|
190
|
-
keysRef.current.push(key);
|
|
191
|
-
tolgee
|
|
192
|
-
.translate({
|
|
193
|
-
key: key,
|
|
194
|
-
params: wrapTagHandlers(params),
|
|
195
|
-
noWrap: noWrap,
|
|
196
|
-
defaultValue: defaultValue,
|
|
197
|
-
})
|
|
198
|
-
.then(function (value) {
|
|
199
|
-
if (value !== translation) {
|
|
200
|
-
forceRerender();
|
|
201
|
-
}
|
|
202
|
-
});
|
|
203
|
-
}
|
|
204
|
-
return translation;
|
|
205
|
-
}, [tolgee, keysRef]);
|
|
206
|
-
var t = React.useCallback(function (keyOrProps) {
|
|
207
|
-
var params = [];
|
|
208
|
-
for (var _i = 1; _i < arguments.length; _i++) {
|
|
209
|
-
params[_i - 1] = arguments[_i];
|
|
210
|
-
}
|
|
211
|
-
var parameters = undefined;
|
|
212
|
-
var noWrap = undefined;
|
|
213
|
-
var defaultValue = undefined;
|
|
214
|
-
// allow user to pass object of params and make the code cleaner
|
|
215
|
-
var key = typeof keyOrProps === 'object' ? keyOrProps.key : keyOrProps;
|
|
216
|
-
if (typeof keyOrProps === 'object') {
|
|
217
|
-
parameters = keyOrProps.parameters;
|
|
218
|
-
noWrap = keyOrProps.noWrap;
|
|
219
|
-
defaultValue = keyOrProps.defaultValue;
|
|
220
|
-
}
|
|
221
|
-
else {
|
|
222
|
-
params.forEach(function (param) {
|
|
223
|
-
switch (typeof param) {
|
|
224
|
-
case 'object':
|
|
225
|
-
parameters = param;
|
|
226
|
-
break;
|
|
227
|
-
case 'boolean':
|
|
228
|
-
noWrap = param;
|
|
229
|
-
break;
|
|
230
|
-
case 'string':
|
|
231
|
-
defaultValue = param;
|
|
232
|
-
}
|
|
233
|
-
});
|
|
234
|
-
}
|
|
235
|
-
return addReactKeys(getTranslation(key, parameters, noWrap, defaultValue));
|
|
236
|
-
}, [getTranslation, instance]);
|
|
237
|
-
return t;
|
|
238
|
-
};
|
|
239
|
-
|
|
240
|
-
/**
|
|
241
|
-
* Custom react hook
|
|
242
|
-
* @return function accepting language abbreviation as parameter
|
|
243
|
-
*/
|
|
244
|
-
var useSetLanguage = function () {
|
|
245
|
-
var context = useTolgeeContext();
|
|
246
|
-
return function (language) { return context.tolgee.changeLanguage(language); };
|
|
247
|
-
};
|
|
248
|
-
|
|
249
213
|
var T = function (props) {
|
|
250
214
|
var key = props.keyName || props.children;
|
|
251
|
-
if (
|
|
215
|
+
if (key === undefined) {
|
|
252
216
|
// eslint-disable-next-line no-console
|
|
253
217
|
console.error('T component: keyName not defined');
|
|
254
218
|
}
|
|
255
219
|
var defaultValue = props.keyName ? props.children : undefined;
|
|
256
|
-
var t =
|
|
257
|
-
var translation = t({
|
|
220
|
+
var t = useTranslateInternal().t;
|
|
221
|
+
var translation = addReactKeys(t({
|
|
258
222
|
key: key,
|
|
259
|
-
|
|
223
|
+
params: wrapTagHandlers(props.params),
|
|
260
224
|
defaultValue: defaultValue,
|
|
261
225
|
noWrap: props.noWrap,
|
|
262
|
-
|
|
226
|
+
ns: props.ns,
|
|
227
|
+
}));
|
|
263
228
|
return React__default["default"].createElement(React__default["default"].Fragment, null, translation);
|
|
264
229
|
};
|
|
265
230
|
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
231
|
+
var useRerender = function () {
|
|
232
|
+
var _a = __read(React.useState(0), 2); _a[0]; var setCounter = _a[1];
|
|
233
|
+
var rerender = React.useCallback(function () {
|
|
234
|
+
setCounter(function (num) { return num + 1; });
|
|
235
|
+
}, [setCounter]);
|
|
236
|
+
return rerender;
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
var useTolgee = function (events) {
|
|
240
|
+
var tolgee = useTolgeeContext().tolgee;
|
|
241
|
+
var rerender = useRerender();
|
|
274
242
|
React.useEffect(function () {
|
|
275
|
-
var
|
|
276
|
-
setLanguage(lang);
|
|
277
|
-
});
|
|
243
|
+
var listeners = events === null || events === void 0 ? void 0 : events.map(function (e) { return tolgee.on(e, rerender); });
|
|
278
244
|
return function () {
|
|
279
|
-
|
|
245
|
+
listeners === null || listeners === void 0 ? void 0 : listeners.forEach(function (listener) { return listener.unsubscribe(); });
|
|
280
246
|
};
|
|
281
|
-
}, []);
|
|
282
|
-
return
|
|
283
|
-
|
|
247
|
+
}, [events === null || events === void 0 ? void 0 : events.join(':')]);
|
|
248
|
+
return tolgee;
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
var ReactPlugin = function (props) {
|
|
252
|
+
return function (tolgee) {
|
|
253
|
+
return tolgee.use(web.DevTools()).use(GlobalContextPlugin(props));
|
|
284
254
|
};
|
|
285
255
|
};
|
|
286
256
|
|
|
257
|
+
exports.ReactPlugin = ReactPlugin;
|
|
287
258
|
exports.T = T;
|
|
288
259
|
exports.TolgeeProvider = TolgeeProvider;
|
|
289
260
|
exports.TolgeeProviderContext = TolgeeProviderContext;
|
|
290
|
-
exports.
|
|
291
|
-
exports.useSetLanguage = useSetLanguage;
|
|
261
|
+
exports.useTolgee = useTolgee;
|
|
292
262
|
exports.useTranslate = useTranslate;
|
|
263
|
+
Object.keys(web).forEach(function (k) {
|
|
264
|
+
if (k !== 'default' && !exports.hasOwnProperty(k)) Object.defineProperty(exports, k, {
|
|
265
|
+
enumerable: true,
|
|
266
|
+
get: function () { return web[k]; }
|
|
267
|
+
});
|
|
268
|
+
});
|
|
293
269
|
//# sourceMappingURL=tolgee-react.cjs.js.map
|