Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions crates/lingui_macro/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand All @@ -105,6 +107,7 @@ impl<'a> MessageBuilder<'a> {
options,
elements_tracking: Vec::new(),
element_index: 0,
numeric_index: 0,
};

builder.process_tokens(tokens);
Expand Down Expand Up @@ -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<Expr>) -> String {
exp = expand_ts_as_expr(exp);

Expand Down Expand Up @@ -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(),
Expand All @@ -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(),
Expand Down
12 changes: 12 additions & 0 deletions crates/lingui_macro/tests/jsx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <Trans id="key">
{ph({ available: 1 })} of <Plural value={zones.length} one="# zone" other="# zones" /> available
</Trans>
"##
);

to!(
jsx_nested_labels,
r#"
Expand Down
Original file line number Diff line number Diff line change
@@ -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 = <Trans id="key">
{ph({ available: 1 })} of <Plural value={zones.length} one="# zone" other="# zones" /> available
</Trans>

↓ ↓ ↓ ↓ ↓ ↓

import { Trans as Trans_ } from "@lingui/react";
const zones = [
1,
2
];
const x = <Trans_ {.../*i18n*/ {
id: "key",
values: {
available: 1,
0: zones.length
},
message: "{available} of {0, plural, one {# zone} other {# zones}} available"
}}/>;
Loading