diff --git a/src/js_macro_folder.rs b/src/js_macro_folder.rs index 14ce7dc..4134edf 100644 --- a/src/js_macro_folder.rs +++ b/src/js_macro_folder.rs @@ -122,7 +122,7 @@ where "id", generate_message_id( &parsed.message_str, - &(context_val.unwrap_or_default()), + context_val.as_deref().unwrap_or_default(), ) .into(), )) @@ -137,6 +137,19 @@ where } } + if !self.ctx.options.strip_non_essential_fields { + if let Some(context) = context_val { + new_props.push(create_key_value_prop("context", context.into())); + } + + let comment = get_object_prop(&obj.props, "comment") + .and_then(|prop| get_expr_as_string(&prop.value)); + + if let Some(comment) = comment { + new_props.push(create_key_value_prop("comment", comment.into())); + } + } + let message_descriptor = Box::new(Expr::Object(ObjectLit { span, props: new_props, diff --git a/src/lib.rs b/src/lib.rs index 9ab17bf..68af411 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -117,6 +117,7 @@ where let parsed = MessageBuilder::parse(trans_visitor.tokens); let id_attr = get_jsx_attr(&el.opening, "id").and_then(|attr| attr.value.as_ref()); + let context_attr = get_jsx_attr(&el.opening, "context").and_then(|attr| attr.value.as_ref()); @@ -148,6 +149,14 @@ where } if !self.ctx.options.strip_non_essential_fields { + let comment_attr = get_jsx_attr(&el.opening, "comment") + .and_then(|attr| attr.value.as_ref()) + .and_then(get_jsx_attr_value_as_string); + + if let Some(comment) = comment_attr { + message_descriptor_props.push(create_key_value_prop("comment", comment.into())); + } + message_descriptor_props.push(create_key_value_prop("message", parsed.message)); if let Some(context_attr) = context_attr { diff --git a/tests/__swc_snapshots__/tests/js_define_message.rs/should_expand_macros.js b/tests/__swc_snapshots__/tests/js_define_message.rs/should_expand_macros.js index a33c6fe..f1c4fa9 100644 --- a/tests/__swc_snapshots__/tests/js_define_message.rs/should_expand_macros.js +++ b/tests/__swc_snapshots__/tests/js_define_message.rs/should_expand_macros.js @@ -3,5 +3,6 @@ const message = /*i18n*/ { message: "{count, plural, one {book} other {books}}", values: { count: count - } + }, + comment: "Description" }; diff --git a/tests/__swc_snapshots__/tests/js_define_message.rs/should_preserve_custom_id.js b/tests/__swc_snapshots__/tests/js_define_message.rs/should_preserve_custom_id.js index 0f75fec..2b2333e 100644 --- a/tests/__swc_snapshots__/tests/js_define_message.rs/should_preserve_custom_id.js +++ b/tests/__swc_snapshots__/tests/js_define_message.rs/should_preserve_custom_id.js @@ -1,4 +1,5 @@ const message = /*i18n*/ { id: "custom.id", - message: "Message" + message: "Message", + comment: "Description" }; diff --git a/tests/__swc_snapshots__/tests/js_define_message.rs/should_transform_define_message.js b/tests/__swc_snapshots__/tests/js_define_message.rs/should_transform_define_message.js index 18e5304..53f1fe9 100644 --- a/tests/__swc_snapshots__/tests/js_define_message.rs/should_transform_define_message.js +++ b/tests/__swc_snapshots__/tests/js_define_message.rs/should_transform_define_message.js @@ -1,8 +1,10 @@ const message1 = /*i18n*/ { id: "xDAtGP", - message: "Message" + message: "Message", + comment: "Description" }; const message2 = /*i18n*/ { id: "xDAtGP", - message: "Message" + message: "Message", + comment: "Description" }; diff --git a/tests/__swc_snapshots__/tests/js_t.rs/js_should_produce_all_fields_when_no_message_set.js b/tests/__swc_snapshots__/tests/js_t.rs/js_should_produce_all_fields_when_no_message_set.js new file mode 100644 index 0000000..ffb8690 --- /dev/null +++ b/tests/__swc_snapshots__/tests/js_t.rs/js_should_produce_all_fields_when_no_message_set.js @@ -0,0 +1,6 @@ +import { i18n as $_i18n } from "@lingui/core"; +const msg2 = $_i18n._(/*i18n*/ { + id: "msgId", + context: "My Context", + comment: "description for translators" +}); diff --git a/tests/__swc_snapshots__/tests/js_t.rs/js_should_produce_all_fields_without_strip_flag.js b/tests/__swc_snapshots__/tests/js_t.rs/js_should_produce_all_fields_without_strip_flag.js new file mode 100644 index 0000000..9a56ba0 --- /dev/null +++ b/tests/__swc_snapshots__/tests/js_t.rs/js_should_produce_all_fields_without_strip_flag.js @@ -0,0 +1,10 @@ +import { i18n as $_i18n } from "@lingui/core"; +const msg2 = $_i18n._(/*i18n*/ { + id: "msgId", + message: "Hello {name}", + values: { + name: name + }, + context: "My Context", + comment: "description for translators" +}); diff --git a/tests/__swc_snapshots__/tests/js_t.rs/js_support_message_descriptor_in_t_fn.js b/tests/__swc_snapshots__/tests/js_t.rs/js_support_message_descriptor_in_t_fn.js index 7c845f0..ed5007e 100644 --- a/tests/__swc_snapshots__/tests/js_t.rs/js_support_message_descriptor_in_t_fn.js +++ b/tests/__swc_snapshots__/tests/js_t.rs/js_support_message_descriptor_in_t_fn.js @@ -4,5 +4,6 @@ const msg = $_i18n._(/*i18n*/ { message: "Hello {name}", values: { name: name - } + }, + comment: "description for translators" }); diff --git a/tests/__swc_snapshots__/tests/js_t.rs/should_generate_diffrent_id_when_context_provided.js b/tests/__swc_snapshots__/tests/js_t.rs/should_generate_diffrent_id_when_context_provided.js index e1ad346..2359798 100644 --- a/tests/__swc_snapshots__/tests/js_t.rs/should_generate_diffrent_id_when_context_provided.js +++ b/tests/__swc_snapshots__/tests/js_t.rs/should_generate_diffrent_id_when_context_provided.js @@ -5,9 +5,11 @@ $_i18n._(/*i18n*/ { }); $_i18n._(/*i18n*/ { id: "7hFP9A", - message: "Ola" + message: "Ola", + context: "My Context" }); $_i18n._(/*i18n*/ { id: "7hFP9A", - message: "Ola" + message: "Ola", + context: "My Context" }); diff --git a/tests/__swc_snapshots__/tests/js_t.rs/support_id_and_comment_in_t_macro_as_call_expression.js b/tests/__swc_snapshots__/tests/js_t.rs/support_id_and_comment_in_t_macro_as_call_expression.js index 42d530f..6021bbd 100644 --- a/tests/__swc_snapshots__/tests/js_t.rs/support_id_and_comment_in_t_macro_as_call_expression.js +++ b/tests/__swc_snapshots__/tests/js_t.rs/support_id_and_comment_in_t_macro_as_call_expression.js @@ -4,5 +4,6 @@ const msg = $_i18n._(/*i18n*/ { message: "{val, plural, one {...} other {...}}", values: { val: val - } + }, + comment: "description for translators" }); diff --git a/tests/__swc_snapshots__/tests/jsx.rs/jsx_preserve_reserved_attrs.js b/tests/__swc_snapshots__/tests/jsx.rs/jsx_preserve_reserved_attrs.js index f83c234..4cffe76 100644 --- a/tests/__swc_snapshots__/tests/jsx.rs/jsx_preserve_reserved_attrs.js +++ b/tests/__swc_snapshots__/tests/jsx.rs/jsx_preserve_reserved_attrs.js @@ -1,6 +1,7 @@ import { Trans as Trans_ } from "@lingui/react"; const exp2 =
{p.translation}
} render={(v)=>v}/>; diff --git a/tests/__swc_snapshots__/tests/jsx_icu.rs/jsx_plural_preserve_reserved_attrs.js b/tests/__swc_snapshots__/tests/jsx_icu.rs/jsx_plural_preserve_reserved_attrs.js index 0e34fd4..7546026 100644 --- a/tests/__swc_snapshots__/tests/jsx_icu.rs/jsx_plural_preserve_reserved_attrs.js +++ b/tests/__swc_snapshots__/tests/jsx_icu.rs/jsx_plural_preserve_reserved_attrs.js @@ -4,6 +4,7 @@ import { Trans as Trans_ } from "@lingui/react"; values: { count: count }, + comment: "Translators Comment", message: "{count, plural, one {...} other {...}}", context: "Message Context" }} render={(v)=>v}/>; diff --git a/tests/__swc_snapshots__/tests/jsx_icu.rs/jsx_select_with_reserved_attrs.js b/tests/__swc_snapshots__/tests/jsx_icu.rs/jsx_select_with_reserved_attrs.js index a3be002..dab5412 100644 --- a/tests/__swc_snapshots__/tests/jsx_icu.rs/jsx_select_with_reserved_attrs.js +++ b/tests/__swc_snapshots__/tests/jsx_icu.rs/jsx_select_with_reserved_attrs.js @@ -7,6 +7,7 @@ import { Trans as Trans_ } from "@lingui/react"; components: { 0: }, + comment: "Translators Comment", message: "{count, select, male {He} female {She} other {<0>Other}}", context: "Message Context" }} render={(v)=>v}/>; diff --git a/tests/js_t.rs b/tests/js_t.rs index a2f39f0..6816127 100644 --- a/tests/js_t.rs +++ b/tests/js_t.rs @@ -163,6 +163,31 @@ to!( "# ); +to!( + js_should_produce_all_fields_without_strip_flag, + r#" + import { t } from '@lingui/core/macro' + const msg2 = t({ + message: `Hello ${name}`, + id: 'msgId', + comment: 'description for translators', + context: 'My Context', + }) + "# +); + +to!( + js_should_produce_all_fields_when_no_message_set, + r#" + import { t } from '@lingui/core/macro' + const msg2 = t({ + id: 'msgId', + comment: 'description for translators', + context: 'My Context', + }) + "# +); + to!( js_support_template_strings_in_t_macro_message_with_custom_i18n_instance, r#"