simplest-i18n
Advanced tools
Comparing version 0.1.0 to 1.0.0
@@ -5,3 +5,3 @@ { | ||
"author": "Ken Berkeley <kenberkeley@foxmail.com>", | ||
"version": "0.1.0", | ||
"version": "1.0.0", | ||
"main": "i18n.min.js", | ||
@@ -8,0 +8,0 @@ "scripts": { |
112
README.md
@@ -5,11 +5,17 @@ # The Simplest Universal i18n Solution | ||
[![npm download][npm-dl-img]][npm-url] | ||
[![build][build-img]][build-url] | ||
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com) | ||
## $ Features | ||
## § Preface | ||
In most cases, **internationalization** is actually translating your website into English | ||
Which means that you might not need a cumbersome framework to implement this | ||
And this tiny repo is for you! | ||
## § Features | ||
* Support browsers and Node.js | ||
* No dependencies (source code < 0.5KB) | ||
* No dependencies (compressed [source code](./i18n.js) < 0.5KB, extremely simple and comprehensible) | ||
* Does not rely on any framework (React / Vue / Angular / ...) or any bundler (Webpack / Parcel / Rollup / ...) | ||
* Extremely simple and flexible | ||
* No tedious and verbose documentation (Just this README) | ||
## $ Installation | ||
## § Installation | ||
### â NPM | ||
@@ -21,3 +27,3 @@ `npm i simplest-i18n -S` | ||
## $ Usage | ||
## § Usage | ||
@@ -52,3 +58,3 @@ ```js | ||
``` | ||
>_ git clone https://github.com/kenberkeley/simplest-i18n | ||
>_ git clone https://github.com/kenberkeley/simplest-i18n.git | ||
>_ npm i | ||
@@ -58,4 +64,98 @@ >_ npm run react (or npm run vue) | ||
## § Merits | ||
### â Keep in context | ||
```js | ||
// this demonstrates how most popular i18n frameworks do | ||
const messages = { | ||
en: { | ||
greeting: 'Hello {name}, long time no see' | ||
}, | ||
cn: { | ||
greeting: 'ä˝ ĺĽ˝ďź{name}ďźĺĽ˝äš ä¸č§äş' | ||
}, | ||
ja: { | ||
greeting: 'ăăăŤăĄăŻă{name}ăéˇăćéăŻčŚăŚăăŞă' | ||
} | ||
} | ||
***************************************************************** | ||
// in another file (lose direct sight of the original translations) | ||
render () { | ||
return ( | ||
<h1>{ | ||
format('greeting', { name: this.state.name }) | ||
}</h1> | ||
) | ||
} | ||
``` | ||
```js | ||
// this is how we do with ES6 template literals | ||
render () { | ||
const { name } = this.state | ||
return ( | ||
<h1>{ | ||
t( | ||
`ä˝ ĺĽ˝ďź${name}ďźĺĽ˝äš ä¸č§`, | ||
`Hello ${name}, long time no see`, | ||
`ăăăŤăĄăŻă${name}ăéˇăćéăŻčŚăŚăăŞă` | ||
) | ||
}</h1> | ||
) | ||
} | ||
``` | ||
From now on, naming things and duplicate keys would not bother you anymore | ||
(the *key* is actually the original text written in your mother tongue) | ||
Before that, you might have to use a kinda nonsense `module1.page1.greeting` (namespace) to avoid these problems | ||
### â Flexible | ||
How do we solve the pluralization problem? | ||
* Method 0: Simple and crude: appending `(s) / (es)` directly | ||
```js | ||
render () { | ||
const { num } = this.state | ||
return ( | ||
<span>{ | ||
t( | ||
`ćć ${num} 个čšć`, | ||
`I have ${num} apple(s)` | ||
) | ||
}</span> | ||
) | ||
} | ||
``` | ||
* Method 1: Write your own helper function | ||
```js | ||
/** | ||
* @param {String} nouns | ||
* @param {String} num | ||
* @return {String} | ||
* e.g. | ||
* pluralize('apple|apples', 3) => '3 apples' | ||
* pluralize('man|men', 1) => '1 man' | ||
*/ | ||
export default function pluralize(nouns, num) { | ||
return `${num} ${nouns.split('|')[+!(num === 1)]}` | ||
} | ||
``` | ||
* Method 2: Search `plural` in [npmjs.com](https://www.npmjs.com) and pick one | ||
> You can control everything in the project! No blackboxes! All functions are pure, simple and composable! | ||
## § Tips | ||
* If you are using Webpack and tired of importing `t` everywhere, try [ProvidePlugin](https://webpack.js.org/plugins/provide-plugin/) instead (`window.t = t` is ok as you like it) | ||
[npm-url]: https://www.npmjs.com/package/simplest-i18n | ||
[npm-v-img]: http://img.shields.io/npm/v/simplest-i18n.svg | ||
[npm-dl-img]: http://img.shields.io/npm/dm/simplest-i18n.svg | ||
[build-img]: https://travis-ci.org/kenberkeley/simplest-i18n.svg?branch=master | ||
[build-url]: https://travis-ci.org/kenberkeley/simplest-i18n |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
10345
1
158