@lingui/macro 3.11.1 → 3.13.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/constants.js +4 -2
- package/global.d.ts +160 -12
- package/index.d.ts +152 -12
- package/index.js +3 -3
- package/macroJs.js +52 -14
- package/macroJsx.js +10 -3
- package/package.json +4 -2
package/constants.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.EXTRACT_MARK = exports.COMMENT = exports.MESSAGE = exports.ID = void 0;
|
|
6
|
+
exports.CONTEXT = exports.EXTRACT_MARK = exports.COMMENT = exports.MESSAGE = exports.ID = void 0;
|
|
7
7
|
var ID = "id";
|
|
8
8
|
exports.ID = ID;
|
|
9
9
|
var MESSAGE = "message";
|
|
@@ -11,4 +11,6 @@ exports.MESSAGE = MESSAGE;
|
|
|
11
11
|
var COMMENT = "comment";
|
|
12
12
|
exports.COMMENT = COMMENT;
|
|
13
13
|
var EXTRACT_MARK = "i18n";
|
|
14
|
-
exports.EXTRACT_MARK = EXTRACT_MARK;
|
|
14
|
+
exports.EXTRACT_MARK = EXTRACT_MARK;
|
|
15
|
+
var CONTEXT = "context";
|
|
16
|
+
exports.CONTEXT = CONTEXT;
|
package/global.d.ts
CHANGED
|
@@ -1,16 +1,84 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
// @ts-ignore
|
|
2
|
+
declare module "@lingui/macro" {
|
|
3
|
+
import type { MessageDescriptor, I18n } from "@lingui/core"
|
|
3
4
|
|
|
4
5
|
export type BasicType = {
|
|
5
6
|
id?: string
|
|
6
7
|
comment?: string
|
|
7
8
|
}
|
|
8
9
|
|
|
10
|
+
/**
|
|
11
|
+
* Translates a message descriptor
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```
|
|
15
|
+
* import { t } from "@lingui/macro";
|
|
16
|
+
* const message = t({
|
|
17
|
+
* id: "msg.hello",
|
|
18
|
+
* comment: "Greetings at the homepage",
|
|
19
|
+
* message: `Hello ${name}`,
|
|
20
|
+
* });
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```
|
|
25
|
+
* import { t } from "@lingui/macro";
|
|
26
|
+
* const message = t({
|
|
27
|
+
* id: "msg.plural",
|
|
28
|
+
* message: plural(value, { one: "...", other: "..." }),
|
|
29
|
+
* });
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* @param descriptor The message descriptor to translate
|
|
33
|
+
*/
|
|
34
|
+
export function t(descriptor: MessageDescriptor): string
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Translates a template string using the global I18n instance
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```
|
|
41
|
+
* import { t } from "@lingui/macro";
|
|
42
|
+
* const message = t`Hello ${name}`;
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
9
45
|
export function t(
|
|
10
|
-
literals: TemplateStringsArray
|
|
46
|
+
literals: TemplateStringsArray,
|
|
11
47
|
...placeholders: any[]
|
|
12
48
|
): string
|
|
13
49
|
|
|
50
|
+
/**
|
|
51
|
+
* Translates a template string or message descriptor using a given I18n instance
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```
|
|
55
|
+
* import { t } from "@lingui/macro";
|
|
56
|
+
* import { I18n } from "@lingui/core";
|
|
57
|
+
* const i18n = new I18n({
|
|
58
|
+
* locale: "nl",
|
|
59
|
+
* messages: { "Hello {0}": "Hallo {0}" },
|
|
60
|
+
* });
|
|
61
|
+
* const message = t(i18n)`Hello ${name}`;
|
|
62
|
+
* ```
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```
|
|
66
|
+
* import { t } from "@lingui/macro";
|
|
67
|
+
* import { I18n } from "@lingui/core";
|
|
68
|
+
* const i18n = new I18n({
|
|
69
|
+
* locale: "nl",
|
|
70
|
+
* messages: { "Hello {0}": "Hallo {0}" },
|
|
71
|
+
* });
|
|
72
|
+
* const message = t(i18n)({ message: `Hello ${name}` });
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
export function t(
|
|
76
|
+
i18n: I18n
|
|
77
|
+
): {
|
|
78
|
+
(literals: TemplateStringsArray, ...placeholders: any[]): string
|
|
79
|
+
(descriptor: MessageDescriptor): string
|
|
80
|
+
}
|
|
81
|
+
|
|
14
82
|
export type UnderscoreDigit<T = string> = { [digit: string]: T }
|
|
15
83
|
export type ChoiceOptions<T = string> = {
|
|
16
84
|
offset?: number
|
|
@@ -21,20 +89,100 @@ declare module '@lingui/macro' {
|
|
|
21
89
|
other?: T
|
|
22
90
|
} & UnderscoreDigit<T>
|
|
23
91
|
|
|
24
|
-
|
|
92
|
+
/**
|
|
93
|
+
* Pluralize a message
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* ```
|
|
97
|
+
* import { plural } from "@lingui/macro";
|
|
98
|
+
* const message = plural(count, {
|
|
99
|
+
* one: "# Book",
|
|
100
|
+
* other: "# Books",
|
|
101
|
+
* });
|
|
102
|
+
* ```
|
|
103
|
+
*
|
|
104
|
+
* @param value Determines the plural form
|
|
105
|
+
* @param options Object with available plural forms
|
|
106
|
+
*/
|
|
107
|
+
export function plural(
|
|
108
|
+
value: number | string,
|
|
109
|
+
options: ChoiceOptions & BasicType
|
|
110
|
+
): string
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Pluralize a message using ordinal forms
|
|
114
|
+
*
|
|
115
|
+
* Similar to `plural` but instead of using cardinal plural forms,
|
|
116
|
+
* it uses ordinal forms.
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* ```
|
|
120
|
+
* import { selectOrdinal } from "@lingui/macro";
|
|
121
|
+
* const message = selectOrdinal(count, {
|
|
122
|
+
* one: "1st",
|
|
123
|
+
* two: "2nd",
|
|
124
|
+
* few: "3rd",
|
|
125
|
+
* other: "#th",
|
|
126
|
+
* });
|
|
127
|
+
* ```
|
|
128
|
+
*
|
|
129
|
+
* @param value Determines the plural form
|
|
130
|
+
* @param options Object with available plural forms
|
|
131
|
+
*/
|
|
25
132
|
export function selectOrdinal(
|
|
26
|
-
|
|
133
|
+
value: number | string,
|
|
27
134
|
options: ChoiceOptions & BasicType
|
|
28
135
|
): string
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Selects a translation based on a value
|
|
139
|
+
*
|
|
140
|
+
* Select works like a switch statement. It will
|
|
141
|
+
* select one of the forms in `options` object which
|
|
142
|
+
* key matches exactly `value`.
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* ```
|
|
146
|
+
* import { select } from "@lingui/macro";
|
|
147
|
+
* const message = select(gender, {
|
|
148
|
+
* male: "he",
|
|
149
|
+
* female: "she",
|
|
150
|
+
* other: "they",
|
|
151
|
+
* });
|
|
152
|
+
* ```
|
|
153
|
+
*
|
|
154
|
+
* @param value The key of choices to use
|
|
155
|
+
* @param choices
|
|
156
|
+
*/
|
|
157
|
+
export function select(
|
|
158
|
+
value: string,
|
|
159
|
+
choices: Record<string, string> & BasicType
|
|
160
|
+
): string
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Define a message for later use
|
|
164
|
+
*
|
|
165
|
+
* `defineMessage` can be used to add comments for translators,
|
|
166
|
+
* or to override the message ID.
|
|
167
|
+
*
|
|
168
|
+
* @example
|
|
169
|
+
* ```
|
|
170
|
+
* import { defineMessage } from "@lingui/macro";
|
|
171
|
+
* const message = defineMessage({
|
|
172
|
+
* comment: "Greetings on the welcome page",
|
|
173
|
+
* message: `Welcome, ${name}!`,
|
|
174
|
+
* });
|
|
175
|
+
* ```
|
|
176
|
+
*
|
|
177
|
+
* @param descriptor The message descriptor
|
|
178
|
+
*/
|
|
179
|
+
export function defineMessage(
|
|
180
|
+
descriptor: MessageDescriptor
|
|
181
|
+
): MessageDescriptor
|
|
34
182
|
|
|
35
183
|
export type ChoiceProps = {
|
|
36
184
|
value?: string | number
|
|
37
|
-
}
|
|
185
|
+
} & ChoiceOptions<string>
|
|
38
186
|
|
|
39
187
|
/**
|
|
40
188
|
* The types should be changed after this PR is merged
|
|
@@ -60,4 +208,4 @@ declare module '@lingui/macro' {
|
|
|
60
208
|
export const Plural: any
|
|
61
209
|
export const Select: any
|
|
62
210
|
export const SelectOrdinal: any
|
|
63
|
-
}
|
|
211
|
+
}
|
package/index.d.ts
CHANGED
|
@@ -1,12 +1,7 @@
|
|
|
1
1
|
import type { ReactElement, ComponentType, ReactNode } from "react"
|
|
2
|
-
import type { MessageDescriptor } from "@lingui/core"
|
|
2
|
+
import type { MessageDescriptor, I18n } from "@lingui/core"
|
|
3
3
|
import type { TransRenderProps } from "@lingui/react"
|
|
4
4
|
|
|
5
|
-
export function t(
|
|
6
|
-
literals: TemplateStringsArray | MessageDescriptor,
|
|
7
|
-
...placeholders: any[]
|
|
8
|
-
): string
|
|
9
|
-
|
|
10
5
|
export type UnderscoreDigit<T = string> = { [digit: string]: T }
|
|
11
6
|
export type ChoiceOptions<T = string> = {
|
|
12
7
|
offset?: number
|
|
@@ -17,21 +12,166 @@ export type ChoiceOptions<T = string> = {
|
|
|
17
12
|
other?: T
|
|
18
13
|
} & UnderscoreDigit<T>
|
|
19
14
|
|
|
20
|
-
|
|
15
|
+
/**
|
|
16
|
+
* Translates a message descriptor
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```
|
|
20
|
+
* import { t } from "@lingui/macro";
|
|
21
|
+
* const message = t({
|
|
22
|
+
* id: "msg.hello",
|
|
23
|
+
* comment: "Greetings at the homepage",
|
|
24
|
+
* message: `Hello ${name}`,
|
|
25
|
+
* });
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```
|
|
30
|
+
* import { t } from "@lingui/macro";
|
|
31
|
+
* const message = t({
|
|
32
|
+
* id: "msg.plural",
|
|
33
|
+
* message: plural(value, { one: "...", other: "..." }),
|
|
34
|
+
* });
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* @param descriptor The message descriptor to translate
|
|
38
|
+
*/
|
|
39
|
+
export function t(descriptor: MessageDescriptor): string
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Translates a template string using the global I18n instance
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```
|
|
46
|
+
* import { t } from "@lingui/macro";
|
|
47
|
+
* const message = t`Hello ${name}`;
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
export function t(
|
|
51
|
+
literals: TemplateStringsArray,
|
|
52
|
+
...placeholders: any[]
|
|
53
|
+
): string
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Translates a template string or message descriptor using a given I18n instance
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```
|
|
60
|
+
* import { t } from "@lingui/macro";
|
|
61
|
+
* import { I18n } from "@lingui/core";
|
|
62
|
+
* const i18n = new I18n({
|
|
63
|
+
* locale: "nl",
|
|
64
|
+
* messages: { "Hello {0}": "Hallo {0}" },
|
|
65
|
+
* });
|
|
66
|
+
* const message = t(i18n)`Hello ${name}`;
|
|
67
|
+
* ```
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```
|
|
71
|
+
* import { t } from "@lingui/macro";
|
|
72
|
+
* import { I18n } from "@lingui/core";
|
|
73
|
+
* const i18n = new I18n({
|
|
74
|
+
* locale: "nl",
|
|
75
|
+
* messages: { "Hello {0}": "Hallo {0}" },
|
|
76
|
+
* });
|
|
77
|
+
* const message = t(i18n)({ message: `Hello ${name}` });
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
export function t(
|
|
81
|
+
i18n: I18n
|
|
82
|
+
): {
|
|
83
|
+
(literals: TemplateStringsArray, ...placeholders: any[]): string
|
|
84
|
+
(descriptor: MessageDescriptor): string
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Pluralize a message
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
* ```
|
|
92
|
+
* import { plural } from "@lingui/macro";
|
|
93
|
+
* const message = plural(count, {
|
|
94
|
+
* one: "# Book",
|
|
95
|
+
* other: "# Books",
|
|
96
|
+
* });
|
|
97
|
+
* ```
|
|
98
|
+
*
|
|
99
|
+
* @param value Determines the plural form
|
|
100
|
+
* @param options Object with available plural forms
|
|
101
|
+
*/
|
|
102
|
+
export function plural(value: number | string, options: ChoiceOptions): string
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Pluralize a message using ordinal forms
|
|
106
|
+
*
|
|
107
|
+
* Similar to `plural` but instead of using cardinal plural forms,
|
|
108
|
+
* it uses ordinal forms.
|
|
109
|
+
*
|
|
110
|
+
* @example
|
|
111
|
+
* ```
|
|
112
|
+
* import { selectOrdinal } from "@lingui/macro";
|
|
113
|
+
* const message = selectOrdinal(count, {
|
|
114
|
+
* one: "1st",
|
|
115
|
+
* two: "2nd",
|
|
116
|
+
* few: "3rd",
|
|
117
|
+
* other: "#th",
|
|
118
|
+
* });
|
|
119
|
+
* ```
|
|
120
|
+
*
|
|
121
|
+
* @param value Determines the plural form
|
|
122
|
+
* @param options Object with available plural forms
|
|
123
|
+
*/
|
|
21
124
|
export function selectOrdinal(
|
|
22
|
-
|
|
125
|
+
value: number | string,
|
|
23
126
|
options: ChoiceOptions
|
|
24
127
|
): string
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Selects a translation based on a value
|
|
131
|
+
*
|
|
132
|
+
* Select works like a switch statement. It will
|
|
133
|
+
* select one of the forms in `options` object which
|
|
134
|
+
* key matches exactly `value`.
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* ```
|
|
138
|
+
* import { select } from "@lingui/macro";
|
|
139
|
+
* const message = select(gender, {
|
|
140
|
+
* male: "he",
|
|
141
|
+
* female: "she",
|
|
142
|
+
* other: "they",
|
|
143
|
+
* });
|
|
144
|
+
* ```
|
|
145
|
+
*
|
|
146
|
+
* @param value The key of choices to use
|
|
147
|
+
* @param choices
|
|
148
|
+
*/
|
|
149
|
+
export function select(value: string, choices: ChoiceOptions): string
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Define a message for later use
|
|
153
|
+
*
|
|
154
|
+
* `defineMessage` can be used to add comments for translators,
|
|
155
|
+
* or to override the message ID.
|
|
156
|
+
*
|
|
157
|
+
* @example
|
|
158
|
+
* ```
|
|
159
|
+
* import { defineMessage } from "@lingui/macro";
|
|
160
|
+
* const message = defineMessage({
|
|
161
|
+
* comment: "Greetings on the welcome page",
|
|
162
|
+
* message: `Welcome, ${name}!`,
|
|
163
|
+
* });
|
|
164
|
+
* ```
|
|
165
|
+
*
|
|
166
|
+
* @param descriptor The message descriptor
|
|
167
|
+
*/
|
|
29
168
|
export function defineMessage(descriptor: MessageDescriptor): MessageDescriptor
|
|
30
169
|
|
|
31
170
|
export type TransProps = {
|
|
32
171
|
id?: string
|
|
33
172
|
comment?: string
|
|
34
173
|
values?: Record<string, unknown>
|
|
174
|
+
context?: string
|
|
35
175
|
component?: React.ComponentType<TransRenderProps>
|
|
36
176
|
render?: (props: TransRenderProps) => ReactElement<any, any> | null
|
|
37
177
|
}
|
package/index.js
CHANGED
|
@@ -55,6 +55,7 @@ function macro(_ref) {
|
|
|
55
55
|
babel = _ref.babel;
|
|
56
56
|
var jsxNodes = [];
|
|
57
57
|
var jsNodes = [];
|
|
58
|
+
var needsI18nImport = false;
|
|
58
59
|
Object.keys(references).forEach(function (tagName) {
|
|
59
60
|
var nodes = references[tagName];
|
|
60
61
|
var macroType = getMacroType(tagName);
|
|
@@ -79,7 +80,7 @@ function macro(_ref) {
|
|
|
79
80
|
var macro = new _macroJs.default(babel, {
|
|
80
81
|
i18nImportName: i18nImportName
|
|
81
82
|
});
|
|
82
|
-
macro.replacePath(path);
|
|
83
|
+
if (macro.replacePath(path)) needsI18nImport = true;
|
|
83
84
|
});
|
|
84
85
|
jsxNodes.filter(isRootPath(jsxNodes)).forEach(function (path) {
|
|
85
86
|
if (alreadyVisited(path)) return;
|
|
@@ -87,7 +88,7 @@ function macro(_ref) {
|
|
|
87
88
|
macro.replacePath(path);
|
|
88
89
|
});
|
|
89
90
|
|
|
90
|
-
if (
|
|
91
|
+
if (needsI18nImport) {
|
|
91
92
|
addImport(babel, state, i18nImportModule, i18nImportName);
|
|
92
93
|
}
|
|
93
94
|
|
|
@@ -146,7 +147,6 @@ function alreadyVisited(path) {
|
|
|
146
147
|
|
|
147
148
|
function getMacroType(tagName) {
|
|
148
149
|
switch (tagName) {
|
|
149
|
-
case "defineMessages":
|
|
150
150
|
case "defineMessage":
|
|
151
151
|
case "arg":
|
|
152
152
|
case "t":
|
package/macroJs.js
CHANGED
|
@@ -53,7 +53,7 @@ var MacroJs = /*#__PURE__*/function () {
|
|
|
53
53
|
(0, _defineProperty2.default)(this, "types", void 0);
|
|
54
54
|
(0, _defineProperty2.default)(this, "i18nImportName", void 0);
|
|
55
55
|
(0, _defineProperty2.default)(this, "_expressionIndex", void 0);
|
|
56
|
-
(0, _defineProperty2.default)(this, "replacePathWithMessage", function (path, _ref3) {
|
|
56
|
+
(0, _defineProperty2.default)(this, "replacePathWithMessage", function (path, _ref3, linguiInstance) {
|
|
57
57
|
var id = _ref3.id,
|
|
58
58
|
message = _ref3.message,
|
|
59
59
|
values = _ref3.values,
|
|
@@ -87,7 +87,7 @@ var MacroJs = /*#__PURE__*/function () {
|
|
|
87
87
|
args.push(_this.types.objectExpression(options));
|
|
88
88
|
}
|
|
89
89
|
|
|
90
|
-
var newNode = _this.types.callExpression(_this.types.memberExpression(_this.types.identifier(_this.i18nImportName), _this.types.identifier("_")), args); // preserve line number
|
|
90
|
+
var newNode = _this.types.callExpression(_this.types.memberExpression(linguiInstance !== null && linguiInstance !== void 0 ? linguiInstance : _this.types.identifier(_this.i18nImportName), _this.types.identifier("_")), args); // preserve line number
|
|
91
91
|
|
|
92
92
|
|
|
93
93
|
newNode.loc = path.node.loc;
|
|
@@ -102,24 +102,60 @@ var MacroJs = /*#__PURE__*/function () {
|
|
|
102
102
|
if (_this.isDefineMessage(path.node)) {
|
|
103
103
|
_this.replaceDefineMessage(path);
|
|
104
104
|
|
|
105
|
-
return;
|
|
105
|
+
return true;
|
|
106
|
+
} // t(i18nInstance)`Message` -> i18nInstance._('Message')
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
if (_this.types.isCallExpression(path.node) && _this.types.isTaggedTemplateExpression(path.parentPath.node) && _this.types.isIdentifier(path.node.arguments[0]) && _this.isIdentifier(path.node.callee, "t")) {
|
|
110
|
+
// Use the first argument as i18n instance instead of the default i18n instance
|
|
111
|
+
var i18nInstance = path.node.arguments[0];
|
|
112
|
+
|
|
113
|
+
var _tokens = _this.tokenizeNode(path.parentPath.node);
|
|
114
|
+
|
|
115
|
+
var _messageFormat = new _icu.default();
|
|
116
|
+
|
|
117
|
+
var _messageFormat$fromTo = _messageFormat.fromTokens(_tokens),
|
|
118
|
+
_messageRaw = _messageFormat$fromTo.message,
|
|
119
|
+
_values = _messageFormat$fromTo.values,
|
|
120
|
+
_id = _messageFormat$fromTo.id,
|
|
121
|
+
_comment = _messageFormat$fromTo.comment;
|
|
122
|
+
|
|
123
|
+
var _message = normalizeWhitespace(_messageRaw);
|
|
124
|
+
|
|
125
|
+
_this.replacePathWithMessage(path.parentPath, {
|
|
126
|
+
id: _id,
|
|
127
|
+
message: _message,
|
|
128
|
+
values: _values,
|
|
129
|
+
comment: _comment
|
|
130
|
+
}, i18nInstance);
|
|
131
|
+
|
|
132
|
+
return false;
|
|
133
|
+
} // t(i18nInstance)(messageDescriptor) -> i18nInstance._(messageDescriptor)
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
if (_this.types.isCallExpression(path.node) && _this.types.isCallExpression(path.parentPath.node) && _this.types.isIdentifier(path.node.arguments[0]) && _this.isIdentifier(path.node.callee, "t")) {
|
|
137
|
+
var _i18nInstance = path.node.arguments[0];
|
|
138
|
+
|
|
139
|
+
_this.replaceTAsFunction(path.parentPath, _i18nInstance);
|
|
140
|
+
|
|
141
|
+
return false;
|
|
106
142
|
}
|
|
107
143
|
|
|
108
144
|
if (_this.types.isCallExpression(path.node) && _this.isIdentifier(path.node.callee, "t")) {
|
|
109
145
|
_this.replaceTAsFunction(path);
|
|
110
146
|
|
|
111
|
-
return;
|
|
147
|
+
return true;
|
|
112
148
|
}
|
|
113
149
|
|
|
114
150
|
var tokens = _this.tokenizeNode(path.node);
|
|
115
151
|
|
|
116
152
|
var messageFormat = new _icu.default();
|
|
117
153
|
|
|
118
|
-
var _messageFormat$
|
|
119
|
-
messageRaw = _messageFormat$
|
|
120
|
-
values = _messageFormat$
|
|
121
|
-
id = _messageFormat$
|
|
122
|
-
comment = _messageFormat$
|
|
154
|
+
var _messageFormat$fromTo2 = messageFormat.fromTokens(tokens),
|
|
155
|
+
messageRaw = _messageFormat$fromTo2.message,
|
|
156
|
+
values = _messageFormat$fromTo2.values,
|
|
157
|
+
id = _messageFormat$fromTo2.id,
|
|
158
|
+
comment = _messageFormat$fromTo2.comment;
|
|
123
159
|
|
|
124
160
|
var message = normalizeWhitespace(messageRaw);
|
|
125
161
|
|
|
@@ -129,6 +165,8 @@ var MacroJs = /*#__PURE__*/function () {
|
|
|
129
165
|
values: values,
|
|
130
166
|
comment: comment
|
|
131
167
|
});
|
|
168
|
+
|
|
169
|
+
return true;
|
|
132
170
|
});
|
|
133
171
|
(0, _defineProperty2.default)(this, "replaceDefineMessage", function (path) {
|
|
134
172
|
// reset the expression counter
|
|
@@ -138,10 +176,10 @@ var MacroJs = /*#__PURE__*/function () {
|
|
|
138
176
|
|
|
139
177
|
path.replaceWith(descriptor);
|
|
140
178
|
});
|
|
141
|
-
(0, _defineProperty2.default)(this, "replaceTAsFunction", function (path) {
|
|
179
|
+
(0, _defineProperty2.default)(this, "replaceTAsFunction", function (path, linguiInstance) {
|
|
142
180
|
var descriptor = _this.processDescriptor(path.node.arguments[0]);
|
|
143
181
|
|
|
144
|
-
var newNode = _this.types.callExpression(_this.types.memberExpression(_this.types.identifier(_this.i18nImportName), _this.types.identifier("_")), [descriptor]);
|
|
182
|
+
var newNode = _this.types.callExpression(_this.types.memberExpression(linguiInstance !== null && linguiInstance !== void 0 ? linguiInstance : _this.types.identifier(_this.i18nImportName), _this.types.identifier("_")), [descriptor]);
|
|
145
183
|
|
|
146
184
|
path.replaceWith(newNode);
|
|
147
185
|
});
|
|
@@ -166,9 +204,9 @@ var MacroJs = /*#__PURE__*/function () {
|
|
|
166
204
|
if (tokens != null) {
|
|
167
205
|
var messageFormat = new _icu.default();
|
|
168
206
|
|
|
169
|
-
var _messageFormat$
|
|
170
|
-
messageRaw = _messageFormat$
|
|
171
|
-
values = _messageFormat$
|
|
207
|
+
var _messageFormat$fromTo3 = messageFormat.fromTokens(tokens),
|
|
208
|
+
messageRaw = _messageFormat$fromTo3.message,
|
|
209
|
+
values = _messageFormat$fromTo3.values;
|
|
172
210
|
|
|
173
211
|
var message = normalizeWhitespace(messageRaw);
|
|
174
212
|
messageNode = _this.types.stringLiteral(message);
|
package/macroJsx.js
CHANGED
|
@@ -81,7 +81,8 @@ var MacroJSX = /*#__PURE__*/function () {
|
|
|
81
81
|
var _this$stripMacroAttri = _this.stripMacroAttributes(path.node),
|
|
82
82
|
attributes = _this$stripMacroAttri.attributes,
|
|
83
83
|
id = _this$stripMacroAttri.id,
|
|
84
|
-
comment = _this$stripMacroAttri.comment
|
|
84
|
+
comment = _this$stripMacroAttri.comment,
|
|
85
|
+
context = _this$stripMacroAttri.context;
|
|
85
86
|
|
|
86
87
|
if (!id && !message) {
|
|
87
88
|
return;
|
|
@@ -103,6 +104,10 @@ var MacroJSX = /*#__PURE__*/function () {
|
|
|
103
104
|
if (comment) {
|
|
104
105
|
attributes.push(_this.types.jsxAttribute(_this.types.jsxIdentifier(_constants.COMMENT), _this.types.stringLiteral(comment)));
|
|
105
106
|
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (context) {
|
|
110
|
+
attributes.push(_this.types.jsxAttribute(_this.types.jsxIdentifier(_constants.CONTEXT), _this.types.stringLiteral(context)));
|
|
106
111
|
} // Parameters for variable substitution
|
|
107
112
|
|
|
108
113
|
|
|
@@ -147,7 +152,8 @@ var MacroJSX = /*#__PURE__*/function () {
|
|
|
147
152
|
var id = attributes.filter(_this.attrName([_constants.ID]))[0];
|
|
148
153
|
var message = attributes.filter(_this.attrName([_constants.MESSAGE]))[0];
|
|
149
154
|
var comment = attributes.filter(_this.attrName([_constants.COMMENT]))[0];
|
|
150
|
-
var
|
|
155
|
+
var context = attributes.filter(_this.attrName([_constants.CONTEXT]))[0];
|
|
156
|
+
var reserved = [_constants.ID, _constants.MESSAGE, _constants.COMMENT, _constants.CONTEXT];
|
|
151
157
|
|
|
152
158
|
if (_this.isI18nComponent(node)) {// no reserved prop names
|
|
153
159
|
} else if (_this.isChoiceComponent(node)) {
|
|
@@ -158,6 +164,7 @@ var MacroJSX = /*#__PURE__*/function () {
|
|
|
158
164
|
id: maybeNodeValue(id),
|
|
159
165
|
message: maybeNodeValue(message),
|
|
160
166
|
comment: maybeNodeValue(comment),
|
|
167
|
+
context: maybeNodeValue(context),
|
|
161
168
|
attributes: attributes.filter(_this.attrName(reserved, true))
|
|
162
169
|
};
|
|
163
170
|
});
|
|
@@ -225,7 +232,7 @@ var MacroJSX = /*#__PURE__*/function () {
|
|
|
225
232
|
(0, _defineProperty2.default)(this, "tokenizeChoiceComponent", function (node) {
|
|
226
233
|
var element = node.openingElement;
|
|
227
234
|
var format = element.name.name.toLowerCase();
|
|
228
|
-
var props = element.attributes.filter(_this.attrName([_constants.ID, _constants.COMMENT, _constants.MESSAGE, "key", // we remove <Trans /> react props that are not useful for translation
|
|
235
|
+
var props = element.attributes.filter(_this.attrName([_constants.ID, _constants.COMMENT, _constants.MESSAGE, _constants.CONTEXT, "key", // we remove <Trans /> react props that are not useful for translation
|
|
229
236
|
"render", "component", "components"], true));
|
|
230
237
|
var token = {
|
|
231
238
|
type: "arg",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lingui/macro",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.13.1",
|
|
4
4
|
"description": "Macro for generating messages in ICU MessageFormat syntax",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"author": {
|
|
@@ -30,10 +30,12 @@
|
|
|
30
30
|
],
|
|
31
31
|
"dependencies": {
|
|
32
32
|
"@babel/runtime": "^7.11.2",
|
|
33
|
-
"@lingui/conf": "^3.
|
|
33
|
+
"@lingui/conf": "^3.13.1",
|
|
34
34
|
"ramda": "^0.27.1"
|
|
35
35
|
},
|
|
36
36
|
"peerDependencies": {
|
|
37
|
+
"@lingui/core": "^3.13.0",
|
|
38
|
+
"@lingui/react": "^3.13.0",
|
|
37
39
|
"babel-plugin-macros": "2 || 3"
|
|
38
40
|
}
|
|
39
41
|
}
|