Better handling of JSX text?
FallenMax opened this issue · comments
FallenMax commented
Proposal
Maybe we should wrap JSX Text as a whole?
(Inspired by ttag)
let desc = <strong>nice</strong>
let vdom = <div>Today is a {desc} day <div>
// should be wrapped as:
let desc = <strong>nice</strong>
let vdom = <div>{a18n.x`Today is a {desc} day`}<div> // a18n.x returns array instead of string
// and extracted to key: "Today is a %1 day"
Background
Currently we only wrap StringLiteral and TemplateLiterals inside JSX Element children, this:
- breaks a complete sentence in JSX children into multiple texts, and is not ideal.
- does not support scenario like
<span>aaa <strong>bbb</strong> ccc</span>
FallenMax commented
I think it's not worth implement, reasons:
- It creates extra noise for JSX elements like:
<div>输入: <input .../></div>
- current a18n will generate:
<div>{a18n("输入: ") <input .../></div>
and{"输入:": null }
- this proposal will generate:
<div>{a18n.x`输入: <input .../>`}</div>
and{"输入: %s": null}
- current a18n will generate:
- Not improving situations like
<span>aaa <strong>bbb</strong> ccc</span>
- this proposal will generate templates "aaa %s ccc" and "bbb", still does not form a complete sentence
- More complexity in wrap/extract/purge logic, and some scenarios are hard to get it right (nested/parallel a18n calls inside a JSX element)
Closing.
FallenMax commented
Well, I think an a18n.x
method that returns array is still useful for scenarios like rich text translation. So we added it in 1.5.1 anyway :)