Skip to content

Commit a4ef30f

Browse files
support comment and context extraction
1 parent 1f2074c commit a4ef30f

21 files changed

Lines changed: 139 additions & 32 deletions

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/lingui_extractor/tests/__snapshots__/js-with-macros.js.json

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"id": "Nu4oKW",
2828
"message": "Description",
2929
"context": null,
30-
"comment": null,
30+
"comment": "description",
3131
"placeholders": {},
3232
"origin": {
3333
"filename": "js-with-macros.js",
@@ -88,7 +88,7 @@
8888
{
8989
"id": "Some ID",
9090
"message": null,
91-
"context": null,
91+
"context": "Context1",
9292
"comment": null,
9393
"placeholders": {},
9494
"origin": {
@@ -100,7 +100,7 @@
100100
{
101101
"id": "Some other ID",
102102
"message": null,
103-
"context": null,
103+
"context": "Context1",
104104
"comment": null,
105105
"placeholders": {},
106106
"origin": {
@@ -112,7 +112,7 @@
112112
{
113113
"id": "Some ID",
114114
"message": null,
115-
"context": null,
115+
"context": "Context2",
116116
"comment": null,
117117
"placeholders": {},
118118
"origin": {
@@ -124,7 +124,7 @@
124124
{
125125
"id": "Some ID",
126126
"message": null,
127-
"context": null,
127+
"context": "Context2",
128128
"comment": null,
129129
"placeholders": {},
130130
"origin": {
@@ -170,5 +170,17 @@
170170
"line": 57,
171171
"column": 14
172172
}
173+
},
174+
{
175+
"id": "AZczQP",
176+
"message": "Some ID",
177+
"context": "Context2",
178+
"comment": "This is a comment for a message",
179+
"placeholders": {},
180+
"origin": {
181+
"filename": "js-with-macros.js",
182+
"line": 65,
183+
"column": 36
184+
}
173185
}
174186
]

crates/lingui_extractor/tests/__snapshots__/jsx-with-macros.js.json

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
},
1010
"origin": {
1111
"filename": "jsx-with-macros.js",
12-
"line": 3,
13-
"column": 9
12+
"line": 4,
13+
"column": 8
1414
}
1515
},
1616
{
@@ -21,7 +21,7 @@
2121
"placeholders": {},
2222
"origin": {
2323
"filename": "jsx-with-macros.js",
24-
"line": 4,
24+
"line": 5,
2525
"column": 28
2626
}
2727
},
@@ -33,7 +33,7 @@
3333
"placeholders": {},
3434
"origin": {
3535
"filename": "jsx-with-macros.js",
36-
"line": 5,
36+
"line": 6,
3737
"column": 28
3838
}
3939
},
@@ -45,7 +45,7 @@
4545
"placeholders": {},
4646
"origin": {
4747
"filename": "jsx-with-macros.js",
48-
"line": 6,
48+
"line": 7,
4949
"column": 28
5050
}
5151
},
@@ -57,7 +57,7 @@
5757
"placeholders": {},
5858
"origin": {
5959
"filename": "jsx-with-macros.js",
60-
"line": 7,
60+
"line": 8,
6161
"column": 16
6262
}
6363
},
@@ -71,8 +71,20 @@
7171
},
7272
"origin": {
7373
"filename": "jsx-with-macros.js",
74-
"line": 9,
74+
"line": 10,
7575
"column": 17
7676
}
77+
},
78+
{
79+
"id": "TFdOtc",
80+
"message": "Some message with comment",
81+
"context": null,
82+
"comment": "This is comment",
83+
"placeholders": {},
84+
"origin": {
85+
"filename": "jsx-with-macros.js",
86+
"line": 15,
87+
"column": 35
88+
}
7789
}
7890
]

crates/lingui_extractor/tests/__snapshots__/jsx-without-trans.js.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
}
1515
},
1616
{
17-
"id": "8qNz+K",
17+
"id": "8qNz-K",
1818
"message": "{count, plural, one {# book} other {# books}}",
1919
"context": "Some context",
2020
"comment": null,

crates/lingui_extractor/tests/fixtures/js-with-macros.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { t, defineMessage, msg, plural } from "@lingui/core/macro"
2-
import { useLingui } from "@lingui/react/macro"
1+
import {t, defineMessage, msg, plural} from "@lingui/core/macro"
2+
import {useLingui} from "@lingui/react/macro"
33

44
t`Message`
55

@@ -49,7 +49,7 @@ const defineMessageAlias = msg({
4949
const defineMessageAlias2 = msg`TplLiteral`
5050

5151
function MyComponent() {
52-
const { t } = useLingui()
52+
const {t} = useLingui()
5353

5454
t`[useLingui]: TplLiteral`
5555

@@ -61,3 +61,9 @@ function MyComponent() {
6161
other: "# books",
6262
})}`
6363
}
64+
65+
const defineMessageWithAllFields = msg({
66+
message: "Some ID",
67+
context: "Context2",
68+
comment: "This is a comment for a message"
69+
})
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
import { Trans } from "@lingui/react/macro"
2-
import { t, plural } from "@lingui/core/macro"
3-
;<Trans>Hi, my name is {name}</Trans>
1+
import {Trans} from "@lingui/react/macro";
2+
import {t, plural} from "@lingui/core/macro";
3+
4+
<Trans>Hi, my name is {name}</Trans>
45
;<Trans context="Context1">Some message</Trans>
56
;<Trans context="Context1">Some other message</Trans>
67
;<Trans context="Context2">Some message</Trans>
7-
;<span title={t`Title`} />
8+
;<span title={t`Title`}/>
89
;<span
910
title={plural(count, {
1011
one: "# book",
1112
other: "# books",
1213
})}
1314
/>
15+
;<Trans comment="This is comment">Some message with comment</Trans>
16+

crates/lingui_extractor/tests/message_extractor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use lingui_extractor::{extract_messages, ExtractorOptions};
33

44
fn extract_and_sort(source_code: &str, filename: &str) -> (Vec<ExtractedMessage>, Vec<String>) {
55
let options = ExtractorOptions {
6-
syntax: Syntax::Typescript(TsSyntax {
6+
parser: Syntax::Typescript(TsSyntax {
77
tsx: true,
88

99
..Default::default()

crates/lingui_macro/src/js_macro_folder.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ where
122122
"id",
123123
generate_message_id(
124124
&parsed.message_str,
125-
&(context_val.unwrap_or_default()),
125+
context_val.as_deref().unwrap_or_default(),
126126
)
127127
.into(),
128128
))
@@ -137,6 +137,19 @@ where
137137
}
138138
}
139139

140+
if !self.ctx.options.strip_non_essential_fields {
141+
if let Some(context) = context_val {
142+
new_props.push(create_key_value_prop("context", context.into()));
143+
}
144+
145+
let comment = get_object_prop(&obj.props, "comment")
146+
.and_then(|prop| get_expr_as_string(&prop.value));
147+
148+
if let Some(comment) = comment {
149+
new_props.push(create_key_value_prop("comment", comment.into()));
150+
}
151+
}
152+
140153
let message_descriptor = Box::new(Expr::Object(ObjectLit {
141154
span,
142155
props: new_props,

crates/lingui_macro/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ where
117117

118118
let parsed = MessageBuilder::parse(trans_visitor.tokens);
119119
let id_attr = get_jsx_attr(&el.opening, "id").and_then(|attr| attr.value.as_ref());
120+
120121
let context_attr =
121122
get_jsx_attr(&el.opening, "context").and_then(|attr| attr.value.as_ref());
122123

@@ -148,6 +149,14 @@ where
148149
}
149150

150151
if !self.ctx.options.strip_non_essential_fields {
152+
let comment_attr = get_jsx_attr(&el.opening, "comment")
153+
.and_then(|attr| attr.value.as_ref())
154+
.and_then(get_jsx_attr_value_as_string);
155+
156+
if let Some(comment) = comment_attr {
157+
message_descriptor_props.push(create_key_value_prop("comment", comment.into()));
158+
}
159+
151160
message_descriptor_props.push(create_key_value_prop("message", parsed.message));
152161

153162
if let Some(context_attr) = context_attr {

crates/lingui_macro/tests/__swc_snapshots__/tests/js_define_message.rs/should_expand_macros.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ const message = /*i18n*/ {
33
message: "{count, plural, one {book} other {books}}",
44
values: {
55
count: count
6-
}
6+
},
7+
comment: "Description"
78
};

0 commit comments

Comments
 (0)