o18n 是一個能幫你組織化語言檔的套件. 能決定語系的顯示順序,還能切割語系檔到各個元件中,以避免語言包非常大一坨。 因為分每個元件可能會有自己的語系檔,所以就算重複Key值,也沒有關係。
import O18N from 'o18n';
const lang = new O18N({
zh: { hello: '哈囉'},
en: { hello: 'hello'},
});
/* Default is the first language */
console.log(lang.hello); // 哈囉
lang.locale = 'en';
console.log(lang.hello); // hello
- 簡單易用: 如果你了 create-react-app 這傢伙。只要 import o18n,然後就能翹腳開始。
- 語系順序: 使用當前語系沒有某個key值,它會以第二順位語系的key替代,如果第二順位也沒有該key,那它會找第三順位,直到永遠。
- 組件語系: 為了避免單一語系檔過大,你可以分割你的語系檔到各個元件裡面。也因為如此,所以不同元件之間就算有重複的key也無所謂。
如果你對 webpack 不熟但你又很想很快使用它,那 create-react-app 是一個建置環境的好選擇。
npm i -g create-react-app
npm i --save o18n
yarn add o18n
- Example with create-react-app
- Example order of display for different languages
- Example Use o18n between different components
馬上看 線上示範
- sexyoung
MIT License. 請直接看 LICENSE 這個檔.