@goldenthumb/simple-i18n-csv-to-json
v0.2.2
Published
A utility that makes @goldenthumb/simple-i18n easy to use
Downloads
7
Maintainers
Readme
simple-i18n-csv-to-json
Install
npm install @goldenthumb/simple-i18n-csv-to-json
const toJson = require('@goldenthumb/simple-i18n-csv-to-json');
Usage
sample csv
| | ko | en | ja | zh_CN | zh_TW | |-----|----|----|------|-------|-------| | yes | 예 | Yes | はい | 是的 | 是的 | | No | | No | いいえ | 没有 | 沒有 | ,ko,en,ja,zh_CN,zh_TW yes,예,Yes,はい,是的,是的 no,,No,いいえ,没有,沒有
CLI
Usage: i18n-csv2json <input file> [<output file>] [--allowEmpty]
$ i18n-csv2json ./sample.csv ./output.json
Basic
$ i18n-csv2json ./sample.csv ./output.json
const toJson = require('@goldenthumb/simple-i18n-csv-to-json');
(async () => {
try {
const result = await toJson('./sample.csv');
console.log(result);
> result
{
ko: {
yes: '예'
},
en: {
yes: 'Yes',
no: 'No'
},
ja: {
yes: 'はい',
no: 'いいえ'
},
zh_CN: {
yes: '是的',
no: '没有'
},
zh_TW: {
yes: '是的',
no: '沒有'
}
};
} catch(e) {
console.log(e);
}
})();
Allow Empty String
$ i18n-csv2json ./sample.csv ./output.json --allowEmpty
const toJson = require('@goldenthumb/simple-i18n-csv-to-json');
(async () => {
try {
const result = await toJson('./sample.csv', { allowEmpty: true });
console.log(result);
> result
{
ko: {
yes: '예',
no: ''
},
en: {
yes: 'Yes',
no: 'No'
},
ja: {
yes: 'はい',
no: 'いいえ'
},
zh_CN: {
yes: '是的',
no: '没有'
},
zh_TW: {
yes: '是的',
no: '沒有'
}
};
} catch(e) {
console.log(e);
}
})();
Using by with @goldenthumb/simple
npm install @goldenthumb/simple-i18n @goldenthumb/simple-i18n-csv-to-json
const SimpleI18n = require('@goldenthumb/simple-i18n');
const toJson = require('@goldenthumb/simple-i18n-csv-to-json');
(async () => {
try {
const messages = await toJson('./sample.csv');
const i18n = new SimpleI18n({
defaultLocale: ['en'],
locale: 'en',
messages
});
i18n.message('yes')
> Yes.
i18n.switchLang('ja');
i18n.message('no');
> いいえ
} catch(e) {
console.log(e);
}
})();
License
MIT