Repository navigation
Replies: 3 comments
|
Too much magic, isn't it? Also it would be quite hard to determine boundaries of the component reliably. const MyComponent = () => {
const myFragment = () => <div>{t`username`}</div> // simple hoisting to a closest function would hoist this to the myFragment closure
<div>{t`hello`} {myFragment()}</div>
}In my opinion - good design should make wrong patterns to be hard to write and good pattern to be easy to write. With the global // non react component function
const getTitle = () => {
return t`hello` // this would use global and breaks semantics?
} |
|
Thanks for the proposal! I agree with Timofei here. The |
|
@andrii-bodnar I'm fully aware of the hook, and it great. However we have a 4+ year old codebase, and I rather not go and do a massive find and replace (or spend tokens on it) when a modified macro can handle it as well. @timofei-iatsenko Nah, not to much magic. I come from Ruby-On-Rails, there is no such thing as to much magic! :D You do bring up some valid concerns, that would have to be well designed around. So for the second example the macro would throw an error. It's not in a hook, or a component, so it must crash. The first example, is a bit more tough. Technically the This should work though: const MyComponent = () => {
const MyFragment = () => <div>{t`username`}</div>
return (<div>{t`hello`} <MyFragment /></div>)
}
// ↓ ↓ ↓ ↓ ↓ ↓
const MyComponent = () => {
const { _ } = useLingui();
const MyFragment = () => {
const { _ } = useLingui();
return (<div>{_(...)}</div>);
}
return (<div>{_(...)} <MyFragment /></div>)
} |
Uh oh!
There was an error while loading. Please reload this page.
Motivation
I'm a big fan of the core macros. The brevity of a
t`Hello`macro can't be beat.But functionally these are tied to a global value. In most cases this works great but can become an issue in larger applications. For our application there are 2 reasons why we don't always want to use a global instance:
The above 2 things can be handled cleanly with
I18nContextanduseLingui.I would like to be able to eat my cake and have it as well.
Solution/Idea
This new macro would do the following:
When
tis already in scope, the existing value would be used.I see two way of doing this.
runtimeConfigModule@lingui/react/macroThis moves quite a bit of complexity to the macro, but I think this should be perfectly possible.
All reactions