Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@tiny-i18n/react-live

imcuttle47MIT2.0.11TypeScript support: included

The magical effect making tiny-i18n could be used easily and edit live in react.

tiny-i18n, react, edit-live, live, internationalization, i18n

readme

@tiny-i18n/react-live

NPM version NPM Downloads

The magical effect making tiny-i18n could be used easily and edit live in react.

A demo and video created by react and using edit-live.

Usage

npm install @tiny-i18n/react-live tiny-i18n
  • entry.js (the frontend's start point)
import * as React from 'react'
import * as ReactDOM from 'react-dom'
import '@tiny-i18n/react-live/lib/style.css'
import { withTinyI18n, I18nProvider } from '@rcp/use.i18ncontext'

import { createReactI18nLive } from '@tiny-i18n/react-live'

import { createIsolateI18n } from 'tiny-i18n'

const { tinyI18n, transaction, createElement, configure } = createReactI18nLive({tinyI18n: createIsolateI18n()})

configure({
  enabled: true
})

const { setDictionary, getLanguages, getCurrentLanguage, i18n, getDictionary } = tinyI18n

let zhDict = require('./dict/zh-CN')
let enDict = require('./dict/en-US')

transaction.setConfig({
  fetchWord(data) {
    return fetch(`/i18n/word` , {
      method: 'POST',
      headers: {
        'content-type': 'application/json'
      },
      body: JSON.stringify(data)
    })
      .then(res => res.json())
      .then(json => {
        return json.data
      })
    // word
  },
  fetchUpdate({ lang, key, value }) {
    return fetch('/i18n/update', {
      method: 'POST',
      headers: {
        'content-type': 'application/json'
      },
      body: JSON.stringify({ lang, key, value })
    })
      .then(res => res.json())
      .then(json => {
        console.log(json)
      })
  }
})
transaction.on('error', e => {
  console.error('error', e)
})

setDictionary(zhDict, 'zh-CN')

setDictionary(enDict, 'en-US')

@withTinyI18n
class View extends React.Component {
  changeLanguage = lang => {
    this.props.tinyI18n.setLanguage(lang)
  }
  componentWillMount() {
    this.changeLanguage(getLanguages()[0])
  }

  render() {
    return (
      <div>
        <h3>Current Language: {getCurrentLanguage()}</h3>
        {getLanguages().map(lang => (
          <button key={lang} onClick={this.changeLanguage.bind(this, lang)}>
            Change Language to {lang}
          </button>
        ))}
        <div>Im not an i18n text.</div>
        <div>{i18n('hi') + ',' + i18n('cong')}</div>
        <div title={'abc' + i18n('hi') + i18n('cong')}>
          {i18n('hi')},{i18n('cong', i18n('cong', 'hjhjhj'))}
        </div>
        <div title={i18n('say.hi', 'hah')}>{i18n('say.hi', i18n('cong'))}</div>

        <div title={i18n('hi') + ',' + i18n('tpl.name', i18n('cong'))}>
          {'Hover me! [translated words in title attribute] (The nested and concat case)'}
        </div>
        {/*TODO BUG*/
        /*<div title={i18n('hi') + ',' + i18n('say.hi', i18n('tpl.name', i18n('cong')))}>{'Change my title attribute (The nested and concat case)'}</div>*/}
      </div>
    )
  }
}

ReactDOM.render(
  <I18nProvider tinyI18n={tinyI18n}>
    <View />
  </I18nProvider>,
  window.root
)

Examples

# The Demo in `examples/` which could edit i18n word in live.
npm run example

changelog

Change Log

All notable changes to this project will be documented in this file. See Conventional Commits for commit guidelines.

2.0.11 (2020-07-05)

Note: Version bump only for package @tiny-i18n/react-live

2.0.10 (2020-07-04)

Note: Version bump only for package @tiny-i18n/react-live

2.0.9 (2020-07-04)

Note: Version bump only for package @tiny-i18n/react-live

2.0.8 (2020-07-04)

Note: Version bump only for package @tiny-i18n/react-live

2.0.7 (2020-07-03)

Bug Fixes

1.1.2 (2018-08-27)

1.1.1 (2018-08-27)

1.1.0 (2018-07-01)

Performance Improvements

  • tiny-i18n: support createIsolateI18n function (988d7ee)

1.0.3 (2018-06-23)

1.0.2 (2018-06-22)

Bug Fixes

  • react-live: wrappedSetLanguage make some side effect (b0606db)
  • prepublishOnly to prepare (3a17383)

1.0.1 (2018-06-22)

2.0.6 (2020-07-03)

Bug Fixes

2.0.5 (2020-07-03)

Note: Version bump only for package @tiny-i18n/react-live

2.0.4 (2020-07-03)

Note: Version bump only for package @tiny-i18n/react-live

2.0.3 (2020-07-03)

Note: Version bump only for package @tiny-i18n/react-live

2.0.2 (2020-07-03)

Note: Version bump only for package @tiny-i18n/react-live

2.0.1 (2020-07-03)

2.0.0 (2020-07-03)

1.1.2 (2018-08-27)

1.1.1 (2018-08-27)

1.1.0 (2018-07-01)

Performance Improvements

  • tiny-i18n: support createIsolateI18n function (988d7ee)

1.0.3 (2018-06-23)

1.0.2 (2018-06-22)

Bug Fixes

  • react-live: wrappedSetLanguage make some side effect (b0606db)
  • prepublishOnly to prepare (3a17383)

1.0.1 (2018-06-22)

1.1.2 (2018-08-27)

Note: Version bump only for package @tiny-i18n/react-live