@tolgee/react 4.9.2 → 5.0.0-rc.0b3e627.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 +6 -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 +8 -6
- package/dist/tagsTools.d.ts +1 -2
- package/dist/tolgee-react.cjs.js +158 -172
- 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 +256 -0
- package/dist/tolgee-react.esm.mjs.map +1 -0
- package/dist/tolgee-react.umd.js +161 -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 +6 -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 +8 -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 +7 -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/core';
|
|
2
|
+
import type { ReactOptions, TolgeeReactContext } from './types';
|
|
3
|
+
export declare const GlobalContextPlugin: (options?: Partial<ReactOptions>) => TolgeePlugin;
|
|
4
|
+
export declare function getGlobalContext(): TolgeeReactContext | undefined;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { TolgeePlugin } from '@tolgee/core';
|
|
2
|
+
import { ObserverOptions } from '@tolgee/devtools-web';
|
|
3
|
+
import { ReactOptions } from './types';
|
|
4
|
+
declare type Props = Partial<ObserverOptions> & Partial<ReactOptions>;
|
|
5
|
+
export declare const ReactPlugin: (props?: Props) => TolgeePlugin;
|
|
6
|
+
export {};
|
package/dist/T.d.ts
CHANGED
|
@@ -1,14 +1,12 @@
|
|
|
1
|
+
import { FallbackNSTranslation, TranslateParams } from '@tolgee/core';
|
|
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/core';
|
|
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,8 @@
|
|
|
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/core';
|
|
8
|
+
export * from '@tolgee/devtools-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
|
@@ -4,27 +4,10 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
4
4
|
|
|
5
5
|
var React = require('react');
|
|
6
6
|
var core = require('@tolgee/core');
|
|
7
|
+
var devtoolsWeb = require('@tolgee/devtools-web');
|
|
7
8
|
|
|
8
9
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
9
10
|
|
|
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
11
|
var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
|
|
29
12
|
|
|
30
13
|
/******************************************************************************
|
|
@@ -68,43 +51,131 @@ function __read(o, n) {
|
|
|
68
51
|
finally { if (e) throw e.error; }
|
|
69
52
|
}
|
|
70
53
|
return ar;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function __spreadArray(to, from, pack) {
|
|
57
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
58
|
+
if (ar || !(i in from)) {
|
|
59
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
60
|
+
ar[i] = from[i];
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
71
64
|
}
|
|
72
65
|
|
|
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]);
|
|
66
|
+
var DEFAULT_REACT_OPTIONS = {
|
|
67
|
+
useSuspense: true,
|
|
68
|
+
};
|
|
69
|
+
var TolgeeProviderContext = React__default["default"].createContext(undefined);
|
|
70
|
+
var TolgeeProvider = function (_a) {
|
|
71
|
+
var tolgee = _a.tolgee, options = _a.options, children = _a.children, fallback = _a.fallback;
|
|
72
|
+
var _b = __read(React.useState(!tolgee.isLoaded()), 2), loading = _b[0], setLoading = _b[1];
|
|
91
73
|
React.useEffect(function () {
|
|
92
|
-
tolgee.run().then(function () {
|
|
74
|
+
tolgee.run().then(function () {
|
|
75
|
+
setLoading(false);
|
|
76
|
+
});
|
|
93
77
|
return function () {
|
|
94
78
|
tolgee.stop();
|
|
95
79
|
};
|
|
96
80
|
}, []);
|
|
97
|
-
|
|
81
|
+
var optionsWithDefault = __assign(__assign({}, DEFAULT_REACT_OPTIONS), options);
|
|
82
|
+
if (optionsWithDefault.useSuspense) {
|
|
83
|
+
return (React__default["default"].createElement(TolgeeProviderContext.Provider, { value: { tolgee: tolgee, options: optionsWithDefault } },
|
|
84
|
+
React__default["default"].createElement(React.Suspense, { fallback: fallback || null }, children)));
|
|
85
|
+
}
|
|
86
|
+
return (React__default["default"].createElement(TolgeeProviderContext.Provider, { value: { tolgee: tolgee, options: optionsWithDefault } }, fallback && loading ? fallback : children));
|
|
98
87
|
};
|
|
99
88
|
|
|
89
|
+
var globalContext;
|
|
90
|
+
var GlobalContextPlugin = function (options) {
|
|
91
|
+
return function (tolgee) {
|
|
92
|
+
globalContext = {
|
|
93
|
+
tolgee: tolgee,
|
|
94
|
+
options: __assign(__assign({}, DEFAULT_REACT_OPTIONS), options),
|
|
95
|
+
};
|
|
96
|
+
return tolgee;
|
|
97
|
+
};
|
|
98
|
+
};
|
|
99
|
+
function getGlobalContext() {
|
|
100
|
+
return globalContext;
|
|
101
|
+
}
|
|
102
|
+
|
|
100
103
|
var useTolgeeContext = function () {
|
|
101
|
-
var context = React.useContext(TolgeeProviderContext);
|
|
102
|
-
if (context
|
|
103
|
-
throw new Error('
|
|
104
|
+
var context = React.useContext(TolgeeProviderContext) || getGlobalContext();
|
|
105
|
+
if (!context) {
|
|
106
|
+
throw new Error("Couldn't find tolgee instance, did you forgot to use `ReactPlugin`?");
|
|
104
107
|
}
|
|
105
108
|
return context;
|
|
106
109
|
};
|
|
107
110
|
|
|
111
|
+
var useTranslateInternal = function (ns, options) {
|
|
112
|
+
var _a = useTolgeeContext(), tolgee = _a.tolgee, defaultOptions = _a.options;
|
|
113
|
+
var namespaces = core.getFallback(ns);
|
|
114
|
+
var namespacesJoined = core.getFallbackArray(namespaces).join(':');
|
|
115
|
+
var currentOptions = __assign(__assign({}, defaultOptions), options);
|
|
116
|
+
// dummy state to enable re-rendering
|
|
117
|
+
var _b = __read(React.useState(0), 2), instance = _b[0], setInstance = _b[1];
|
|
118
|
+
var forceRerender = React.useCallback(function () {
|
|
119
|
+
setInstance(function (v) { return v + 1; });
|
|
120
|
+
}, [setInstance]);
|
|
121
|
+
var subscriptionRef = React.useRef();
|
|
122
|
+
var subscriptionQueue = React.useRef([]);
|
|
123
|
+
var subscribeToKey = function (key) {
|
|
124
|
+
if (subscriptionRef.current) {
|
|
125
|
+
subscriptionRef.current.subscribeKey(key);
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
subscriptionQueue.current.push(key);
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
React.useEffect(function () {
|
|
132
|
+
subscriptionRef.current = tolgee.onKeyUpdate(forceRerender);
|
|
133
|
+
subscriptionQueue.current.forEach(function (key) {
|
|
134
|
+
subscriptionRef.current.subscribeKey(key);
|
|
135
|
+
});
|
|
136
|
+
subscriptionQueue.current = [];
|
|
137
|
+
return function () {
|
|
138
|
+
subscriptionRef.current.unsubscribe();
|
|
139
|
+
};
|
|
140
|
+
}, []);
|
|
141
|
+
var isLoaded = tolgee.isLoaded(namespaces);
|
|
142
|
+
React.useEffect(function () {
|
|
143
|
+
if (!isLoaded) {
|
|
144
|
+
subscriptionRef.current.subscribeNs(namespaces);
|
|
145
|
+
return function () {
|
|
146
|
+
subscriptionRef.current.unsubscribeNs(namespaces);
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
}, [namespacesJoined, isLoaded]);
|
|
150
|
+
React.useEffect(function () {
|
|
151
|
+
tolgee.addActiveNs(namespaces);
|
|
152
|
+
return function () { return tolgee.removeActiveNs(namespaces); };
|
|
153
|
+
}, [namespacesJoined]);
|
|
154
|
+
var t = React.useCallback(function (props) {
|
|
155
|
+
var fallbackNs = props.ns || (namespaces === null || namespaces === void 0 ? void 0 : namespaces[0]);
|
|
156
|
+
subscribeToKey({ key: props.key, ns: fallbackNs });
|
|
157
|
+
return tolgee.t(__assign(__assign({}, props), { ns: fallbackNs }));
|
|
158
|
+
}, [tolgee, instance]);
|
|
159
|
+
if ((currentOptions === null || currentOptions === void 0 ? void 0 : currentOptions.useSuspense) && !isLoaded) {
|
|
160
|
+
throw tolgee.addActiveNs(namespaces, true);
|
|
161
|
+
}
|
|
162
|
+
return { t: t, isLoading: !isLoaded };
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
var useTranslate = function (ns, options) {
|
|
166
|
+
var _a = useTranslateInternal(ns, options), tInternal = _a.t, isLoading = _a.isLoading;
|
|
167
|
+
var t = React.useCallback(function () {
|
|
168
|
+
var params = [];
|
|
169
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
170
|
+
params[_i] = arguments[_i];
|
|
171
|
+
}
|
|
172
|
+
// @ts-ignore
|
|
173
|
+
var props = core.getTranslateParams.apply(void 0, __spreadArray([], __read(params), false));
|
|
174
|
+
return tInternal(props);
|
|
175
|
+
}, [tInternal]);
|
|
176
|
+
return { t: t, isLoading: isLoading };
|
|
177
|
+
};
|
|
178
|
+
|
|
108
179
|
var wrapTagHandlers = function (params) {
|
|
109
180
|
if (!params) {
|
|
110
181
|
return undefined;
|
|
@@ -120,7 +191,9 @@ var wrapTagHandlers = function (params) {
|
|
|
120
191
|
else if (React__default["default"].isValidElement(value)) {
|
|
121
192
|
var el_1 = value;
|
|
122
193
|
result[key] = function (chunk) {
|
|
123
|
-
return __assign(__assign({}, el_1), { props: __assign(__assign({}, el_1.props), { children:
|
|
194
|
+
return __assign(__assign({}, el_1), { props: __assign(__assign({}, el_1.props), { children: el_1.props.children !== undefined
|
|
195
|
+
? el_1.props.children
|
|
196
|
+
: addReactKeys(chunk) }) });
|
|
124
197
|
};
|
|
125
198
|
}
|
|
126
199
|
else {
|
|
@@ -138,156 +211,69 @@ var addReactKeys = function (val) {
|
|
|
138
211
|
}
|
|
139
212
|
};
|
|
140
213
|
|
|
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
214
|
var T = function (props) {
|
|
250
215
|
var key = props.keyName || props.children;
|
|
251
|
-
if (
|
|
216
|
+
if (key === undefined) {
|
|
252
217
|
// eslint-disable-next-line no-console
|
|
253
218
|
console.error('T component: keyName not defined');
|
|
254
219
|
}
|
|
255
220
|
var defaultValue = props.keyName ? props.children : undefined;
|
|
256
|
-
var t =
|
|
257
|
-
var translation = t({
|
|
221
|
+
var t = useTranslateInternal().t;
|
|
222
|
+
var translation = addReactKeys(t({
|
|
258
223
|
key: key,
|
|
259
|
-
|
|
224
|
+
params: wrapTagHandlers(props.params),
|
|
260
225
|
defaultValue: defaultValue,
|
|
261
226
|
noWrap: props.noWrap,
|
|
262
|
-
|
|
227
|
+
ns: props.ns,
|
|
228
|
+
}));
|
|
263
229
|
return React__default["default"].createElement(React__default["default"].Fragment, null, translation);
|
|
264
230
|
};
|
|
265
231
|
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
232
|
+
var useRerender = function () {
|
|
233
|
+
var _a = __read(React.useState(0), 2); _a[0]; var setCounter = _a[1];
|
|
234
|
+
var rerender = React.useCallback(function () {
|
|
235
|
+
setCounter(function (num) { return num + 1; });
|
|
236
|
+
}, [setCounter]);
|
|
237
|
+
return rerender;
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
var useTolgee = function (events) {
|
|
241
|
+
var tolgee = useTolgeeContext().tolgee;
|
|
242
|
+
var rerender = useRerender();
|
|
274
243
|
React.useEffect(function () {
|
|
275
|
-
var
|
|
276
|
-
setLanguage(lang);
|
|
277
|
-
});
|
|
244
|
+
var listeners = events === null || events === void 0 ? void 0 : events.map(function (e) { return tolgee.on(e, rerender); });
|
|
278
245
|
return function () {
|
|
279
|
-
|
|
246
|
+
listeners === null || listeners === void 0 ? void 0 : listeners.forEach(function (listener) { return listener.unsubscribe(); });
|
|
280
247
|
};
|
|
281
|
-
}, []);
|
|
282
|
-
return
|
|
283
|
-
|
|
248
|
+
}, [events === null || events === void 0 ? void 0 : events.join(':')]);
|
|
249
|
+
return tolgee;
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
var ReactPlugin = function (props) {
|
|
253
|
+
return function (tolgee) {
|
|
254
|
+
return tolgee
|
|
255
|
+
.use(devtoolsWeb.BrowserExtensionPlugin({ noReload: true }))
|
|
256
|
+
.use(devtoolsWeb.DevTools({ observer: props }))
|
|
257
|
+
.use(GlobalContextPlugin(props));
|
|
284
258
|
};
|
|
285
259
|
};
|
|
286
260
|
|
|
261
|
+
exports.ReactPlugin = ReactPlugin;
|
|
287
262
|
exports.T = T;
|
|
288
263
|
exports.TolgeeProvider = TolgeeProvider;
|
|
289
264
|
exports.TolgeeProviderContext = TolgeeProviderContext;
|
|
290
|
-
exports.
|
|
291
|
-
exports.useSetLanguage = useSetLanguage;
|
|
265
|
+
exports.useTolgee = useTolgee;
|
|
292
266
|
exports.useTranslate = useTranslate;
|
|
267
|
+
Object.keys(core).forEach(function (k) {
|
|
268
|
+
if (k !== 'default' && !exports.hasOwnProperty(k)) Object.defineProperty(exports, k, {
|
|
269
|
+
enumerable: true,
|
|
270
|
+
get: function () { return core[k]; }
|
|
271
|
+
});
|
|
272
|
+
});
|
|
273
|
+
Object.keys(devtoolsWeb).forEach(function (k) {
|
|
274
|
+
if (k !== 'default' && !exports.hasOwnProperty(k)) Object.defineProperty(exports, k, {
|
|
275
|
+
enumerable: true,
|
|
276
|
+
get: function () { return devtoolsWeb[k]; }
|
|
277
|
+
});
|
|
278
|
+
});
|
|
293
279
|
//# sourceMappingURL=tolgee-react.cjs.js.map
|