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
31 changes: 22 additions & 9 deletions crates/lingui_macro/src/js_macro_folder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,31 @@ where
args: Vec<ExprOrSpread>,
span: Span,
) -> CallExpr {
CallExpr {
span,
callee: Expr::Member(MemberExpr {
let callee = if let Some(obj) = callee_obj {
// `t(customI18n)` — call `._` on the passed i18n object
Expr::Member(MemberExpr {
span: DUMMY_SP,
obj: callee_obj.unwrap_or_else(|| {
self.ctx.should_add_18n_import = true;

Box::new(self.ctx.runtime_idents.i18n.clone().into())
}),
obj,
prop: MemberProp::Ident(IdentName::new("_".into(), DUMMY_SP)),
})
.as_callee(),
.as_callee()
} else if let Some(t_ident) = &self.ctx.runtime_idents.use_lingui_t {
// `_` destructured from `useLingui()` is the translator itself
Callee::Expr(Box::new(Expr::Ident(t_ident.clone())))
} else {
self.ctx.should_add_18n_import = true;

Expr::Member(MemberExpr {
span: DUMMY_SP,
obj: Box::new(self.ctx.runtime_idents.i18n.clone().into()),
prop: MemberProp::Ident(IdentName::new("_".into(), DUMMY_SP)),
})
.as_callee()
};

CallExpr {
span,
callee,
args,
type_args: None,
ctxt: SyntaxContext::empty(),
Expand Down
56 changes: 25 additions & 31 deletions crates/lingui_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,37 +287,31 @@ where
let mut new_props: Vec<ObjectPatProp> = obj_pat
.props
.into_iter()
.map(|prop| {
get_local_ident_from_object_pat_prop(&prop, "t")
.map(|ident| {
ctx.register_reference(
&"t".into(),
&ident.to_id(),
);

let new_i18n_ident =
private_ident!("$__i18n");

ident_replacer = Some(IdentReplacer {
from: ident.to_id(),
to: underscore_ident.clone(),
});

ctx.runtime_idents.i18n =
new_i18n_ident.clone();

ObjectPatProp::KeyValue(
KeyValuePatProp {
value: Box::new(Pat::Ident(
new_i18n_ident.into(),
)),
key: PropName::Ident(
quote_ident!("i18n"),
),
},
)
})
.unwrap_or(prop)
.filter_map(|prop| {
if let Some(ident) =
get_local_ident_from_object_pat_prop(
&prop, "t",
)
{
ctx.register_reference(
&"t".into(),
&ident.to_id(),
);

ident_replacer = Some(IdentReplacer {
from: ident.to_id(),
to: underscore_ident.clone(),
});

ctx.runtime_idents.use_lingui_t =
Some(underscore_ident.clone());

// `t` is dropped: the `_: $__` binding
// pushed below is called directly
return None;
}

Some(prop)
})
.collect();

Expand Down
5 changes: 5 additions & 0 deletions crates/lingui_macro/src/macro_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,10 @@ pub struct RuntimeIdents {
pub i18n: Ident,
pub trans: IdentName,
pub use_lingui: IdentName,
/// `_` translator destructured from `useLingui()`.
/// When set (inside a `useLingui` scope), `t`-family calls
/// invoke it directly instead of `<i18n>._(...)`.
pub use_lingui_t: Option<Ident>,
}

impl Default for RuntimeIdents {
Expand All @@ -338,6 +342,7 @@ impl Default for RuntimeIdents {
i18n: quote_ident!("$_i18n").into(),
trans: quote_ident!("Trans_"),
use_lingui: quote_ident!("$_useLingui"),
use_lingui_t: None,
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ function App() {

import { useLingui as $_useLingui } from "@lingui/react";
function App() {
const { i18n: $__i18n, _: $__ } = $_useLingui();
/* lingui-set context="first" */ const msg1 = $__i18n._(/*i18n*/ {
const { _: $__ } = $_useLingui();
/* lingui-set context="first" */ const msg1 = $__(/*i18n*/ {
id: "VUeJYD",
message: "Hello",
context: "first"
});
/* lingui-set context="second" */ const msg2 = $__i18n._(/*i18n*/ {
/* lingui-set context="second" */ const msg2 = $__(/*i18n*/ {
id: "ovKD_8",
message: "World",
context: "second"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ function App() {

import { useLingui as $_useLingui } from "@lingui/react";
function App() {
const { i18n: $__i18n, _: $__ } = $_useLingui();
/* lingui-set context="my context" */ return $__i18n._(/*i18n*/ {
const { _: $__ } = $_useLingui();
/* lingui-set context="my context" */ return $__(/*i18n*/ {
id: "WMU_Op",
message: "Hello",
context: "my context"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ const bla1 = ()=>{
console.log();
};
function bla() {
const { i18n: $__i18n, i18n, _: $__ } = $_useLingui();
$__i18n._(/*i18n*/ {
const { i18n, _: $__ } = $_useLingui();
$__(/*i18n*/ {
id: "EsCV2T",
message: "Refresh inbox"
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ function MyComponent() {

import { useLingui as $_useLingui } from "@lingui/react";
function MyComponent() {
const { i18n: $__i18n, _: $__ } = $_useLingui();
const a = $__i18n._(/*i18n*/ {
const { _: $__ } = $_useLingui();
const a = $__(/*i18n*/ {
id: "xeiujy",
message: "Text"
});
Expand All @@ -34,7 +34,7 @@ function MyComponent() {
}
{
// here is child scope which should be processed, since 't' relates to outer scope
$__i18n._(/*i18n*/ {
$__(/*i18n*/ {
id: "xeiujy",
message: "Text"
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ function MyComponent() {

import { useLingui as $_useLingui } from "@lingui/react";
function MyComponent() {
const { i18n: $__i18n, _: $__ } = $_useLingui();
const a = $__i18n._(/*i18n*/ {
const { _: $__ } = $_useLingui();
const a = $__(/*i18n*/ {
id: "hJRCh6",
message: "Text {0, plural, offset:1 =0 {No books} =1 {1 book} other {# books}}",
values: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ const MyComponent = () => {

import { useLingui as $_useLingui } from "@lingui/react";
const MyComponent = ()=>{
const { i18n: $__i18n, _: $__ } = $_useLingui();
const a = $__i18n._(/*i18n*/ {
const { _: $__ } = $_useLingui();
const a = $__(/*i18n*/ {
id: "hJRCh6",
message: "Text {0, plural, offset:1 =0 {No books} =1 {1 book} other {# books}}",
values: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ function MyComponent() {

import { useLingui as $_useLingui } from "@lingui/react";
function MyComponent() {
const { i18n: $__i18n, _: $__ } = $_useLingui();
const a = useMemo(()=>$__i18n._(/*i18n*/ {
const { _: $__ } = $_useLingui();
const a = useMemo(()=>$__(/*i18n*/ {
id: "xeiujy",
message: "Text"
}), [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ function MyComponent() {

import { useLingui as $_useLingui } from "@lingui/react";
function MyComponent() {
const { i18n: $__i18n, _: $__ } = $_useLingui();
const a = $__i18n._(/*i18n*/ {
const { _: $__ } = $_useLingui();
const a = $__(/*i18n*/ {
id: "xeiujy",
message: "Text"
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ const MyComponent = () => {

import { useLingui as $_useLingui } from "@lingui/react";
const MyComponent = ()=>{
const { i18n: $__i18n, _: $__ } = $_useLingui();
const a = $__i18n._(/*i18n*/ {
const { _: $__ } = $_useLingui();
const a = $__(/*i18n*/ {
id: "xeiujy",
message: "Text"
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
source: crates/lingui_macro/tests/use_lingui.rs
---
import { useLingui } from '@lingui/react/macro';

function MyComponent() {
const { t, i18n } = useLingui();
const a = t`Text`;
const b = t(i18n)({ message: "With custom i18n" });
}

↓ ↓ ↓ ↓ ↓ ↓

import { useLingui as $_useLingui } from "@lingui/react";
function MyComponent() {
const { i18n, _: $__ } = $_useLingui();
const a = $__(/*i18n*/ {
id: "xeiujy",
message: "Text"
});
const b = i18n._(/*i18n*/ {
id: "f0HTH9",
message: "With custom i18n"
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import { useLingui } from '@lingui/react';
function MyComponent() {
const { _ } = useLingui();
console.log(_);
const { i18n: $__i18n, _: $__ } = $_useLingui();
const a = $__i18n._(/*i18n*/ {
const { _: $__ } = $_useLingui();
const a = $__(/*i18n*/ {
id: "xeiujy",
message: "Text"
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ function MyComponent2() {

import { useLingui as $_useLingui } from "@lingui/react";
function MyComponent() {
const { i18n: $__i18n, _: $__ } = $_useLingui();
const a = $__i18n._(/*i18n*/ {
const { _: $__ } = $_useLingui();
const a = $__(/*i18n*/ {
id: "xeiujy",
message: "Text"
});
}
function MyComponent2() {
const { i18n: $__i18n, _: $__ } = $_useLingui();
const b = $__i18n._(/*i18n*/ {
const { _: $__ } = $_useLingui();
const b = $__(/*i18n*/ {
id: "xeiujy",
message: "Text"
});
Expand Down
13 changes: 13 additions & 0 deletions crates/lingui_macro/tests/use_lingui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,16 @@ const MyComponent = () => {
}
"#
);

to!(
work_with_custom_i18n_instance,
r#"
import { useLingui } from '@lingui/react/macro';

function MyComponent() {
const { t, i18n } = useLingui();
const a = t`Text`;
const b = t(i18n)({ message: "With custom i18n" });
}
"#
);
4 changes: 2 additions & 2 deletions packages/native-tools/__test__/transform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ const App = ({ name }: { name: string }) => {
message: "Hello"
};
const App = ({ name })=>{
const { i18n: $__i18n, _: $__ } = $_useLingui();
return <div title={$__i18n._(/*i18n*/ {
const { _: $__ } = $_useLingui();
return <div title={$__(/*i18n*/ {
id: "MHrjPM",
message: "Title"
})}><Trans_ {.../*i18n*/ {
Expand Down
Loading