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