项目作者: Vovan-VE

项目描述 :
Render placeholders from text with custom React components
高级语言: JavaScript
项目地址: git://github.com/Vovan-VE/react-text-subst.git
创建时间: 2018-08-10T00:55:24Z
项目社区:https://github.com/Vovan-VE/react-text-subst

开源协议:MIT License

下载


react-text-subst

NPM latest
NPM next
Build Status

How will you add i18n support in following cases?

  1. <span>Hello <span>{username}</span>!</span>
  1. <span>
  2. By clicking “Sign up”, you agree
  3. to our <a href="...">terms of service</a> and...
  4. </span>

Sure, you just can split texts in parts, but that parts would be translated not properly.

Here is a solution:

  1. import TextSubst from 'react-text-subst';
  2. return (
  3. <TextSubst user={<span>{username}</span>}>
  4. Hello @[user]!
  5. </TextSubst>
  6. );
  1. <TextSubst link={({children}) => <a href="...">{children}</a>}>
  2. By clicking “Sign up”, you agree to our @[link[terms of service]] and...
  3. </TextSubst>

Now you can simply add i18n to patterns and translate them.

Install

  1. npm i --save react-text-subst

Pattern syntax

Pattern text has following special tokens:

  • @[foo] - inline node. Render corresponding standalone value, element or component.
  • @[foo[ - start block node. Render a component wrapped around block’s content.
  • ]] - end corresponding block node.

Notice: Since React has special props key and ref, you shouldn’t use such names
in pattern.

Nodes

So, there are few types of nodes. Nodes of any type can repeat or share its’ names - everything
is up to you.

Inline @[foo]

Inline node can render anything what React can render as a node, or component.
The last will receive following property:

  • name - corresponding node name (foo in example).
  1. <TextSubst
  2. bar={something}
  3. baz={<b>{something}</b>}
  4. qux={({name}) => <b>value of {name}</b>}
  5. >
  6. foo @[bar] lol @[baz]@[qux].
  7. </TextSubst>
  8. // <>
  9. // foo {something} lol <b>{something}</b><b>value of qux</b>
  10. // </>

Block @[foo[...]]

Block node can only render a component. It will receive following properties:

  • name - corresponding node name (foo in example).
  • children - rendered content of the block.

Blocks can be nested.

  1. <TextSubst
  2. b={({children}) => <b>{children}</b>}
  3. i={({children}) => <i>{children}</i>}
  4. >
  5. lorem @[b[ipsum @[i[dolor]] sit]] amet @[i[consectep@[b[ture]]]]
  6. </TextSubst>
  7. // <>
  8. // lorem <b>ipsum <i>dolor</i> sit</b> amet <i>consectep<b>ture</b></i>
  9. // </>
  1. <TextSubst foo={({children}) => <span>{children}</span>}>
  2. lorem @[foo[ipsum @[foo[dolor]] sit]] amet
  3. </TextSubst>
  4. // <>
  5. // lorem <span>ipsum <span>dolor</span> sit</span> amet
  6. // </>
  1. const Link = ({name, children}) => <a href={URL[name]}>{children}</a>;
  2. return (
  3. <TextSubst foo={Link} bar={Link}>
  4. lorem @[foo[ipsum]] dolor @[bar[sit]] amet
  5. </TextSubst>
  6. );
  7. // <>
  8. // lorem <a href={URL.foo}>ipsum</a> dolor <a href={URL.bar}>sit</a> amet
  9. // </>

API

Properties

  • children - pattern string to render, only single string is acceptable:

    1. <TextSubst>Lorem ipsum dolor</TextSubst>
    2. <TextSubst>{'Lorem ipsum dolor'}</TextSubst>
    3. <TextSubst>{i18n('Lorem ipsum dolor')}</TextSubst>
  • ... rest - value, element or component to render nodes with corresponding
    name.

    Notice: You cannot use React specific props like key or ref for values.

License

This package is under MIT License