1 | const getLocales = require('./get-locales')
|
2 |
|
3 | let l
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | module.exports = (...args) => {
|
10 | if (typeof l === 'undefined') l = getLocales()
|
11 |
|
12 | let key = ''
|
13 | let str
|
14 | let options = {}
|
15 | const keys = []
|
16 |
|
17 | args.forEach((value, index) => {
|
18 | if (index == args.length - 1 && typeof value === 'object') {
|
19 | options = value
|
20 | return
|
21 | }
|
22 | if (typeof value === 'string' && value.includes('.')) {
|
23 | value.split('.').forEach(value => keys.push(value))
|
24 | return
|
25 | }
|
26 | keys.push(value)
|
27 | })
|
28 |
|
29 | const length = keys.length
|
30 |
|
31 | if (typeof keys[0] === 'object') {
|
32 | key = keys[0]
|
33 | for (let i = 1; i < length; i++) {
|
34 | if (typeof key[keys[i]] !== 'undefined')
|
35 | key = key[keys[i]]
|
36 | }
|
37 | if (typeof key === 'object') key = keys[length - 1]
|
38 | } else {
|
39 | for (let i = 0; i < length; i++) {
|
40 | key += (i ? '.' : '') + keys[i]
|
41 | }
|
42 | }
|
43 |
|
44 |
|
45 |
|
46 | if (typeof l === 'undefined') {
|
47 | str = key
|
48 | } else {
|
49 | str = (l && typeof l[key] !== 'undefined') ? l[key] : undefined
|
50 | }
|
51 |
|
52 |
|
53 | if (typeof str === 'undefined') {
|
54 | try {
|
55 | str = eval('l.' + key)
|
56 | } catch (e) { }
|
57 | }
|
58 |
|
59 | if (typeof str === 'undefined') str = key
|
60 |
|
61 | if (typeof str === 'string')
|
62 | return str.replace(
|
63 | /\$\{([^}]+)\}/g,
|
64 | (match, p) => typeof options[p] === 'undefined' ? p : options[p]
|
65 | )
|
66 | else
|
67 | return str
|
68 | }
|