diff --git a/crates/lingui_macro/src/builder.rs b/crates/lingui_macro/src/builder.rs index 3d19754..82b5250 100644 --- a/crates/lingui_macro/src/builder.rs +++ b/crates/lingui_macro/src/builder.rs @@ -92,6 +92,8 @@ pub struct MessageBuilder<'a> { options: &'a LinguiOptions, elements_tracking: Vec<(String, JSXOpeningElement)>, element_index: usize, + // Counter for auto-generated numeric placeholders + numeric_index: usize, } impl<'a> MessageBuilder<'a> { @@ -105,6 +107,7 @@ impl<'a> MessageBuilder<'a> { options, elements_tracking: Vec::new(), element_index: 0, + numeric_index: 0, }; builder.process_tokens(tokens); @@ -292,6 +295,12 @@ impl<'a> MessageBuilder<'a> { } } + fn next_numeric_index(&mut self) -> String { + let index = self.numeric_index.to_string(); + self.numeric_index += 1; + index + } + fn push_exp(&mut self, mut exp: Box) -> String { exp = expand_ts_as_expr(exp); @@ -334,7 +343,7 @@ impl<'a> MessageBuilder<'a> { } // fallback for {...spread} or {} - let index = self.values_indexed.len().to_string(); + let index = self.next_numeric_index(); self.values_indexed.push(ValueWithPlaceholder { placeholder: index.clone(), @@ -344,7 +353,7 @@ impl<'a> MessageBuilder<'a> { index } _ => { - let index = self.values_indexed.len().to_string(); + let index = self.next_numeric_index(); self.values_indexed.push(ValueWithPlaceholder { placeholder: index.clone(), diff --git a/crates/lingui_macro/tests/jsx.rs b/crates/lingui_macro/tests/jsx.rs index 998498b..ed24e1f 100644 --- a/crates/lingui_macro/tests/jsx.rs +++ b/crates/lingui_macro/tests/jsx.rs @@ -144,6 +144,18 @@ to!( "# ); +to!( + jsx_ph_label_with_nested_plural, + r##" + import { Trans, Plural, ph } from "@lingui/react/macro"; + + const zones = [1, 2] + const x = + {ph({ available: 1 })} of available + + "## +); + to!( jsx_nested_labels, r#" diff --git a/crates/lingui_macro/tests/snapshots/jsx__jsx_ph_label_with_nested_plural.snap b/crates/lingui_macro/tests/snapshots/jsx__jsx_ph_label_with_nested_plural.snap new file mode 100644 index 0000000..329ca66 --- /dev/null +++ b/crates/lingui_macro/tests/snapshots/jsx__jsx_ph_label_with_nested_plural.snap @@ -0,0 +1,25 @@ +--- +source: crates/lingui_macro/tests/jsx.rs +--- +import { Trans, Plural, ph } from "@lingui/react/macro"; + +const zones = [1, 2] +const x = + {ph({ available: 1 })} of available + + +↓ ↓ ↓ ↓ ↓ ↓ + +import { Trans as Trans_ } from "@lingui/react"; +const zones = [ + 1, + 2 +]; +const x = ;