1 | const reg = /<i18n[^>]*>([\s\S]*?)<\/i18n>/
|
2 | const fs = require('fs')
|
3 | const yamlReader = require('js-yaml')
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | function 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 |
|
22 | function 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 |
|
31 | exports.get = get
|
32 | exports.getWithLocale = getWithLocale
|