Completed
Push — master ( 33a525...eb0f8c )
by greg
17s
created

translate.js ➔ getVariable   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
nc 3
nop 3
dl 0
loc 14
rs 9.4285
1
import {coreUtils} from '../../../'
2
import Handlebars from 'handlebars'
3
 
4
function getVariable(variable, json, options) {
5
  if (variable.indexOf('{{') > -1) {
6
    var template = Handlebars.compile(variable)
7
    var res = template(json)
8
 
9
    if (res === "") {
10
      template = Handlebars.compile(variable)
11
      res = template(options.data.root)
12
    }
13
    return res
14
  }else {
15
    return variable
16
  }
17
}
18
 
19
export default function translate(lang, str, json, options) {
20
  var resLang = getVariable(lang, json, options)
21
  var resStr = getVariable(str, json, options)
22
 
23
  var trad = coreUtils.locales.instance.i18n
24
  if(typeof trad[resLang] !== 'undefined' && trad[resLang] !== null
25
    && typeof trad[resLang][resStr] !== 'undefined' && trad[resLang][resStr] !== null) {
26
    return trad[resLang][resStr]
27
  }
28
 
29
  return resStr
30
}