UNPKG

738 BJavaScriptView Raw
1const reg = /<i18n[^>]*>([\s\S]*?)<\/i18n>/
2const fs = require('fs')
3const yamlReader = require('js-yaml')
4
5// @todo add global cache
6
7// @todo try parse
8function get (code, isFile = false) {
9 let content = code
10 if (isFile) {
11 content = fs.readFileSync(code, 'utf-8')
12 }
13 const results = content.match(/<i18n[^>]*>([\s\S]*?)<\/i18n>/)
14 try {
15 const local = yamlReader.safeLoad(results[1])
16 return local
17 } catch (e) {
18 return {}
19 }
20}
21
22function getWithLocale ({ code = '', isFile = false, locale = ''}) {
23 const rs = get(code, isFile)
24 let _rs = {}
25 for (let i in rs) {
26 _rs[i] = typeof rs[i][locale] === 'undefined' ? i : rs[i][locale]
27 }
28 return _rs
29}
30
31exports.get = get
32exports.getWithLocale = getWithLocale