Skip to content

Commit 27e38f2

Browse files
authored
fix: align numeric placeholder generation with babel implementation (#239)
1 parent 300cf86 commit 27e38f2

3 files changed

Lines changed: 48 additions & 2 deletions

File tree

crates/lingui_macro/src/builder.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ pub struct MessageBuilder<'a> {
9292
options: &'a LinguiOptions,
9393
elements_tracking: Vec<(String, JSXOpeningElement)>,
9494
element_index: usize,
95+
// Counter for auto-generated numeric placeholders
96+
numeric_index: usize,
9597
}
9698

9799
impl<'a> MessageBuilder<'a> {
@@ -105,6 +107,7 @@ impl<'a> MessageBuilder<'a> {
105107
options,
106108
elements_tracking: Vec::new(),
107109
element_index: 0,
110+
numeric_index: 0,
108111
};
109112

110113
builder.process_tokens(tokens);
@@ -292,6 +295,12 @@ impl<'a> MessageBuilder<'a> {
292295
}
293296
}
294297

298+
fn next_numeric_index(&mut self) -> String {
299+
let index = self.numeric_index.to_string();
300+
self.numeric_index += 1;
301+
index
302+
}
303+
295304
fn push_exp(&mut self, mut exp: Box<Expr>) -> String {
296305
exp = expand_ts_as_expr(exp);
297306

@@ -334,7 +343,7 @@ impl<'a> MessageBuilder<'a> {
334343
}
335344

336345
// fallback for {...spread} or {}
337-
let index = self.values_indexed.len().to_string();
346+
let index = self.next_numeric_index();
338347

339348
self.values_indexed.push(ValueWithPlaceholder {
340349
placeholder: index.clone(),
@@ -344,7 +353,7 @@ impl<'a> MessageBuilder<'a> {
344353
index
345354
}
346355
_ => {
347-
let index = self.values_indexed.len().to_string();
356+
let index = self.next_numeric_index();
348357

349358
self.values_indexed.push(ValueWithPlaceholder {
350359
placeholder: index.clone(),

crates/lingui_macro/tests/jsx.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,18 @@ to!(
144144
"#
145145
);
146146

147+
to!(
148+
jsx_ph_label_with_nested_plural,
149+
r##"
150+
import { Trans, Plural, ph } from "@lingui/react/macro";
151+
152+
const zones = [1, 2]
153+
const x = <Trans id="key">
154+
{ph({ available: 1 })} of <Plural value={zones.length} one="# zone" other="# zones" /> available
155+
</Trans>
156+
"##
157+
);
158+
147159
to!(
148160
jsx_nested_labels,
149161
r#"
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
source: crates/lingui_macro/tests/jsx.rs
3+
---
4+
import { Trans, Plural, ph } from "@lingui/react/macro";
5+
6+
const zones = [1, 2]
7+
const x = <Trans id="key">
8+
{ph({ available: 1 })} of <Plural value={zones.length} one="# zone" other="# zones" /> available
9+
</Trans>
10+
11+
↓ ↓ ↓ ↓ ↓ ↓
12+
13+
import { Trans as Trans_ } from "@lingui/react";
14+
const zones = [
15+
1,
16+
2
17+
];
18+
const x = <Trans_ {.../*i18n*/ {
19+
id: "key",
20+
values: {
21+
available: 1,
22+
0: zones.length
23+
},
24+
message: "{available} of {0, plural, one {# zone} other {# zones}} available"
25+
}}/>;

0 commit comments

Comments
 (0)