udf-i18n

1.1.0 • Public • Published

udf-i18n

A univeral i18n library.

Install

$ npm i udf-i18n

Usage

How to use

Initialize

  • Synchronized
import { init } from 'udf-i18n';

const langData = {
	"en-US": {
		"version": 1,
		"texts": {
			"marco": "Polo",
			"mail": "You have recieved ${1} mails"
			"foo": {
				"bar": "baz"
			}
		}
	},
	"ja-JP": {
		"version": 1,
		"texts": {
			"marco": "ポロ",
			"mail": "メールが${1}通を受け取りました"
			"foo": {
				"bar": "バズ",
			}
		}
	}
};

init('ja-JP', langData);
  • Asynchronized
import T, { init } from 'udf-i18n';

const getData = async lang => await axios.get(`/lang/${lang}.json`);
// If the data is a function that returns a promise, you need to await it
await init('ja-JP', getData);

Use in code

console.log(T`marco`);		// ポロ
console.log(T`foo.bar`);	// バズ

Use in HTML:

<!-- <span>ポロ</span>  -->
<span>@{marco}</span>

<!-- use variables -->
<!-- <span>メールが3通を受け取りました</span>  -->
<span>@{mail:3}</span>

<!-- <span title="バズ">Hello, world!</span>  -->
<span title="@{foo.bar}">Hello, world!</span>

Package Sidebar

Install

npm i udf-i18n

Weekly Downloads

1

Version

1.1.0

License

MIT

Unpacked Size

4.66 kB

Total Files

3

Last publish

Collaborators

  • undefineded