@lingui/react 3.17.0 → 3.17.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/CHANGELOG.md +11 -0
- package/build/cjs/index.js +105 -159
- package/build/cjs/index.js.map +1 -1
- package/build/esm/index.js +105 -159
- package/build/esm/index.js.map +1 -1
- package/build/index.d.ts +6 -6
- package/package.json +4 -4
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,17 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [3.17.1](https://github.com/lingui/js-lingui/compare/v3.17.0...v3.17.1) (2023-02-07)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Bug Fixes
|
|
10
|
+
|
|
11
|
+
* Named components not working in Trans in @lingui/react ([#1402](https://github.com/lingui/js-lingui/issues/1402)) ([bf7f655](https://github.com/lingui/js-lingui/commit/bf7f655ccac3fc22fb7d36662ab0ec96595574e5))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
6
17
|
# [3.17.0](https://github.com/lingui/js-lingui/compare/v3.16.1...v3.17.0) (2023-02-01)
|
|
7
18
|
|
|
8
19
|
**Note:** Version bump only for package @lingui/react
|
package/build/cjs/index.js
CHANGED
|
@@ -1,47 +1,42 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
|
|
4
3
|
var _extends = require('@babel/runtime/helpers/extends');
|
|
5
4
|
var React = require('react');
|
|
6
|
-
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
|
|
7
5
|
|
|
8
|
-
|
|
6
|
+
const LinguiContext = /*#__PURE__*/React.createContext(null);
|
|
9
7
|
function useLingui() {
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
const context = React.useContext(LinguiContext);
|
|
12
9
|
if (process.env.NODE_ENV !== "production") {
|
|
13
10
|
if (context == null) {
|
|
14
11
|
throw new Error("useLingui hook was used without I18nProvider.");
|
|
15
12
|
}
|
|
16
13
|
}
|
|
17
|
-
|
|
18
14
|
return context;
|
|
19
15
|
}
|
|
20
16
|
function withI18n(o) {
|
|
21
|
-
return
|
|
22
|
-
return
|
|
17
|
+
return WrappedComponent => {
|
|
18
|
+
return props => {
|
|
23
19
|
if (process.env.NODE_ENV !== "production") {
|
|
24
20
|
if (typeof o === "function" || /*#__PURE__*/React.isValidElement(o)) {
|
|
25
21
|
throw new Error("withI18n([options]) takes options as a first argument, " + "but received React component itself. Without options, the Component " + "should be wrapped as withI18n()(Component), not withI18n(Component).");
|
|
26
22
|
}
|
|
27
23
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
24
|
+
const {
|
|
25
|
+
i18n
|
|
26
|
+
} = useLingui();
|
|
32
27
|
return /*#__PURE__*/React.createElement(WrappedComponent, _extends({}, props, {
|
|
33
28
|
i18n: i18n
|
|
34
29
|
}));
|
|
35
30
|
};
|
|
36
31
|
};
|
|
37
32
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
33
|
+
const I18nProvider = _ref => {
|
|
34
|
+
let {
|
|
35
|
+
i18n,
|
|
36
|
+
defaultComponent,
|
|
37
|
+
forceRenderOnLocaleChange = true,
|
|
38
|
+
children
|
|
39
|
+
} = _ref;
|
|
45
40
|
/**
|
|
46
41
|
* We can't pass `i18n` object directly through context, because even when locale
|
|
47
42
|
* or messages are changed, i18n object is still the same. Context provider compares
|
|
@@ -53,25 +48,16 @@ var I18nProvider = function I18nProvider(_ref) {
|
|
|
53
48
|
*
|
|
54
49
|
* We can't use useMemo hook either, because we want to recalculate value manually.
|
|
55
50
|
*/
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
51
|
+
const makeContext = () => ({
|
|
52
|
+
i18n,
|
|
53
|
+
defaultComponent
|
|
54
|
+
});
|
|
55
|
+
const getRenderKey = () => {
|
|
56
|
+
return forceRenderOnLocaleChange ? i18n.locale || "default" : "default";
|
|
61
57
|
};
|
|
58
|
+
const [context, setContext] = React.useState(makeContext()),
|
|
59
|
+
[renderKey, setRenderKey] = React.useState(getRenderKey());
|
|
62
60
|
|
|
63
|
-
var getRenderKey = function getRenderKey() {
|
|
64
|
-
return forceRenderOnLocaleChange ? i18n.locale || 'default' : 'default';
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
var _React$useState = React.useState(makeContext()),
|
|
68
|
-
_React$useState2 = _slicedToArray(_React$useState, 2),
|
|
69
|
-
context = _React$useState2[0],
|
|
70
|
-
setContext = _React$useState2[1],
|
|
71
|
-
_React$useState3 = React.useState(getRenderKey()),
|
|
72
|
-
_React$useState4 = _slicedToArray(_React$useState3, 2),
|
|
73
|
-
renderKey = _React$useState4[0],
|
|
74
|
-
setRenderKey = _React$useState4[1];
|
|
75
61
|
/**
|
|
76
62
|
* Subscribe for locale/message changes
|
|
77
63
|
*
|
|
@@ -84,44 +70,33 @@ var I18nProvider = function I18nProvider(_ref) {
|
|
|
84
70
|
* `useEffect` hook being called. This can happen if locales are loaded/activated
|
|
85
71
|
* async.
|
|
86
72
|
*/
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
React.useEffect(function () {
|
|
90
|
-
var unsubscribe = i18n.on("change", function () {
|
|
73
|
+
React.useEffect(() => {
|
|
74
|
+
const unsubscribe = i18n.on("change", () => {
|
|
91
75
|
setContext(makeContext());
|
|
92
76
|
setRenderKey(getRenderKey());
|
|
93
77
|
});
|
|
94
|
-
|
|
95
|
-
if (renderKey === 'default') {
|
|
78
|
+
if (renderKey === "default") {
|
|
96
79
|
setRenderKey(getRenderKey());
|
|
97
80
|
}
|
|
98
|
-
|
|
99
|
-
if (forceRenderOnLocaleChange && renderKey === 'default') {
|
|
81
|
+
if (forceRenderOnLocaleChange && renderKey === "default") {
|
|
100
82
|
console.log("I18nProvider did not render. A call to i18n.activate still needs to happen or forceRenderOnLocaleChange must be set to false.");
|
|
101
83
|
}
|
|
102
|
-
|
|
103
|
-
return function () {
|
|
104
|
-
return unsubscribe();
|
|
105
|
-
};
|
|
84
|
+
return () => unsubscribe();
|
|
106
85
|
}, []);
|
|
107
|
-
if (forceRenderOnLocaleChange && renderKey ===
|
|
86
|
+
if (forceRenderOnLocaleChange && renderKey === "default") return null;
|
|
108
87
|
return /*#__PURE__*/React.createElement(LinguiContext.Provider, {
|
|
109
88
|
value: context,
|
|
110
89
|
key: renderKey
|
|
111
90
|
}, children);
|
|
112
91
|
};
|
|
113
92
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
93
|
+
// match <tag>paired</tag> and <tag/> unpaired tags
|
|
94
|
+
const tagRe = /<([a-zA-Z0-9]+)>(.*?)<\/\1>|<([a-zA-Z0-9]+)\/>/;
|
|
95
|
+
const nlRe = /(?:\r\n|\r|\n)/g;
|
|
119
96
|
|
|
120
|
-
|
|
121
|
-
var nlRe = /(?:\r\n|\r|\n)/g; // For HTML, certain tags should omit their close tag. We keep a whitelist for
|
|
97
|
+
// For HTML, certain tags should omit their close tag. We keep a whitelist for
|
|
122
98
|
// those special-case tags.
|
|
123
|
-
|
|
124
|
-
var voidElementTags = {
|
|
99
|
+
const voidElementTags = {
|
|
125
100
|
area: true,
|
|
126
101
|
base: true,
|
|
127
102
|
br: true,
|
|
@@ -139,62 +114,47 @@ var voidElementTags = {
|
|
|
139
114
|
wbr: true,
|
|
140
115
|
menuitem: true
|
|
141
116
|
};
|
|
117
|
+
|
|
142
118
|
/**
|
|
143
119
|
* `formatElements` - parse string and return tree of react elements
|
|
144
120
|
*
|
|
145
|
-
* `value` is string to be formatted with <
|
|
121
|
+
* `value` is string to be formatted with <tag>Paired<tag/> or <tag/> (unpaired)
|
|
146
122
|
* placeholders. `elements` is a array of react elements which indexes
|
|
147
123
|
* correspond to element indexes in formatted string
|
|
148
124
|
*/
|
|
149
|
-
|
|
150
125
|
function formatElements(value) {
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
126
|
+
let elements = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
127
|
+
const uniqueId = makeCounter(0, "$lingui$");
|
|
128
|
+
const parts = value.replace(nlRe, "").split(tagRe);
|
|
154
129
|
|
|
130
|
+
// no inline elements, return
|
|
155
131
|
if (parts.length === 1) return value;
|
|
156
|
-
|
|
157
|
-
|
|
132
|
+
const tree = [];
|
|
133
|
+
const before = parts.shift();
|
|
158
134
|
if (before) tree.push(before);
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
index = _step$value[0],
|
|
167
|
-
children = _step$value[1],
|
|
168
|
-
after = _step$value[2];
|
|
169
|
-
|
|
170
|
-
var element = elements[index];
|
|
171
|
-
|
|
172
|
-
if (!element || voidElementTags[element.type] && children) {
|
|
173
|
-
if (!element) {
|
|
174
|
-
console.error("Can use element at index '".concat(index, "' as it is not declared in the original translation"));
|
|
175
|
-
} else {
|
|
176
|
-
console.error("".concat(element.type, " is a void element tag therefore it must have no children"));
|
|
177
|
-
} // ignore problematic element but push its children and elements after it
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
element = /*#__PURE__*/React.createElement(React.Fragment);
|
|
135
|
+
for (const [index, children, after] of getElements(parts)) {
|
|
136
|
+
let element = elements[index];
|
|
137
|
+
if (!element || voidElementTags[element.type] && children) {
|
|
138
|
+
if (!element) {
|
|
139
|
+
console.error(`Can use element at index '${index}' as it is not declared in the original translation`);
|
|
140
|
+
} else {
|
|
141
|
+
console.error(`${element.type} is a void element tag therefore it must have no children`);
|
|
181
142
|
}
|
|
182
143
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
}, // format children for pair tags
|
|
186
|
-
// unpaired tags might have children if it's a component passed as a variable
|
|
187
|
-
children ? formatElements(children, elements) : element.props.children));
|
|
188
|
-
if (after) tree.push(after);
|
|
144
|
+
// ignore problematic element but push its children and elements after it
|
|
145
|
+
element = /*#__PURE__*/React.createElement(React.Fragment);
|
|
189
146
|
}
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
147
|
+
tree.push( /*#__PURE__*/React.cloneElement(element, {
|
|
148
|
+
key: uniqueId()
|
|
149
|
+
},
|
|
150
|
+
// format children for pair tags
|
|
151
|
+
// unpaired tags might have children if it's a component passed as a variable
|
|
152
|
+
children ? formatElements(children, elements) : element.props.children));
|
|
153
|
+
if (after) tree.push(after);
|
|
194
154
|
}
|
|
195
|
-
|
|
196
155
|
return tree;
|
|
197
156
|
}
|
|
157
|
+
|
|
198
158
|
/*
|
|
199
159
|
* `getElements` - return array of element indexes and element childrens
|
|
200
160
|
*
|
|
@@ -209,47 +169,35 @@ function formatElements(value) {
|
|
|
209
169
|
*
|
|
210
170
|
* Returns: Array<[elementIndex, children, after]>
|
|
211
171
|
*/
|
|
212
|
-
|
|
213
|
-
|
|
214
172
|
function getElements(parts) {
|
|
215
173
|
if (!parts.length) return [];
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
_parts$slice2 = _slicedToArray(_parts$slice, 4),
|
|
219
|
-
paired = _parts$slice2[0],
|
|
220
|
-
children = _parts$slice2[1],
|
|
221
|
-
unpaired = _parts$slice2[2],
|
|
222
|
-
after = _parts$slice2[3];
|
|
223
|
-
|
|
224
|
-
return [[parseInt(paired || unpaired), children || "", after]].concat(getElements(parts.slice(4, parts.length)));
|
|
174
|
+
const [paired, children, unpaired, after] = parts.slice(0, 4);
|
|
175
|
+
return [[paired || unpaired, children || "", after]].concat(getElements(parts.slice(4, parts.length)));
|
|
225
176
|
}
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
return function () {
|
|
231
|
-
return "".concat(prefix, "_").concat(count++);
|
|
232
|
-
};
|
|
177
|
+
const makeCounter = function () {
|
|
178
|
+
let count = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
|
|
179
|
+
let prefix = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "";
|
|
180
|
+
return () => `${prefix}_${count++}`;
|
|
233
181
|
};
|
|
234
182
|
|
|
235
|
-
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; }
|
|
236
|
-
|
|
237
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
|
238
183
|
function Trans(props) {
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
184
|
+
const {
|
|
185
|
+
i18n,
|
|
186
|
+
defaultComponent
|
|
187
|
+
} = useLingui();
|
|
188
|
+
const {
|
|
189
|
+
render,
|
|
190
|
+
component,
|
|
191
|
+
id,
|
|
192
|
+
message,
|
|
193
|
+
formats
|
|
194
|
+
} = props;
|
|
195
|
+
const values = {
|
|
196
|
+
...props.values
|
|
197
|
+
};
|
|
198
|
+
const components = {
|
|
199
|
+
...props.components
|
|
200
|
+
};
|
|
253
201
|
if (values) {
|
|
254
202
|
/*
|
|
255
203
|
Related discussion: https://github.com/lingui/js-lingui/issues/183
|
|
@@ -260,57 +208,55 @@ function Trans(props) {
|
|
|
260
208
|
Values: { name: <strong>Jane</strong> }
|
|
261
209
|
It'll become "Hello <0 />" with components=[<strong>Jane</strong>]
|
|
262
210
|
*/
|
|
263
|
-
|
|
264
|
-
|
|
211
|
+
|
|
212
|
+
Object.keys(values).forEach(key => {
|
|
213
|
+
const value = values[key];
|
|
265
214
|
if (! /*#__PURE__*/React.isValidElement(value)) return;
|
|
266
|
-
|
|
215
|
+
const index = Object.keys(components).length;
|
|
267
216
|
components[index] = value;
|
|
268
|
-
values[key] =
|
|
217
|
+
values[key] = `<${index}/>`;
|
|
269
218
|
});
|
|
270
219
|
}
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
formats: formats
|
|
220
|
+
const _translation = i18n && typeof i18n._ === "function" ? i18n._(id, values, {
|
|
221
|
+
message,
|
|
222
|
+
formats
|
|
275
223
|
}) : id; // i18n provider isn't loaded at all
|
|
276
224
|
|
|
277
|
-
|
|
278
|
-
var translation = _translation ? formatElements(_translation, components) : null;
|
|
279
|
-
|
|
225
|
+
const translation = _translation ? formatElements(_translation, components) : null;
|
|
280
226
|
if (render === null || component === null) {
|
|
281
227
|
// Although `string` is a valid react element, types only allow `Element`
|
|
282
228
|
// Upstream issue: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/20544
|
|
283
229
|
return translation;
|
|
284
230
|
}
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
translation: translation,
|
|
231
|
+
const FallbackComponent = defaultComponent || React.Fragment;
|
|
232
|
+
const i18nProps = {
|
|
233
|
+
id,
|
|
234
|
+
message,
|
|
235
|
+
translation,
|
|
291
236
|
isTranslated: id !== translation && message !== translation
|
|
292
|
-
};
|
|
237
|
+
};
|
|
293
238
|
|
|
239
|
+
// Validation of `render` and `component` props
|
|
294
240
|
if (render && component) {
|
|
295
241
|
console.error("You can't use both `component` and `render` prop at the same time. `component` is ignored.");
|
|
296
242
|
} else if (render && typeof render !== "function") {
|
|
297
|
-
console.error(
|
|
243
|
+
console.error(`Invalid value supplied to prop \`render\`. It must be a function, provided ${render}`);
|
|
298
244
|
} else if (component && typeof component !== "function") {
|
|
299
245
|
// Apparently, both function components and class components are functions
|
|
300
246
|
// See https://stackoverflow.com/a/41658173/1535540
|
|
301
|
-
console.error(
|
|
247
|
+
console.error(`Invalid value supplied to prop \`component\`. It must be a React component, provided ${component}`);
|
|
302
248
|
return /*#__PURE__*/React.createElement(FallbackComponent, i18nProps, translation);
|
|
303
|
-
}
|
|
304
|
-
|
|
249
|
+
}
|
|
305
250
|
|
|
251
|
+
// Rendering using a render prop
|
|
306
252
|
if (typeof render === "function") {
|
|
307
253
|
// Component: render={(props) => <a title={props.translation}>x</a>}
|
|
308
254
|
return render(i18nProps);
|
|
309
|
-
}
|
|
310
|
-
|
|
255
|
+
}
|
|
311
256
|
|
|
312
|
-
|
|
313
|
-
|
|
257
|
+
// `component` prop has a higher precedence over `defaultComponent`
|
|
258
|
+
const Component = component || FallbackComponent;
|
|
259
|
+
const DefaultComponent = defaultComponent;
|
|
314
260
|
return DefaultComponent && !component ? /*#__PURE__*/React.createElement(DefaultComponent, i18nProps, translation) : /*#__PURE__*/React.createElement(Component, null, translation);
|
|
315
261
|
}
|
|
316
262
|
Trans.defaultProps = {
|
package/build/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../src/I18nProvider.tsx","../../src/format.ts","../../src/Trans.tsx"],"sourcesContent":["import React, { ComponentType, FunctionComponent } from \"react\"\nimport { I18n } from \"@lingui/core\"\nimport { TransRenderProps } from \"./Trans\"\n\nexport type I18nContext = {\n i18n: I18n\n defaultComponent?: ComponentType<TransRenderProps>\n}\n\nexport type withI18nProps = {\n i18n: I18n\n}\n\nexport type I18nProviderProps = I18nContext & {\n forceRenderOnLocaleChange?: boolean\n children?: React.ReactNode\n}\n\nconst LinguiContext = React.createContext<I18nContext>(null)\n\nexport function useLingui(): I18nContext {\n const context = React.useContext<I18nContext>(LinguiContext)\n\n if (process.env.NODE_ENV !== \"production\") {\n if (context == null) {\n throw new Error(\"useLingui hook was used without I18nProvider.\")\n }\n }\n\n return context\n}\n\nexport function withI18n(\n o?: object\n): <P extends withI18nProps>(\n Component: ComponentType<P>\n) => React.ComponentType<Omit<P, 'i18n'>> {\n return <P extends withI18nProps>(\n WrappedComponent: ComponentType<P>\n ): ComponentType<P> => {\n return (props) => {\n if (process.env.NODE_ENV !== \"production\") {\n if (typeof o === \"function\" || React.isValidElement(o)) {\n throw new Error(\n \"withI18n([options]) takes options as a first argument, \" +\n \"but received React component itself. Without options, the Component \" +\n \"should be wrapped as withI18n()(Component), not withI18n(Component).\"\n )\n }\n }\n\n const { i18n } = useLingui()\n return <WrappedComponent {...props} i18n={i18n} />\n }\n }\n}\n\nexport const I18nProvider: FunctionComponent<I18nProviderProps> = ({\n i18n,\n defaultComponent,\n forceRenderOnLocaleChange = true,\n children,\n}) => {\n /**\n * We can't pass `i18n` object directly through context, because even when locale\n * or messages are changed, i18n object is still the same. Context provider compares\n * reference identity and suggested workaround is create a wrapper object every time\n * we need to trigger re-render. See https://reactjs.org/docs/context.html#caveats.\n *\n * Due to this effect we also pass `defaultComponent` in the same context, instead\n * of creating a separate Provider/Consumer pair.\n *\n * We can't use useMemo hook either, because we want to recalculate value manually.\n */\n const makeContext = () => ({\n i18n,\n defaultComponent,\n })\n const getRenderKey = () => {\n return (forceRenderOnLocaleChange ? (i18n.locale || 'default') : 'default') as string\n }\n\n const [context, setContext] = React.useState<I18nContext>(makeContext()),\n [renderKey, setRenderKey] = React.useState<string>(getRenderKey())\n\n /**\n * Subscribe for locale/message changes\n *\n * I18n object from `@lingui/core` is the single source of truth for all i18n related\n * data (active locale, catalogs). When new messages are loaded or locale is changed\n * we need to trigger re-rendering of LinguiContext.Consumers.\n *\n * We call `setContext(makeContext())` after adding the observer in case the `change`\n * event would already have fired between the inital renderKey calculation and the\n * `useEffect` hook being called. This can happen if locales are loaded/activated\n * async.\n */\n React.useEffect(() => {\n const unsubscribe = i18n.on(\"change\", () => {\n setContext(makeContext())\n setRenderKey(getRenderKey())\n })\n if (renderKey === 'default') {\n setRenderKey(getRenderKey())\n }\n if (forceRenderOnLocaleChange && renderKey === 'default') {\n console.log(\"I18nProvider did not render. A call to i18n.activate still needs to happen or forceRenderOnLocaleChange must be set to false.\")\n }\n return () => unsubscribe()\n }, [])\n\n if (forceRenderOnLocaleChange && renderKey === 'default') return null\n\n return (\n <LinguiContext.Provider value={context} key={renderKey}>\n {children}\n </LinguiContext.Provider>\n )\n}\n","import React from \"react\"\n\n// match <0>paired</0> and <1/> unpaired tags\nconst tagRe = /<(\\d+)>(.*?)<\\/\\1>|<(\\d+)\\/>/\nconst nlRe = /(?:\\r\\n|\\r|\\n)/g\n\n// For HTML, certain tags should omit their close tag. We keep a whitelist for\n// those special-case tags.\nconst voidElementTags = {\n area: true,\n base: true,\n br: true,\n col: true,\n embed: true,\n hr: true,\n img: true,\n input: true,\n keygen: true,\n link: true,\n meta: true,\n param: true,\n source: true,\n track: true,\n wbr: true,\n menuitem: true\n}\n\n/**\n * `formatElements` - parse string and return tree of react elements\n *\n * `value` is string to be formatted with <0>Paired<0/> or <0/> (unpaired)\n * placeholders. `elements` is a array of react elements which indexes\n * correspond to element indexes in formatted string\n */\nfunction formatElements(\n value: string,\n elements: { [key: string]: React.ReactElement<any> } = {}\n): string | Array<any> {\n const uniqueId = makeCounter(0, '$lingui$')\n const parts = value.replace(nlRe, \"\").split(tagRe)\n\n // no inline elements, return\n if (parts.length === 1) return value\n\n const tree = []\n\n const before = parts.shift()\n if (before) tree.push(before)\n\n for (const [index, children, after] of getElements(parts)) {\n let element = elements[index]\n\n if (!element || (voidElementTags[element.type as string] && children)) {\n if (!element) {\n console.error(\n `Can use element at index '${index}' as it is not declared in the original translation`\n )\n } else {\n console.error(\n `${element.type} is a void element tag therefore it must have no children`\n )\n }\n\n // ignore problematic element but push its children and elements after it\n element = React.createElement(React.Fragment)\n }\n\n tree.push(\n React.cloneElement(\n element,\n { key: uniqueId() },\n\n // format children for pair tags\n // unpaired tags might have children if it's a component passed as a variable\n children ? formatElements(children, elements) : element.props.children\n )\n )\n\n if (after) tree.push(after)\n }\n\n return tree\n}\n\n/*\n * `getElements` - return array of element indexes and element childrens\n *\n * `parts` is array of [pairedIndex, children, unpairedIndex, textAfter, ...]\n * where:\n * - `pairedIndex` is index of paired element (undef for unpaired)\n * - `children` are children of paired element (undef for unpaired)\n * - `unpairedIndex` is index of unpaired element (undef for paired)\n * - `textAfter` is string after all elements (empty string, if there's nothing)\n *\n * `parts` length is always multiply of 4\n *\n * Returns: Array<[elementIndex, children, after]>\n */\nfunction getElements(parts) {\n if (!parts.length) return []\n\n const [paired, children, unpaired, after] = parts.slice(0, 4)\n\n return [[parseInt(paired || unpaired), children || \"\", after]].concat(\n getElements(parts.slice(4, parts.length))\n )\n}\n\nconst makeCounter = (count = 0, prefix = \"\") => () => `${prefix}_${count++}`\n\nexport { formatElements }\n","import React from \"react\"\n\nimport { useLingui } from \"./I18nProvider\"\nimport { formatElements } from \"./format\"\n\nexport type TransRenderProps = {\n id?: string\n translation?: React.ReactNode\n children?: React.ReactNode\n message?: string | null\n isTranslated?: boolean\n}\n\nexport type TransProps = {\n id: string\n message?: string\n values: Record<string, unknown>\n context?: string\n components: { [key: string]: React.ElementType | any }\n formats?: Record<string, unknown>\n children?: React.ReactNode\n component?: React.ComponentType<TransRenderProps>\n render?: (props: TransRenderProps) => React.ReactElement<any, any> | null\n}\n\nexport function Trans(props: TransProps): React.ReactElement<any, any> | null {\n const { i18n, defaultComponent } = useLingui()\n const { render, component, id, message, formats } = props\n\n const values = { ...props.values }\n const components = { ...props.components }\n\n if (values) {\n /*\n Related discussion: https://github.com/lingui/js-lingui/issues/183\n\n Values *might* contain React elements with static content.\n They're replaced with <INDEX /> placeholders and added to `components`.\n\n Example:\n Translation: Hello {name}\n Values: { name: <strong>Jane</strong> }\n\n It'll become \"Hello <0 />\" with components=[<strong>Jane</strong>]\n */\n\n Object.keys(values).forEach((key) => {\n const value = values[key]\n if (!React.isValidElement(value)) return\n\n const index = Object.keys(components).length\n\n components[index] = value\n values[key] = `<${index}/>`\n })\n }\n\n const _translation: string =\n i18n && typeof i18n._ === \"function\"\n ? i18n._(id, values, { message, formats })\n : id // i18n provider isn't loaded at all\n\n const translation = _translation\n ? formatElements(_translation, components)\n : null\n\n if (render === null || component === null) {\n // Although `string` is a valid react element, types only allow `Element`\n // Upstream issue: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/20544\n return (translation as unknown) as React.ReactElement<any, any>\n }\n\n const FallbackComponent = (defaultComponent ||\n React.Fragment) as React.ComponentType<any>\n\n const i18nProps = {\n id,\n message,\n translation,\n isTranslated: id !== translation && message !== translation,\n }\n\n // Validation of `render` and `component` props\n if (render && component) {\n console.error(\n \"You can't use both `component` and `render` prop at the same time. `component` is ignored.\"\n )\n } else if (render && typeof render !== \"function\") {\n console.error(\n `Invalid value supplied to prop \\`render\\`. It must be a function, provided ${render}`\n )\n } else if (component && typeof component !== \"function\") {\n // Apparently, both function components and class components are functions\n // See https://stackoverflow.com/a/41658173/1535540\n console.error(\n `Invalid value supplied to prop \\`component\\`. It must be a React component, provided ${component}`\n )\n return <FallbackComponent {...i18nProps}>{translation}</FallbackComponent>\n }\n\n // Rendering using a render prop\n if (typeof render === \"function\") {\n // Component: render={(props) => <a title={props.translation}>x</a>}\n return render(i18nProps)\n }\n\n // `component` prop has a higher precedence over `defaultComponent`\n const Component = (component || FallbackComponent) as React.ComponentType<any>\n const DefaultComponent = defaultComponent\n\n return DefaultComponent && !component ? (\n <DefaultComponent {...i18nProps}>{translation}</DefaultComponent>\n ) : (\n <Component>{translation}</Component>\n )\n}\n\nTrans.defaultProps = {\n values: {},\n components: {},\n}\n"],"names":["LinguiContext","React","createContext","useLingui","context","useContext","process","env","NODE_ENV","Error","withI18n","o","WrappedComponent","props","isValidElement","i18n","I18nProvider","defaultComponent","forceRenderOnLocaleChange","children","makeContext","getRenderKey","locale","useState","setContext","renderKey","setRenderKey","useEffect","unsubscribe","on","console","log","tagRe","nlRe","voidElementTags","area","base","br","col","embed","hr","img","input","keygen","link","meta","param","source","track","wbr","menuitem","formatElements","value","elements","uniqueId","makeCounter","parts","replace","split","length","tree","before","shift","push","getElements","index","after","element","type","error","createElement","Fragment","cloneElement","key","slice","paired","unpaired","parseInt","concat","count","prefix","Trans","render","component","id","message","formats","values","components","Object","keys","forEach","_translation","_","translation","FallbackComponent","i18nProps","isTranslated","Component","DefaultComponent","defaultProps"],"mappings":";;;;;;;AAkBA,IAAMA,aAAa,gBAAGC,KAAK,CAACC,aAAN,CAAiC,IAAjC,CAAtB,CAAA;AAEO,SAASC,SAAT,GAAkC;AACvC,EAAA,IAAMC,OAAO,GAAGH,KAAK,CAACI,UAAN,CAA8BL,aAA9B,CAAhB,CAAA;;AAEA,EAAA,IAAIM,OAAO,CAACC,GAAR,CAAYC,QAAZ,KAAyB,YAA7B,EAA2C;AACzC,IAAIJ,IAAAA,OAAO,IAAI,IAAf,EAAqB;AACnB,MAAA,MAAM,IAAIK,KAAJ,CAAU,+CAAV,CAAN,CAAA;AACD,KAAA;AACF,GAAA;;AAED,EAAA,OAAOL,OAAP,CAAA;AACD,CAAA;AAEM,SAASM,QAAT,CACLC,CADK,EAImC;AACxC,EAAO,OAAA,UACLC,gBADK,EAEgB;AACrB,IAAO,OAAA,UAACC,KAAD,EAAW;AAChB,MAAA,IAAIP,OAAO,CAACC,GAAR,CAAYC,QAAZ,KAAyB,YAA7B,EAA2C;AACzC,QAAI,IAAA,OAAOG,CAAP,KAAa,UAAb,iBAA2BV,KAAK,CAACa,cAAN,CAAqBH,CAArB,CAA/B,EAAw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IAAI,OAAOA,MAAP,KAAkB,UAAhC,EAA4C;AACjDpD,IAAAA,OAAO,CAACuC,KAAR,CAAA,2EAAA,CAAA,MAAA,CACgFa,MADhF,CAAA,CAAA,CAAA;AAGD,GAJM,MAIA,IAAIC,SAAS,IAAI,OAAOA,SAAP,KAAqB,UAAtC,EAAkD;AACvD;AACA;AACArD,IAAAA,OAAO,CAACuC,KAAR,CAAA,qFAAA,CAAA,MAAA,CAC0Fc,SAD1F,CAAA,CAAA,CAAA;AAGA,IAAA,oBAAO,oBAAC,iBAAD,EAAuBa,SAAvB,EAAmCF,WAAnC,CAAP,CAAA;AACD,GAzE2E;;;AA4E5E,EAAA,IAAI,OAAOZ,MAAP,KAAkB,UAAtB,EAAkC;AAChC;AACA,IAAOA,OAAAA,MAAM,CAACc,SAAD,CAAb,CAAA;AACD,GA/E2E;;;AAkF5E,EAAA,IAAME,SAAS,GAAIf,SAAS,IAAIY,iBAAhC,CAAA;AACA,EAAMI,IAAAA,gBAAgB,GAAGlF,gBAAzB,CAAA;AAEA,EAAA,OAAOkF,gBAAgB,IAAI,CAAChB,SAArB,gBACL,oBAAC,gBAAD,EAAsBa,SAAtB,EAAkCF,WAAlC,CADK,gBAGL,oBAAC,SAAD,EAAA,IAAA,EAAYA,WAAZ,CAHF,CAAA;AAKD,CAAA;AAEDb,KAAK,CAACmB,YAAN,GAAqB;AACnBb,EAAAA,MAAM,EAAE,EADW;AAEnBC,EAAAA,UAAU,EAAE,EAAA;AAFO,CAArB;;;;;;;"}
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../src/I18nProvider.tsx","../../src/format.ts","../../src/Trans.tsx"],"sourcesContent":["import React, { ComponentType, FunctionComponent } from \"react\"\nimport { I18n } from \"@lingui/core\"\nimport { TransRenderProps } from \"./Trans\"\n\nexport type I18nContext = {\n i18n: I18n\n defaultComponent?: ComponentType<TransRenderProps>\n}\n\nexport type withI18nProps = {\n i18n: I18n\n}\n\nexport type I18nProviderProps = I18nContext & {\n forceRenderOnLocaleChange?: boolean\n children?: React.ReactNode\n}\n\nconst LinguiContext = React.createContext<I18nContext>(null)\n\nexport function useLingui(): I18nContext {\n const context = React.useContext<I18nContext>(LinguiContext)\n\n if (process.env.NODE_ENV !== \"production\") {\n if (context == null) {\n throw new Error(\"useLingui hook was used without I18nProvider.\")\n }\n }\n\n return context\n}\n\nexport function withI18n(\n o?: object\n): <P extends withI18nProps>(\n Component: ComponentType<P>\n) => React.ComponentType<Omit<P, \"i18n\">> {\n return <P extends withI18nProps>(\n WrappedComponent: ComponentType<P>\n ): ComponentType<P> => {\n return (props) => {\n if (process.env.NODE_ENV !== \"production\") {\n if (typeof o === \"function\" || React.isValidElement(o)) {\n throw new Error(\n \"withI18n([options]) takes options as a first argument, \" +\n \"but received React component itself. Without options, the Component \" +\n \"should be wrapped as withI18n()(Component), not withI18n(Component).\"\n )\n }\n }\n\n const { i18n } = useLingui()\n return <WrappedComponent {...props} i18n={i18n} />\n }\n }\n}\n\nexport const I18nProvider: FunctionComponent<I18nProviderProps> = ({\n i18n,\n defaultComponent,\n forceRenderOnLocaleChange = true,\n children,\n}) => {\n /**\n * We can't pass `i18n` object directly through context, because even when locale\n * or messages are changed, i18n object is still the same. Context provider compares\n * reference identity and suggested workaround is create a wrapper object every time\n * we need to trigger re-render. See https://reactjs.org/docs/context.html#caveats.\n *\n * Due to this effect we also pass `defaultComponent` in the same context, instead\n * of creating a separate Provider/Consumer pair.\n *\n * We can't use useMemo hook either, because we want to recalculate value manually.\n */\n const makeContext = () => ({\n i18n,\n defaultComponent,\n })\n const getRenderKey = () => {\n return (\n forceRenderOnLocaleChange ? i18n.locale || \"default\" : \"default\"\n ) as string\n }\n\n const [context, setContext] = React.useState<I18nContext>(makeContext()),\n [renderKey, setRenderKey] = React.useState<string>(getRenderKey())\n\n /**\n * Subscribe for locale/message changes\n *\n * I18n object from `@lingui/core` is the single source of truth for all i18n related\n * data (active locale, catalogs). When new messages are loaded or locale is changed\n * we need to trigger re-rendering of LinguiContext.Consumers.\n *\n * We call `setContext(makeContext())` after adding the observer in case the `change`\n * event would already have fired between the inital renderKey calculation and the\n * `useEffect` hook being called. This can happen if locales are loaded/activated\n * async.\n */\n React.useEffect(() => {\n const unsubscribe = i18n.on(\"change\", () => {\n setContext(makeContext())\n setRenderKey(getRenderKey())\n })\n if (renderKey === \"default\") {\n setRenderKey(getRenderKey())\n }\n if (forceRenderOnLocaleChange && renderKey === \"default\") {\n console.log(\n \"I18nProvider did not render. A call to i18n.activate still needs to happen or forceRenderOnLocaleChange must be set to false.\"\n )\n }\n return () => unsubscribe()\n }, [])\n\n if (forceRenderOnLocaleChange && renderKey === \"default\") return null\n\n return (\n <LinguiContext.Provider value={context} key={renderKey}>\n {children}\n </LinguiContext.Provider>\n )\n}\n","import React from \"react\"\n\n// match <tag>paired</tag> and <tag/> unpaired tags\nconst tagRe = /<([a-zA-Z0-9]+)>(.*?)<\\/\\1>|<([a-zA-Z0-9]+)\\/>/\nconst nlRe = /(?:\\r\\n|\\r|\\n)/g\n\n// For HTML, certain tags should omit their close tag. We keep a whitelist for\n// those special-case tags.\nconst voidElementTags = {\n area: true,\n base: true,\n br: true,\n col: true,\n embed: true,\n hr: true,\n img: true,\n input: true,\n keygen: true,\n link: true,\n meta: true,\n param: true,\n source: true,\n track: true,\n wbr: true,\n menuitem: true,\n}\n\n/**\n * `formatElements` - parse string and return tree of react elements\n *\n * `value` is string to be formatted with <tag>Paired<tag/> or <tag/> (unpaired)\n * placeholders. `elements` is a array of react elements which indexes\n * correspond to element indexes in formatted string\n */\nfunction formatElements(\n value: string,\n elements: { [key: string]: React.ReactElement<any> } = {}\n): string | Array<any> {\n const uniqueId = makeCounter(0, \"$lingui$\")\n const parts = value.replace(nlRe, \"\").split(tagRe)\n\n // no inline elements, return\n if (parts.length === 1) return value\n\n const tree = []\n\n const before = parts.shift()\n if (before) tree.push(before)\n\n for (const [index, children, after] of getElements(parts)) {\n let element = elements[index]\n\n if (!element || (voidElementTags[element.type as string] && children)) {\n if (!element) {\n console.error(\n `Can use element at index '${index}' as it is not declared in the original translation`\n )\n } else {\n console.error(\n `${element.type} is a void element tag therefore it must have no children`\n )\n }\n\n // ignore problematic element but push its children and elements after it\n element = React.createElement(React.Fragment)\n }\n\n tree.push(\n React.cloneElement(\n element,\n { key: uniqueId() },\n\n // format children for pair tags\n // unpaired tags might have children if it's a component passed as a variable\n children ? formatElements(children, elements) : element.props.children\n )\n )\n\n if (after) tree.push(after)\n }\n\n return tree\n}\n\n/*\n * `getElements` - return array of element indexes and element childrens\n *\n * `parts` is array of [pairedIndex, children, unpairedIndex, textAfter, ...]\n * where:\n * - `pairedIndex` is index of paired element (undef for unpaired)\n * - `children` are children of paired element (undef for unpaired)\n * - `unpairedIndex` is index of unpaired element (undef for paired)\n * - `textAfter` is string after all elements (empty string, if there's nothing)\n *\n * `parts` length is always multiply of 4\n *\n * Returns: Array<[elementIndex, children, after]>\n */\nfunction getElements(parts) {\n if (!parts.length) return []\n\n const [paired, children, unpaired, after] = parts.slice(0, 4)\n\n return [[paired || unpaired, children || \"\", after]].concat(\n getElements(parts.slice(4, parts.length))\n )\n}\n\nconst makeCounter =\n (count = 0, prefix = \"\") =>\n () =>\n `${prefix}_${count++}`\n\nexport { formatElements }\n","import React from \"react\"\n\nimport { useLingui } from \"./I18nProvider\"\nimport { formatElements } from \"./format\"\n\nexport type TransRenderProps = {\n id?: string\n translation?: React.ReactNode\n children?: React.ReactNode\n message?: string | null\n isTranslated?: boolean\n}\n\nexport type TransProps = {\n id: string\n message?: string\n values: Record<string, unknown>\n context?: string\n components: { [key: string]: React.ElementType | any }\n formats?: Record<string, unknown>\n children?: React.ReactNode\n component?: React.ComponentType<TransRenderProps>\n render?: (props: TransRenderProps) => React.ReactElement<any, any> | null\n}\n\nexport function Trans(props: TransProps): React.ReactElement<any, any> | null {\n const { i18n, defaultComponent } = useLingui()\n const { render, component, id, message, formats } = props\n\n const values = { ...props.values }\n const components = { ...props.components }\n\n if (values) {\n /*\n Related discussion: https://github.com/lingui/js-lingui/issues/183\n\n Values *might* contain React elements with static content.\n They're replaced with <INDEX /> placeholders and added to `components`.\n\n Example:\n Translation: Hello {name}\n Values: { name: <strong>Jane</strong> }\n\n It'll become \"Hello <0 />\" with components=[<strong>Jane</strong>]\n */\n\n Object.keys(values).forEach((key) => {\n const value = values[key]\n if (!React.isValidElement(value)) return\n\n const index = Object.keys(components).length\n\n components[index] = value\n values[key] = `<${index}/>`\n })\n }\n\n const _translation: string =\n i18n && typeof i18n._ === \"function\"\n ? i18n._(id, values, { message, formats })\n : id // i18n provider isn't loaded at all\n\n const translation = _translation\n ? formatElements(_translation, components)\n : null\n\n if (render === null || component === null) {\n // Although `string` is a valid react element, types only allow `Element`\n // Upstream issue: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/20544\n return translation as unknown as React.ReactElement<any, any>\n }\n\n const FallbackComponent = (defaultComponent ||\n React.Fragment) as React.ComponentType<any>\n\n const i18nProps = {\n id,\n message,\n translation,\n isTranslated: id !== translation && message !== translation,\n }\n\n // Validation of `render` and `component` props\n if (render && component) {\n console.error(\n \"You can't use both `component` and `render` prop at the same time. `component` is ignored.\"\n )\n } else if (render && typeof render !== \"function\") {\n console.error(\n `Invalid value supplied to prop \\`render\\`. It must be a function, provided ${render}`\n )\n } else if (component && typeof component !== \"function\") {\n // Apparently, both function components and class components are functions\n // See https://stackoverflow.com/a/41658173/1535540\n console.error(\n `Invalid value supplied to prop \\`component\\`. It must be a React component, provided ${component}`\n )\n return <FallbackComponent {...i18nProps}>{translation}</FallbackComponent>\n }\n\n // Rendering using a render prop\n if (typeof render === \"function\") {\n // Component: render={(props) => <a title={props.translation}>x</a>}\n return render(i18nProps)\n }\n\n // `component` prop has a higher precedence over `defaultComponent`\n const Component = (component || FallbackComponent) as React.ComponentType<any>\n const DefaultComponent = defaultComponent\n\n return DefaultComponent && !component ? (\n <DefaultComponent {...i18nProps}>{translation}</DefaultComponent>\n ) : (\n <Component>{translation}</Component>\n )\n}\n\nTrans.defaultProps = {\n values: {},\n components: {},\n}\n"],"names":["LinguiContext","React","createContext","useLingui","context","useContext","process","env","NODE_ENV","Error","withI18n","o","WrappedComponent","props","isValidElement","i18n","I18nProvider","defaultComponent","forceRenderOnLocaleChange","children","makeContext","getRenderKey","locale","setContext","useState","renderKey","setRenderKey","useEffect","unsubscribe","on","console","log","tagRe","nlRe","voidElementTags","area","base","br","col","embed","hr","img","input","keygen","link","meta","param","source","track","wbr","menuitem","formatElements","value","elements","uniqueId","makeCounter","parts","replace","split","length","tree","before","shift","push","index","after","getElements","element","type","error","createElement","Fragment","cloneElement","key","paired","unpaired","slice","concat","count","prefix","Trans","render","component","id","message","formats","values","components","Object","keys","forEach","_translation","_","translation","FallbackComponent","i18nProps","isTranslated","Component","DefaultComponent","defaultProps"],"mappings":";;;;;AAkBA,MAAMA,aAAa,gBAAGC,KAAK,CAACC,aAAa,CAAc,IAAI,CAAC,CAAA;AAErD,SAASC,SAAS,GAAgB;AACvC,EAAA,MAAMC,OAAO,GAAGH,KAAK,CAACI,UAAU,CAAcL,aAAa,CAAC,CAAA;AAE5D,EAAA,IAAIM,OAAO,CAACC,GAAG,CAACC,QAAQ,KAAK,YAAY,EAAE;IACzC,IAAIJ,OAAO,IAAI,IAAI,EAAE;AACnB,MAAA,MAAM,IAAIK,KAAK,CAAC,+CAA+C,CAAC,CAAA;AAClE,KAAA;AACF,GAAA;AAEA,EAAA,OAAOL,OAAO,CAAA;AAChB,CAAA;AAEO,SAASM,QAAQ,CACtBC,CAAU,EAG8B;AACxC,EAAA,OACEC,gBAAkC,IACb;AACrB,IAAA,OAAQC,KAAK,IAAK;AAChB,MAAA,IAAIP,OAAO,CAACC,GAAG,CAACC,QAAQ,KAAK,YAAY,EAAE;QACzC,IAAI,OAAOG,CAAC,KAAK,UAAU,iBAAIV,KAAK,CAACa,cAAc,CAACH,CAAC,CAAC,EAAE;UACtD,MAAM,IAAIF,KAAK,CACb,yDAAyD,GACvD,sEAAsE,GACtE,sEAAs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AG;EACnBb,MAAM,EAAE,EAAE;AACVC,EAAAA,UAAU,EAAE,EAAC;AACf,CAAC;;;;;;;"}
|
package/build/esm/index.js
CHANGED
|
@@ -1,45 +1,40 @@
|
|
|
1
|
-
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
|
|
2
1
|
import _extends from '@babel/runtime/helpers/extends';
|
|
3
2
|
import React from 'react';
|
|
4
|
-
import _defineProperty from '@babel/runtime/helpers/defineProperty';
|
|
5
3
|
|
|
6
|
-
|
|
4
|
+
const LinguiContext = /*#__PURE__*/React.createContext(null);
|
|
7
5
|
function useLingui() {
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
const context = React.useContext(LinguiContext);
|
|
10
7
|
if (process.env.NODE_ENV !== "production") {
|
|
11
8
|
if (context == null) {
|
|
12
9
|
throw new Error("useLingui hook was used without I18nProvider.");
|
|
13
10
|
}
|
|
14
11
|
}
|
|
15
|
-
|
|
16
12
|
return context;
|
|
17
13
|
}
|
|
18
14
|
function withI18n(o) {
|
|
19
|
-
return
|
|
20
|
-
return
|
|
15
|
+
return WrappedComponent => {
|
|
16
|
+
return props => {
|
|
21
17
|
if (process.env.NODE_ENV !== "production") {
|
|
22
18
|
if (typeof o === "function" || /*#__PURE__*/React.isValidElement(o)) {
|
|
23
19
|
throw new Error("withI18n([options]) takes options as a first argument, " + "but received React component itself. Without options, the Component " + "should be wrapped as withI18n()(Component), not withI18n(Component).");
|
|
24
20
|
}
|
|
25
21
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
22
|
+
const {
|
|
23
|
+
i18n
|
|
24
|
+
} = useLingui();
|
|
30
25
|
return /*#__PURE__*/React.createElement(WrappedComponent, _extends({}, props, {
|
|
31
26
|
i18n: i18n
|
|
32
27
|
}));
|
|
33
28
|
};
|
|
34
29
|
};
|
|
35
30
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
31
|
+
const I18nProvider = _ref => {
|
|
32
|
+
let {
|
|
33
|
+
i18n,
|
|
34
|
+
defaultComponent,
|
|
35
|
+
forceRenderOnLocaleChange = true,
|
|
36
|
+
children
|
|
37
|
+
} = _ref;
|
|
43
38
|
/**
|
|
44
39
|
* We can't pass `i18n` object directly through context, because even when locale
|
|
45
40
|
* or messages are changed, i18n object is still the same. Context provider compares
|
|
@@ -51,25 +46,16 @@ var I18nProvider = function I18nProvider(_ref) {
|
|
|
51
46
|
*
|
|
52
47
|
* We can't use useMemo hook either, because we want to recalculate value manually.
|
|
53
48
|
*/
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
49
|
+
const makeContext = () => ({
|
|
50
|
+
i18n,
|
|
51
|
+
defaultComponent
|
|
52
|
+
});
|
|
53
|
+
const getRenderKey = () => {
|
|
54
|
+
return forceRenderOnLocaleChange ? i18n.locale || "default" : "default";
|
|
59
55
|
};
|
|
56
|
+
const [context, setContext] = React.useState(makeContext()),
|
|
57
|
+
[renderKey, setRenderKey] = React.useState(getRenderKey());
|
|
60
58
|
|
|
61
|
-
var getRenderKey = function getRenderKey() {
|
|
62
|
-
return forceRenderOnLocaleChange ? i18n.locale || 'default' : 'default';
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
var _React$useState = React.useState(makeContext()),
|
|
66
|
-
_React$useState2 = _slicedToArray(_React$useState, 2),
|
|
67
|
-
context = _React$useState2[0],
|
|
68
|
-
setContext = _React$useState2[1],
|
|
69
|
-
_React$useState3 = React.useState(getRenderKey()),
|
|
70
|
-
_React$useState4 = _slicedToArray(_React$useState3, 2),
|
|
71
|
-
renderKey = _React$useState4[0],
|
|
72
|
-
setRenderKey = _React$useState4[1];
|
|
73
59
|
/**
|
|
74
60
|
* Subscribe for locale/message changes
|
|
75
61
|
*
|
|
@@ -82,44 +68,33 @@ var I18nProvider = function I18nProvider(_ref) {
|
|
|
82
68
|
* `useEffect` hook being called. This can happen if locales are loaded/activated
|
|
83
69
|
* async.
|
|
84
70
|
*/
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
React.useEffect(function () {
|
|
88
|
-
var unsubscribe = i18n.on("change", function () {
|
|
71
|
+
React.useEffect(() => {
|
|
72
|
+
const unsubscribe = i18n.on("change", () => {
|
|
89
73
|
setContext(makeContext());
|
|
90
74
|
setRenderKey(getRenderKey());
|
|
91
75
|
});
|
|
92
|
-
|
|
93
|
-
if (renderKey === 'default') {
|
|
76
|
+
if (renderKey === "default") {
|
|
94
77
|
setRenderKey(getRenderKey());
|
|
95
78
|
}
|
|
96
|
-
|
|
97
|
-
if (forceRenderOnLocaleChange && renderKey === 'default') {
|
|
79
|
+
if (forceRenderOnLocaleChange && renderKey === "default") {
|
|
98
80
|
console.log("I18nProvider did not render. A call to i18n.activate still needs to happen or forceRenderOnLocaleChange must be set to false.");
|
|
99
81
|
}
|
|
100
|
-
|
|
101
|
-
return function () {
|
|
102
|
-
return unsubscribe();
|
|
103
|
-
};
|
|
82
|
+
return () => unsubscribe();
|
|
104
83
|
}, []);
|
|
105
|
-
if (forceRenderOnLocaleChange && renderKey ===
|
|
84
|
+
if (forceRenderOnLocaleChange && renderKey === "default") return null;
|
|
106
85
|
return /*#__PURE__*/React.createElement(LinguiContext.Provider, {
|
|
107
86
|
value: context,
|
|
108
87
|
key: renderKey
|
|
109
88
|
}, children);
|
|
110
89
|
};
|
|
111
90
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
91
|
+
// match <tag>paired</tag> and <tag/> unpaired tags
|
|
92
|
+
const tagRe = /<([a-zA-Z0-9]+)>(.*?)<\/\1>|<([a-zA-Z0-9]+)\/>/;
|
|
93
|
+
const nlRe = /(?:\r\n|\r|\n)/g;
|
|
117
94
|
|
|
118
|
-
|
|
119
|
-
var nlRe = /(?:\r\n|\r|\n)/g; // For HTML, certain tags should omit their close tag. We keep a whitelist for
|
|
95
|
+
// For HTML, certain tags should omit their close tag. We keep a whitelist for
|
|
120
96
|
// those special-case tags.
|
|
121
|
-
|
|
122
|
-
var voidElementTags = {
|
|
97
|
+
const voidElementTags = {
|
|
123
98
|
area: true,
|
|
124
99
|
base: true,
|
|
125
100
|
br: true,
|
|
@@ -137,62 +112,47 @@ var voidElementTags = {
|
|
|
137
112
|
wbr: true,
|
|
138
113
|
menuitem: true
|
|
139
114
|
};
|
|
115
|
+
|
|
140
116
|
/**
|
|
141
117
|
* `formatElements` - parse string and return tree of react elements
|
|
142
118
|
*
|
|
143
|
-
* `value` is string to be formatted with <
|
|
119
|
+
* `value` is string to be formatted with <tag>Paired<tag/> or <tag/> (unpaired)
|
|
144
120
|
* placeholders. `elements` is a array of react elements which indexes
|
|
145
121
|
* correspond to element indexes in formatted string
|
|
146
122
|
*/
|
|
147
|
-
|
|
148
123
|
function formatElements(value) {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
124
|
+
let elements = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
125
|
+
const uniqueId = makeCounter(0, "$lingui$");
|
|
126
|
+
const parts = value.replace(nlRe, "").split(tagRe);
|
|
152
127
|
|
|
128
|
+
// no inline elements, return
|
|
153
129
|
if (parts.length === 1) return value;
|
|
154
|
-
|
|
155
|
-
|
|
130
|
+
const tree = [];
|
|
131
|
+
const before = parts.shift();
|
|
156
132
|
if (before) tree.push(before);
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
index = _step$value[0],
|
|
165
|
-
children = _step$value[1],
|
|
166
|
-
after = _step$value[2];
|
|
167
|
-
|
|
168
|
-
var element = elements[index];
|
|
169
|
-
|
|
170
|
-
if (!element || voidElementTags[element.type] && children) {
|
|
171
|
-
if (!element) {
|
|
172
|
-
console.error("Can use element at index '".concat(index, "' as it is not declared in the original translation"));
|
|
173
|
-
} else {
|
|
174
|
-
console.error("".concat(element.type, " is a void element tag therefore it must have no children"));
|
|
175
|
-
} // ignore problematic element but push its children and elements after it
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
element = /*#__PURE__*/React.createElement(React.Fragment);
|
|
133
|
+
for (const [index, children, after] of getElements(parts)) {
|
|
134
|
+
let element = elements[index];
|
|
135
|
+
if (!element || voidElementTags[element.type] && children) {
|
|
136
|
+
if (!element) {
|
|
137
|
+
console.error(`Can use element at index '${index}' as it is not declared in the original translation`);
|
|
138
|
+
} else {
|
|
139
|
+
console.error(`${element.type} is a void element tag therefore it must have no children`);
|
|
179
140
|
}
|
|
180
141
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
}, // format children for pair tags
|
|
184
|
-
// unpaired tags might have children if it's a component passed as a variable
|
|
185
|
-
children ? formatElements(children, elements) : element.props.children));
|
|
186
|
-
if (after) tree.push(after);
|
|
142
|
+
// ignore problematic element but push its children and elements after it
|
|
143
|
+
element = /*#__PURE__*/React.createElement(React.Fragment);
|
|
187
144
|
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
145
|
+
tree.push( /*#__PURE__*/React.cloneElement(element, {
|
|
146
|
+
key: uniqueId()
|
|
147
|
+
},
|
|
148
|
+
// format children for pair tags
|
|
149
|
+
// unpaired tags might have children if it's a component passed as a variable
|
|
150
|
+
children ? formatElements(children, elements) : element.props.children));
|
|
151
|
+
if (after) tree.push(after);
|
|
192
152
|
}
|
|
193
|
-
|
|
194
153
|
return tree;
|
|
195
154
|
}
|
|
155
|
+
|
|
196
156
|
/*
|
|
197
157
|
* `getElements` - return array of element indexes and element childrens
|
|
198
158
|
*
|
|
@@ -207,47 +167,35 @@ function formatElements(value) {
|
|
|
207
167
|
*
|
|
208
168
|
* Returns: Array<[elementIndex, children, after]>
|
|
209
169
|
*/
|
|
210
|
-
|
|
211
|
-
|
|
212
170
|
function getElements(parts) {
|
|
213
171
|
if (!parts.length) return [];
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
_parts$slice2 = _slicedToArray(_parts$slice, 4),
|
|
217
|
-
paired = _parts$slice2[0],
|
|
218
|
-
children = _parts$slice2[1],
|
|
219
|
-
unpaired = _parts$slice2[2],
|
|
220
|
-
after = _parts$slice2[3];
|
|
221
|
-
|
|
222
|
-
return [[parseInt(paired || unpaired), children || "", after]].concat(getElements(parts.slice(4, parts.length)));
|
|
172
|
+
const [paired, children, unpaired, after] = parts.slice(0, 4);
|
|
173
|
+
return [[paired || unpaired, children || "", after]].concat(getElements(parts.slice(4, parts.length)));
|
|
223
174
|
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
return function () {
|
|
229
|
-
return "".concat(prefix, "_").concat(count++);
|
|
230
|
-
};
|
|
175
|
+
const makeCounter = function () {
|
|
176
|
+
let count = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
|
|
177
|
+
let prefix = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "";
|
|
178
|
+
return () => `${prefix}_${count++}`;
|
|
231
179
|
};
|
|
232
180
|
|
|
233
|
-
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; }
|
|
234
|
-
|
|
235
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
|
236
181
|
function Trans(props) {
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
182
|
+
const {
|
|
183
|
+
i18n,
|
|
184
|
+
defaultComponent
|
|
185
|
+
} = useLingui();
|
|
186
|
+
const {
|
|
187
|
+
render,
|
|
188
|
+
component,
|
|
189
|
+
id,
|
|
190
|
+
message,
|
|
191
|
+
formats
|
|
192
|
+
} = props;
|
|
193
|
+
const values = {
|
|
194
|
+
...props.values
|
|
195
|
+
};
|
|
196
|
+
const components = {
|
|
197
|
+
...props.components
|
|
198
|
+
};
|
|
251
199
|
if (values) {
|
|
252
200
|
/*
|
|
253
201
|
Related discussion: https://github.com/lingui/js-lingui/issues/183
|
|
@@ -258,57 +206,55 @@ function Trans(props) {
|
|
|
258
206
|
Values: { name: <strong>Jane</strong> }
|
|
259
207
|
It'll become "Hello <0 />" with components=[<strong>Jane</strong>]
|
|
260
208
|
*/
|
|
261
|
-
|
|
262
|
-
|
|
209
|
+
|
|
210
|
+
Object.keys(values).forEach(key => {
|
|
211
|
+
const value = values[key];
|
|
263
212
|
if (! /*#__PURE__*/React.isValidElement(value)) return;
|
|
264
|
-
|
|
213
|
+
const index = Object.keys(components).length;
|
|
265
214
|
components[index] = value;
|
|
266
|
-
values[key] =
|
|
215
|
+
values[key] = `<${index}/>`;
|
|
267
216
|
});
|
|
268
217
|
}
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
formats: formats
|
|
218
|
+
const _translation = i18n && typeof i18n._ === "function" ? i18n._(id, values, {
|
|
219
|
+
message,
|
|
220
|
+
formats
|
|
273
221
|
}) : id; // i18n provider isn't loaded at all
|
|
274
222
|
|
|
275
|
-
|
|
276
|
-
var translation = _translation ? formatElements(_translation, components) : null;
|
|
277
|
-
|
|
223
|
+
const translation = _translation ? formatElements(_translation, components) : null;
|
|
278
224
|
if (render === null || component === null) {
|
|
279
225
|
// Although `string` is a valid react element, types only allow `Element`
|
|
280
226
|
// Upstream issue: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/20544
|
|
281
227
|
return translation;
|
|
282
228
|
}
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
translation: translation,
|
|
229
|
+
const FallbackComponent = defaultComponent || React.Fragment;
|
|
230
|
+
const i18nProps = {
|
|
231
|
+
id,
|
|
232
|
+
message,
|
|
233
|
+
translation,
|
|
289
234
|
isTranslated: id !== translation && message !== translation
|
|
290
|
-
};
|
|
235
|
+
};
|
|
291
236
|
|
|
237
|
+
// Validation of `render` and `component` props
|
|
292
238
|
if (render && component) {
|
|
293
239
|
console.error("You can't use both `component` and `render` prop at the same time. `component` is ignored.");
|
|
294
240
|
} else if (render && typeof render !== "function") {
|
|
295
|
-
console.error(
|
|
241
|
+
console.error(`Invalid value supplied to prop \`render\`. It must be a function, provided ${render}`);
|
|
296
242
|
} else if (component && typeof component !== "function") {
|
|
297
243
|
// Apparently, both function components and class components are functions
|
|
298
244
|
// See https://stackoverflow.com/a/41658173/1535540
|
|
299
|
-
console.error(
|
|
245
|
+
console.error(`Invalid value supplied to prop \`component\`. It must be a React component, provided ${component}`);
|
|
300
246
|
return /*#__PURE__*/React.createElement(FallbackComponent, i18nProps, translation);
|
|
301
|
-
}
|
|
302
|
-
|
|
247
|
+
}
|
|
303
248
|
|
|
249
|
+
// Rendering using a render prop
|
|
304
250
|
if (typeof render === "function") {
|
|
305
251
|
// Component: render={(props) => <a title={props.translation}>x</a>}
|
|
306
252
|
return render(i18nProps);
|
|
307
|
-
}
|
|
308
|
-
|
|
253
|
+
}
|
|
309
254
|
|
|
310
|
-
|
|
311
|
-
|
|
255
|
+
// `component` prop has a higher precedence over `defaultComponent`
|
|
256
|
+
const Component = component || FallbackComponent;
|
|
257
|
+
const DefaultComponent = defaultComponent;
|
|
312
258
|
return DefaultComponent && !component ? /*#__PURE__*/React.createElement(DefaultComponent, i18nProps, translation) : /*#__PURE__*/React.createElement(Component, null, translation);
|
|
313
259
|
}
|
|
314
260
|
Trans.defaultProps = {
|
package/build/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../src/I18nProvider.tsx","../../src/format.ts","../../src/Trans.tsx"],"sourcesContent":["import React, { ComponentType, FunctionComponent } from \"react\"\nimport { I18n } from \"@lingui/core\"\nimport { TransRenderProps } from \"./Trans\"\n\nexport type I18nContext = {\n i18n: I18n\n defaultComponent?: ComponentType<TransRenderProps>\n}\n\nexport type withI18nProps = {\n i18n: I18n\n}\n\nexport type I18nProviderProps = I18nContext & {\n forceRenderOnLocaleChange?: boolean\n children?: React.ReactNode\n}\n\nconst LinguiContext = React.createContext<I18nContext>(null)\n\nexport function useLingui(): I18nContext {\n const context = React.useContext<I18nContext>(LinguiContext)\n\n if (process.env.NODE_ENV !== \"production\") {\n if (context == null) {\n throw new Error(\"useLingui hook was used without I18nProvider.\")\n }\n }\n\n return context\n}\n\nexport function withI18n(\n o?: object\n): <P extends withI18nProps>(\n Component: ComponentType<P>\n) => React.ComponentType<Omit<P, 'i18n'>> {\n return <P extends withI18nProps>(\n WrappedComponent: ComponentType<P>\n ): ComponentType<P> => {\n return (props) => {\n if (process.env.NODE_ENV !== \"production\") {\n if (typeof o === \"function\" || React.isValidElement(o)) {\n throw new Error(\n \"withI18n([options]) takes options as a first argument, \" +\n \"but received React component itself. Without options, the Component \" +\n \"should be wrapped as withI18n()(Component), not withI18n(Component).\"\n )\n }\n }\n\n const { i18n } = useLingui()\n return <WrappedComponent {...props} i18n={i18n} />\n }\n }\n}\n\nexport const I18nProvider: FunctionComponent<I18nProviderProps> = ({\n i18n,\n defaultComponent,\n forceRenderOnLocaleChange = true,\n children,\n}) => {\n /**\n * We can't pass `i18n` object directly through context, because even when locale\n * or messages are changed, i18n object is still the same. Context provider compares\n * reference identity and suggested workaround is create a wrapper object every time\n * we need to trigger re-render. See https://reactjs.org/docs/context.html#caveats.\n *\n * Due to this effect we also pass `defaultComponent` in the same context, instead\n * of creating a separate Provider/Consumer pair.\n *\n * We can't use useMemo hook either, because we want to recalculate value manually.\n */\n const makeContext = () => ({\n i18n,\n defaultComponent,\n })\n const getRenderKey = () => {\n return (forceRenderOnLocaleChange ? (i18n.locale || 'default') : 'default') as string\n }\n\n const [context, setContext] = React.useState<I18nContext>(makeContext()),\n [renderKey, setRenderKey] = React.useState<string>(getRenderKey())\n\n /**\n * Subscribe for locale/message changes\n *\n * I18n object from `@lingui/core` is the single source of truth for all i18n related\n * data (active locale, catalogs). When new messages are loaded or locale is changed\n * we need to trigger re-rendering of LinguiContext.Consumers.\n *\n * We call `setContext(makeContext())` after adding the observer in case the `change`\n * event would already have fired between the inital renderKey calculation and the\n * `useEffect` hook being called. This can happen if locales are loaded/activated\n * async.\n */\n React.useEffect(() => {\n const unsubscribe = i18n.on(\"change\", () => {\n setContext(makeContext())\n setRenderKey(getRenderKey())\n })\n if (renderKey === 'default') {\n setRenderKey(getRenderKey())\n }\n if (forceRenderOnLocaleChange && renderKey === 'default') {\n console.log(\"I18nProvider did not render. A call to i18n.activate still needs to happen or forceRenderOnLocaleChange must be set to false.\")\n }\n return () => unsubscribe()\n }, [])\n\n if (forceRenderOnLocaleChange && renderKey === 'default') return null\n\n return (\n <LinguiContext.Provider value={context} key={renderKey}>\n {children}\n </LinguiContext.Provider>\n )\n}\n","import React from \"react\"\n\n// match <0>paired</0> and <1/> unpaired tags\nconst tagRe = /<(\\d+)>(.*?)<\\/\\1>|<(\\d+)\\/>/\nconst nlRe = /(?:\\r\\n|\\r|\\n)/g\n\n// For HTML, certain tags should omit their close tag. We keep a whitelist for\n// those special-case tags.\nconst voidElementTags = {\n area: true,\n base: true,\n br: true,\n col: true,\n embed: true,\n hr: true,\n img: true,\n input: true,\n keygen: true,\n link: true,\n meta: true,\n param: true,\n source: true,\n track: true,\n wbr: true,\n menuitem: true\n}\n\n/**\n * `formatElements` - parse string and return tree of react elements\n *\n * `value` is string to be formatted with <0>Paired<0/> or <0/> (unpaired)\n * placeholders. `elements` is a array of react elements which indexes\n * correspond to element indexes in formatted string\n */\nfunction formatElements(\n value: string,\n elements: { [key: string]: React.ReactElement<any> } = {}\n): string | Array<any> {\n const uniqueId = makeCounter(0, '$lingui$')\n const parts = value.replace(nlRe, \"\").split(tagRe)\n\n // no inline elements, return\n if (parts.length === 1) return value\n\n const tree = []\n\n const before = parts.shift()\n if (before) tree.push(before)\n\n for (const [index, children, after] of getElements(parts)) {\n let element = elements[index]\n\n if (!element || (voidElementTags[element.type as string] && children)) {\n if (!element) {\n console.error(\n `Can use element at index '${index}' as it is not declared in the original translation`\n )\n } else {\n console.error(\n `${element.type} is a void element tag therefore it must have no children`\n )\n }\n\n // ignore problematic element but push its children and elements after it\n element = React.createElement(React.Fragment)\n }\n\n tree.push(\n React.cloneElement(\n element,\n { key: uniqueId() },\n\n // format children for pair tags\n // unpaired tags might have children if it's a component passed as a variable\n children ? formatElements(children, elements) : element.props.children\n )\n )\n\n if (after) tree.push(after)\n }\n\n return tree\n}\n\n/*\n * `getElements` - return array of element indexes and element childrens\n *\n * `parts` is array of [pairedIndex, children, unpairedIndex, textAfter, ...]\n * where:\n * - `pairedIndex` is index of paired element (undef for unpaired)\n * - `children` are children of paired element (undef for unpaired)\n * - `unpairedIndex` is index of unpaired element (undef for paired)\n * - `textAfter` is string after all elements (empty string, if there's nothing)\n *\n * `parts` length is always multiply of 4\n *\n * Returns: Array<[elementIndex, children, after]>\n */\nfunction getElements(parts) {\n if (!parts.length) return []\n\n const [paired, children, unpaired, after] = parts.slice(0, 4)\n\n return [[parseInt(paired || unpaired), children || \"\", after]].concat(\n getElements(parts.slice(4, parts.length))\n )\n}\n\nconst makeCounter = (count = 0, prefix = \"\") => () => `${prefix}_${count++}`\n\nexport { formatElements }\n","import React from \"react\"\n\nimport { useLingui } from \"./I18nProvider\"\nimport { formatElements } from \"./format\"\n\nexport type TransRenderProps = {\n id?: string\n translation?: React.ReactNode\n children?: React.ReactNode\n message?: string | null\n isTranslated?: boolean\n}\n\nexport type TransProps = {\n id: string\n message?: string\n values: Record<string, unknown>\n context?: string\n components: { [key: string]: React.ElementType | any }\n formats?: Record<string, unknown>\n children?: React.ReactNode\n component?: React.ComponentType<TransRenderProps>\n render?: (props: TransRenderProps) => React.ReactElement<any, any> | null\n}\n\nexport function Trans(props: TransProps): React.ReactElement<any, any> | null {\n const { i18n, defaultComponent } = useLingui()\n const { render, component, id, message, formats } = props\n\n const values = { ...props.values }\n const components = { ...props.components }\n\n if (values) {\n /*\n Related discussion: https://github.com/lingui/js-lingui/issues/183\n\n Values *might* contain React elements with static content.\n They're replaced with <INDEX /> placeholders and added to `components`.\n\n Example:\n Translation: Hello {name}\n Values: { name: <strong>Jane</strong> }\n\n It'll become \"Hello <0 />\" with components=[<strong>Jane</strong>]\n */\n\n Object.keys(values).forEach((key) => {\n const value = values[key]\n if (!React.isValidElement(value)) return\n\n const index = Object.keys(components).length\n\n components[index] = value\n values[key] = `<${index}/>`\n })\n }\n\n const _translation: string =\n i18n && typeof i18n._ === \"function\"\n ? i18n._(id, values, { message, formats })\n : id // i18n provider isn't loaded at all\n\n const translation = _translation\n ? formatElements(_translation, components)\n : null\n\n if (render === null || component === null) {\n // Although `string` is a valid react element, types only allow `Element`\n // Upstream issue: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/20544\n return (translation as unknown) as React.ReactElement<any, any>\n }\n\n const FallbackComponent = (defaultComponent ||\n React.Fragment) as React.ComponentType<any>\n\n const i18nProps = {\n id,\n message,\n translation,\n isTranslated: id !== translation && message !== translation,\n }\n\n // Validation of `render` and `component` props\n if (render && component) {\n console.error(\n \"You can't use both `component` and `render` prop at the same time. `component` is ignored.\"\n )\n } else if (render && typeof render !== \"function\") {\n console.error(\n `Invalid value supplied to prop \\`render\\`. It must be a function, provided ${render}`\n )\n } else if (component && typeof component !== \"function\") {\n // Apparently, both function components and class components are functions\n // See https://stackoverflow.com/a/41658173/1535540\n console.error(\n `Invalid value supplied to prop \\`component\\`. It must be a React component, provided ${component}`\n )\n return <FallbackComponent {...i18nProps}>{translation}</FallbackComponent>\n }\n\n // Rendering using a render prop\n if (typeof render === \"function\") {\n // Component: render={(props) => <a title={props.translation}>x</a>}\n return render(i18nProps)\n }\n\n // `component` prop has a higher precedence over `defaultComponent`\n const Component = (component || FallbackComponent) as React.ComponentType<any>\n const DefaultComponent = defaultComponent\n\n return DefaultComponent && !component ? (\n <DefaultComponent {...i18nProps}>{translation}</DefaultComponent>\n ) : (\n <Component>{translation}</Component>\n )\n}\n\nTrans.defaultProps = {\n values: {},\n components: {},\n}\n"],"names":["LinguiContext","React","createContext","useLingui","context","useContext","process","env","NODE_ENV","Error","withI18n","o","WrappedComponent","props","isValidElement","i18n","I18nProvider","defaultComponent","forceRenderOnLocaleChange","children","makeContext","getRenderKey","locale","useState","setContext","renderKey","setRenderKey","useEffect","unsubscribe","on","console","log","tagRe","nlRe","voidElementTags","area","base","br","col","embed","hr","img","input","keygen","link","meta","param","source","track","wbr","menuitem","formatElements","value","elements","uniqueId","makeCounter","parts","replace","split","length","tree","before","shift","push","getElements","index","after","element","type","error","createElement","Fragment","cloneElement","key","slice","paired","unpaired","parseInt","concat","count","prefix","Trans","render","component","id","message","formats","values","components","Object","keys","forEach","_translation","_","translation","FallbackComponent","i18nProps","isTranslated","Component","DefaultComponent","defaultProps"],"mappings":";;;;;AAkBA,IAAMA,aAAa,gBAAGC,KAAK,CAACC,aAAN,CAAiC,IAAjC,CAAtB,CAAA;AAEO,SAASC,SAAT,GAAkC;AACvC,EAAA,IAAMC,OAAO,GAAGH,KAAK,CAACI,UAAN,CAA8BL,aAA9B,CAAhB,CAAA;;AAEA,EAAA,IAAIM,OAAO,CAACC,GAAR,CAAYC,QAAZ,KAAyB,YAA7B,EAA2C;AACzC,IAAIJ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CAAA;AAKD,CAAA;AAEDb,KAAK,CAACmB,YAAN,GAAqB;AACnBb,EAAAA,MAAM,EAAE,EADW;AAEnBC,EAAAA,UAAU,EAAE,EAAA;AAFO,CAArB;;;;"}
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../src/I18nProvider.tsx","../../src/format.ts","../../src/Trans.tsx"],"sourcesContent":["import React, { ComponentType, FunctionComponent } from \"react\"\nimport { I18n } from \"@lingui/core\"\nimport { TransRenderProps } from \"./Trans\"\n\nexport type I18nContext = {\n i18n: I18n\n defaultComponent?: ComponentType<TransRenderProps>\n}\n\nexport type withI18nProps = {\n i18n: I18n\n}\n\nexport type I18nProviderProps = I18nContext & {\n forceRenderOnLocaleChange?: boolean\n children?: React.ReactNode\n}\n\nconst LinguiContext = React.createContext<I18nContext>(null)\n\nexport function useLingui(): I18nContext {\n const context = React.useContext<I18nContext>(LinguiContext)\n\n if (process.env.NODE_ENV !== \"production\") {\n if (context == null) {\n throw new Error(\"useLingui hook was used without I18nProvider.\")\n }\n }\n\n return context\n}\n\nexport function withI18n(\n o?: object\n): <P extends withI18nProps>(\n Component: ComponentType<P>\n) => React.ComponentType<Omit<P, \"i18n\">> {\n return <P extends withI18nProps>(\n WrappedComponent: ComponentType<P>\n ): ComponentType<P> => {\n return (props) => {\n if (process.env.NODE_ENV !== \"production\") {\n if (typeof o === \"function\" || React.isValidElement(o)) {\n throw new Error(\n \"withI18n([options]) takes options as a first argument, \" +\n \"but received React component itself. Without options, the Component \" +\n \"should be wrapped as withI18n()(Component), not withI18n(Component).\"\n )\n }\n }\n\n const { i18n } = useLingui()\n return <WrappedComponent {...props} i18n={i18n} />\n }\n }\n}\n\nexport const I18nProvider: FunctionComponent<I18nProviderProps> = ({\n i18n,\n defaultComponent,\n forceRenderOnLocaleChange = true,\n children,\n}) => {\n /**\n * We can't pass `i18n` object directly through context, because even when locale\n * or messages are changed, i18n object is still the same. Context provider compares\n * reference identity and suggested workaround is create a wrapper object every time\n * we need to trigger re-render. See https://reactjs.org/docs/context.html#caveats.\n *\n * Due to this effect we also pass `defaultComponent` in the same context, instead\n * of creating a separate Provider/Consumer pair.\n *\n * We can't use useMemo hook either, because we want to recalculate value manually.\n */\n const makeContext = () => ({\n i18n,\n defaultComponent,\n })\n const getRenderKey = () => {\n return (\n forceRenderOnLocaleChange ? i18n.locale || \"default\" : \"default\"\n ) as string\n }\n\n const [context, setContext] = React.useState<I18nContext>(makeContext()),\n [renderKey, setRenderKey] = React.useState<string>(getRenderKey())\n\n /**\n * Subscribe for locale/message changes\n *\n * I18n object from `@lingui/core` is the single source of truth for all i18n related\n * data (active locale, catalogs). When new messages are loaded or locale is changed\n * we need to trigger re-rendering of LinguiContext.Consumers.\n *\n * We call `setContext(makeContext())` after adding the observer in case the `change`\n * event would already have fired between the inital renderKey calculation and the\n * `useEffect` hook being called. This can happen if locales are loaded/activated\n * async.\n */\n React.useEffect(() => {\n const unsubscribe = i18n.on(\"change\", () => {\n setContext(makeContext())\n setRenderKey(getRenderKey())\n })\n if (renderKey === \"default\") {\n setRenderKey(getRenderKey())\n }\n if (forceRenderOnLocaleChange && renderKey === \"default\") {\n console.log(\n \"I18nProvider did not render. A call to i18n.activate still needs to happen or forceRenderOnLocaleChange must be set to false.\"\n )\n }\n return () => unsubscribe()\n }, [])\n\n if (forceRenderOnLocaleChange && renderKey === \"default\") return null\n\n return (\n <LinguiContext.Provider value={context} key={renderKey}>\n {children}\n </LinguiContext.Provider>\n )\n}\n","import React from \"react\"\n\n// match <tag>paired</tag> and <tag/> unpaired tags\nconst tagRe = /<([a-zA-Z0-9]+)>(.*?)<\\/\\1>|<([a-zA-Z0-9]+)\\/>/\nconst nlRe = /(?:\\r\\n|\\r|\\n)/g\n\n// For HTML, certain tags should omit their close tag. We keep a whitelist for\n// those special-case tags.\nconst voidElementTags = {\n area: true,\n base: true,\n br: true,\n col: true,\n embed: true,\n hr: true,\n img: true,\n input: true,\n keygen: true,\n link: true,\n meta: true,\n param: true,\n source: true,\n track: true,\n wbr: true,\n menuitem: true,\n}\n\n/**\n * `formatElements` - parse string and return tree of react elements\n *\n * `value` is string to be formatted with <tag>Paired<tag/> or <tag/> (unpaired)\n * placeholders. `elements` is a array of react elements which indexes\n * correspond to element indexes in formatted string\n */\nfunction formatElements(\n value: string,\n elements: { [key: string]: React.ReactElement<any> } = {}\n): string | Array<any> {\n const uniqueId = makeCounter(0, \"$lingui$\")\n const parts = value.replace(nlRe, \"\").split(tagRe)\n\n // no inline elements, return\n if (parts.length === 1) return value\n\n const tree = []\n\n const before = parts.shift()\n if (before) tree.push(before)\n\n for (const [index, children, after] of getElements(parts)) {\n let element = elements[index]\n\n if (!element || (voidElementTags[element.type as string] && children)) {\n if (!element) {\n console.error(\n `Can use element at index '${index}' as it is not declared in the original translation`\n )\n } else {\n console.error(\n `${element.type} is a void element tag therefore it must have no children`\n )\n }\n\n // ignore problematic element but push its children and elements after it\n element = React.createElement(React.Fragment)\n }\n\n tree.push(\n React.cloneElement(\n element,\n { key: uniqueId() },\n\n // format children for pair tags\n // unpaired tags might have children if it's a component passed as a variable\n children ? formatElements(children, elements) : element.props.children\n )\n )\n\n if (after) tree.push(after)\n }\n\n return tree\n}\n\n/*\n * `getElements` - return array of element indexes and element childrens\n *\n * `parts` is array of [pairedIndex, children, unpairedIndex, textAfter, ...]\n * where:\n * - `pairedIndex` is index of paired element (undef for unpaired)\n * - `children` are children of paired element (undef for unpaired)\n * - `unpairedIndex` is index of unpaired element (undef for paired)\n * - `textAfter` is string after all elements (empty string, if there's nothing)\n *\n * `parts` length is always multiply of 4\n *\n * Returns: Array<[elementIndex, children, after]>\n */\nfunction getElements(parts) {\n if (!parts.length) return []\n\n const [paired, children, unpaired, after] = parts.slice(0, 4)\n\n return [[paired || unpaired, children || \"\", after]].concat(\n getElements(parts.slice(4, parts.length))\n )\n}\n\nconst makeCounter =\n (count = 0, prefix = \"\") =>\n () =>\n `${prefix}_${count++}`\n\nexport { formatElements }\n","import React from \"react\"\n\nimport { useLingui } from \"./I18nProvider\"\nimport { formatElements } from \"./format\"\n\nexport type TransRenderProps = {\n id?: string\n translation?: React.ReactNode\n children?: React.ReactNode\n message?: string | null\n isTranslated?: boolean\n}\n\nexport type TransProps = {\n id: string\n message?: string\n values: Record<string, unknown>\n context?: string\n components: { [key: string]: React.ElementType | any }\n formats?: Record<string, unknown>\n children?: React.ReactNode\n component?: React.ComponentType<TransRenderProps>\n render?: (props: TransRenderProps) => React.ReactElement<any, any> | null\n}\n\nexport function Trans(props: TransProps): React.ReactElement<any, any> | null {\n const { i18n, defaultComponent } = useLingui()\n const { render, component, id, message, formats } = props\n\n const values = { ...props.values }\n const components = { ...props.components }\n\n if (values) {\n /*\n Related discussion: https://github.com/lingui/js-lingui/issues/183\n\n Values *might* contain React elements with static content.\n They're replaced with <INDEX /> placeholders and added to `components`.\n\n Example:\n Translation: Hello {name}\n Values: { name: <strong>Jane</strong> }\n\n It'll become \"Hello <0 />\" with components=[<strong>Jane</strong>]\n */\n\n Object.keys(values).forEach((key) => {\n const value = values[key]\n if (!React.isValidElement(value)) return\n\n const index = Object.keys(components).length\n\n components[index] = value\n values[key] = `<${index}/>`\n })\n }\n\n const _translation: string =\n i18n && typeof i18n._ === \"function\"\n ? i18n._(id, values, { message, formats })\n : id // i18n provider isn't loaded at all\n\n const translation = _translation\n ? formatElements(_translation, components)\n : null\n\n if (render === null || component === null) {\n // Although `string` is a valid react element, types only allow `Element`\n // Upstream issue: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/20544\n return translation as unknown as React.ReactElement<any, any>\n }\n\n const FallbackComponent = (defaultComponent ||\n React.Fragment) as React.ComponentType<any>\n\n const i18nProps = {\n id,\n message,\n translation,\n isTranslated: id !== translation && message !== translation,\n }\n\n // Validation of `render` and `component` props\n if (render && component) {\n console.error(\n \"You can't use both `component` and `render` prop at the same time. `component` is ignored.\"\n )\n } else if (render && typeof render !== \"function\") {\n console.error(\n `Invalid value supplied to prop \\`render\\`. It must be a function, provided ${render}`\n )\n } else if (component && typeof component !== \"function\") {\n // Apparently, both function components and class components are functions\n // See https://stackoverflow.com/a/41658173/1535540\n console.error(\n `Invalid value supplied to prop \\`component\\`. It must be a React component, provided ${component}`\n )\n return <FallbackComponent {...i18nProps}>{translation}</FallbackComponent>\n }\n\n // Rendering using a render prop\n if (typeof render === \"function\") {\n // Component: render={(props) => <a title={props.translation}>x</a>}\n return render(i18nProps)\n }\n\n // `component` prop has a higher precedence over `defaultComponent`\n const Component = (component || FallbackComponent) as React.ComponentType<any>\n const DefaultComponent = defaultComponent\n\n return DefaultComponent && !component ? (\n <DefaultComponent {...i18nProps}>{translation}</DefaultComponent>\n ) : (\n <Component>{translation}</Component>\n )\n}\n\nTrans.defaultProps = {\n values: {},\n components: {},\n}\n"],"names":["LinguiContext","React","createContext","useLingui","context","useContext","process","env","NODE_ENV","Error","withI18n","o","WrappedComponent","props","isValidElement","i18n","I18nProvider","defaultComponent","forceRenderOnLocaleChange","children","makeContext","getRenderKey","locale","setContext","useState","renderKey","setRenderKey","useEffect","unsubscribe","on","console","log","tagRe","nlRe","voidElementTags","area","base","br","col","embed","hr","img","input","keygen","link","meta","param","source","track","wbr","menuitem","formatElements","value","elements","uniqueId","makeCounter","parts","replace","split","length","tree","before","shift","push","index","after","getElements","element","type","error","createElement","Fragment","cloneElement","key","paired","unpaired","slice","concat","count","prefix","Trans","render","component","id","message","formats","values","components","Object","keys","forEach","_translation","_","translation","FallbackComponent","i18nProps","isTranslated","Component","DefaultComponent","defaultProps"],"mappings":";;;AAkBA,MAAMA,aAAa,gBAAGC,KAAK,CAACC,aAAa,CAAc,IAAI,CAAC,CAAA;AAErD,SAASC,SAAS,GAAgB;AACvC,EAAA,MAAMC,OAAO,GAAGH,KAAK,CAACI,UAAU,CAAcL,aAAa,CAAC,CAAA;AAE5D,EAAA,IAAIM,OAAO,CAACC,GAAG,CAACC,QAAQ,KAAK,YAAY,EAAE;IACzC,IAAIJ,OAAO,IAAI,IAAI,EAAE;AACnB,MAAA,MAAM,I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A;AACA,EAAA,IAAI,OAAOZ,MAAM,KAAK,UAAU,EAAE;AAChC;IACA,OAAOA,MAAM,CAACc,SAAS,CAAC,CAAA;AAC1B,GAAA;;AAEA;AACA,EAAA,MAAME,SAAS,GAAIf,SAAS,IAAIY,iBAA8C,CAAA;EAC9E,MAAMI,gBAAgB,GAAGjF,gBAAgB,CAAA;AAEzC,EAAA,OAAOiF,gBAAgB,IAAI,CAAChB,SAAS,gBACnC,oBAAC,gBAAgB,EAAKa,SAAS,EAAGF,WAAW,CAAoB,gBAEjE,oBAAC,SAAS,EAAA,IAAA,EAAEA,WAAW,CACxB,CAAA;AACH,CAAA;AAEAb,KAAK,CAACmB,YAAY,GAAG;EACnBb,MAAM,EAAE,EAAE;AACVC,EAAAA,UAAU,EAAE,EAAC;AACf,CAAC;;;;"}
|
package/build/index.d.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import React, { ComponentType, FunctionComponent } from 'react';
|
|
2
2
|
import { I18n } from '@lingui/core';
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
type TransRenderProps = {
|
|
5
5
|
id?: string;
|
|
6
6
|
translation?: React.ReactNode;
|
|
7
7
|
children?: React.ReactNode;
|
|
8
8
|
message?: string | null;
|
|
9
9
|
isTranslated?: boolean;
|
|
10
10
|
};
|
|
11
|
-
|
|
11
|
+
type TransProps = {
|
|
12
12
|
id: string;
|
|
13
13
|
message?: string;
|
|
14
14
|
values: Record<string, unknown>;
|
|
@@ -29,19 +29,19 @@ declare namespace Trans {
|
|
|
29
29
|
};
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
|
|
32
|
+
type I18nContext = {
|
|
33
33
|
i18n: I18n;
|
|
34
34
|
defaultComponent?: ComponentType<TransRenderProps>;
|
|
35
35
|
};
|
|
36
|
-
|
|
36
|
+
type withI18nProps = {
|
|
37
37
|
i18n: I18n;
|
|
38
38
|
};
|
|
39
|
-
|
|
39
|
+
type I18nProviderProps = I18nContext & {
|
|
40
40
|
forceRenderOnLocaleChange?: boolean;
|
|
41
41
|
children?: React.ReactNode;
|
|
42
42
|
};
|
|
43
43
|
declare function useLingui(): I18nContext;
|
|
44
|
-
declare function withI18n(o?: object): <P extends withI18nProps>(Component: ComponentType<P>) => React.ComponentType<Omit<P,
|
|
44
|
+
declare function withI18n(o?: object): <P extends withI18nProps>(Component: ComponentType<P>) => React.ComponentType<Omit<P, "i18n">>;
|
|
45
45
|
declare const I18nProvider: FunctionComponent<I18nProviderProps>;
|
|
46
46
|
|
|
47
47
|
export { I18nContext, I18nProvider, I18nProviderProps, Trans, TransProps, TransRenderProps, useLingui, withI18n, withI18nProps };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lingui/react",
|
|
3
|
-
"version": "3.17.
|
|
3
|
+
"version": "3.17.1",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"description": "React components for translations",
|
|
6
6
|
"main": "./build/cjs/index.js",
|
|
@@ -53,11 +53,11 @@
|
|
|
53
53
|
"react": "^16.8.0 || ^17.0.0 || ^18.0.0"
|
|
54
54
|
},
|
|
55
55
|
"dependencies": {
|
|
56
|
-
"@babel/runtime": "^7.
|
|
57
|
-
"@lingui/core": "3.17.
|
|
56
|
+
"@babel/runtime": "^7.20.13",
|
|
57
|
+
"@lingui/core": "3.17.1"
|
|
58
58
|
},
|
|
59
59
|
"devDependencies": {
|
|
60
60
|
"react-testing-library": "^8.0.1"
|
|
61
61
|
},
|
|
62
|
-
"gitHead": "
|
|
62
|
+
"gitHead": "76ef4e8d1c668578ce2c3829ebf35d22ca5e679c"
|
|
63
63
|
}
|