FallenMax / a18n

Automated I18n solution for JavaScript/TypeScript/React

Home Page:https://www.npmjs.com/package/a18n

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Better handling of JSX text?

FallenMax opened this issue · comments

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:

  1. breaks a complete sentence in JSX children into multiple texts, and is not ideal.
  2. does not support scenario like <span>aaa <strong>bbb</strong> ccc</span>

I think it's not worth implement, reasons:

  1. 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}
  2. 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
  3. 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.

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 :)