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
27 changes: 27 additions & 0 deletions crates/lingui_macro/src/macro_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,13 @@ pub fn tokenize_tpl(ctx: &mut MacroCtx, tpl: &Tpl) -> Vec<MsgToken> {
}
}

if let Expr::TaggedTpl(tagged_tpl) = exp.as_ref() {
if let Some(inner_tokens) = try_tokenize_tagged_tpl(ctx, tagged_tpl) {
tokens.extend(inner_tokens);
continue;
}
}

let arg = tokenize_expr_to_arg(ctx, exp.clone());
tokens.push(MsgToken::Arg(arg));
}
Expand Down Expand Up @@ -204,6 +211,23 @@ pub fn try_tokenize_call_expr_as_choice_cmp(
None
}

fn try_tokenize_tagged_tpl(ctx: &mut MacroCtx, tagged_tpl: &TaggedTpl) -> Option<Vec<MsgToken>> {
let tag = tagged_tpl.tag.as_ref();

let (is_t, _callee) = ctx.transform.is_lingui_t_call_expr(tag);
if is_t {
return Some(tokenize_tpl(ctx, &tagged_tpl.tpl));
}

if let Expr::Ident(ident) = tag {
if ctx.transform.is_define_message_ident(ident) {
return Some(tokenize_tpl(ctx, &tagged_tpl.tpl));
}
}

None
}

pub fn try_tokenize_expr(ctx: &mut MacroCtx, expr: &Expr) -> Option<Vec<MsgToken>> {
match expr {
// String Literal: "has # friend"
Expand All @@ -218,6 +242,9 @@ pub fn try_tokenize_expr(ctx: &mut MacroCtx, expr: &Expr) -> Option<Vec<MsgToken

// Call Expression: {one: plural(numArticles, {...})}
Expr::Call(expr) => try_tokenize_call_expr_as_choice_cmp(ctx, expr),

// Tagged template literal: msg`Hello ${name}` or t`Hello ${name}`
Expr::TaggedTpl(tagged_tpl) => try_tokenize_tagged_tpl(ctx, tagged_tpl),
_ => None,
}
}
Expand Down
10 changes: 10 additions & 0 deletions crates/lingui_macro/tests/js_define_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,13 @@ to!(
})
"#
);

to!(
should_inline_msg_tagged_tpl_in_message_prop,
r#"
import { defineMessage, msg } from '@lingui/macro';
const message = defineMessage({
message: msg`Hello ${name}`
})
"#
);
11 changes: 11 additions & 0 deletions crates/lingui_macro/tests/js_icu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,14 @@ to!(
});
"#
);

to!(
js_should_support_msg_in_options,
r#"
import { plural, msg } from '@lingui/macro'
const message = plural(count, {
one: msg`${name} has ${count} friend`,
other: msg`${name} has {count} friends`
})
"#
);
32 changes: 32 additions & 0 deletions crates/lingui_macro/tests/js_t.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,35 @@ to!(
t`Variable ${name satisfies string}`;
"#
);

to!(
js_t_nested_t_tagged_tpl,
r#"
import { t } from '@lingui/core/macro';
t`Outer ${t`Hello ${name}`} end`
"#
);

to!(
js_t_nested_msg_tagged_tpl,
r#"
import { t, msg } from '@lingui/core/macro';
t`Field ${msg`First Name`} is required`
"#
);

to!(
js_t_call_with_msg_tagged_tpl_message,
r#"
import { t, msg } from '@lingui/core/macro';
const message = t({ message: msg`Hello ${name}` })
"#
);

to!(
js_t_unknown_tagged_tpl_nested,
r#"
import { t } from '@lingui/core/macro';
t`Field ${aaa`First Name`} is required`
"#
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
source: crates/lingui_macro/tests/js_define_message.rs
---
import { defineMessage, msg } from '@lingui/macro';
const message = defineMessage({
message: msg`Hello ${name}`
})

↓ ↓ ↓ ↓ ↓ ↓

const message = /*i18n*/ {
id: "OVaF9k",
message: "Hello {name}",
values: {
name: name
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
source: crates/lingui_macro/tests/js_icu.rs
---
import { plural, msg } from '@lingui/macro'
const message = plural(count, {
one: msg`${name} has ${count} friend`,
other: msg`${name} has {count} friends`
})

↓ ↓ ↓ ↓ ↓ ↓

import { i18n as $_i18n } from "@lingui/core";
const message = $_i18n._(/*i18n*/ {
id: "tK7kAV",
message: "{count, plural, one {{name} has {count} friend} other {{name} has {count} friends}}",
values: {
count: count,
name: name
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
source: crates/lingui_macro/tests/js_t.rs
---
import { t, msg } from '@lingui/core/macro';
const message = t({ message: msg`Hello ${name}` })

↓ ↓ ↓ ↓ ↓ ↓

import { i18n as $_i18n } from "@lingui/core";
const message = $_i18n._(/*i18n*/ {
id: "OVaF9k",
message: "Hello {name}",
values: {
name: name
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
source: crates/lingui_macro/tests/js_t.rs
---
import { t, msg } from '@lingui/core/macro';
t`Field ${msg`First Name`} is required`

↓ ↓ ↓ ↓ ↓ ↓

import { i18n as $_i18n } from "@lingui/core";
$_i18n._(/*i18n*/ {
id: "-nmgWY",
message: "Field First Name is required"
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
source: crates/lingui_macro/tests/js_t.rs
---
import { t } from '@lingui/core/macro';
t`Outer ${t`Hello ${name}`} end`

↓ ↓ ↓ ↓ ↓ ↓

import { i18n as $_i18n } from "@lingui/core";
$_i18n._(/*i18n*/ {
id: "Nji4uQ",
message: "Outer Hello {name} end",
values: {
name: name
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
source: crates/lingui_macro/tests/js_t.rs
---
import { t } from '@lingui/core/macro';
t`Field ${aaa`First Name`} is required`

↓ ↓ ↓ ↓ ↓ ↓

import { i18n as $_i18n } from "@lingui/core";
$_i18n._(/*i18n*/ {
id: "O8dJMg",
message: "Field {0} is required",
values: {
0: aaa`First Name`
}
});
Loading