Code coverage report for ./index.js

Statements: 95.92% (47 / 49)      Branches: 76.92% (30 / 39)      Functions: 100% (5 / 5)      Lines: 97.87% (46 / 47)      Ignored: none     

All files » ./ » index.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98        1             1                   1 14 14 14   1 7 7 7 6   1   7       14 14 14   14 9   7 7 7 3 2 1 1   4 1 1   1 1       7             14 9 9 9 7 7 14 14 1 1   13       9       14     7   7        
/**
 * Import(s)
 */
 
var format = require('./lib/format')
 
 
/**
 * Export(s)
 */
 
module.exports = plugin
 
 
/**
 * plugin
 *
 * @param {Object} Vue
 * @param {Object} opts
 */
 
function plugin (Vue, opts) {
  opts = opts || {}
  var lang = opts.lang || 'en'
  var locales = opts.locales || opts.resources || {}
 
  function getVal (path, key, lang, args) {
    var value = key
    try {
      var val = path.get(locales[lang], key)
      value = (args ? format(val, args) : val) || key
    } catch (e) {
      value = key
    }
    return value
  }
 
  // `$t` method (for Vue 0.11.4 later)
  try {
    var path = Vue.parsers.path
    var util = Vue.util
 
    Vue.prototype.$t = function (key) {
      if (!key) { return '' }
 
      var args = null
      var language = lang
      if (arguments.length === 2) {
        if (util.isObject(arguments[1]) || util.isArray(arguments[1])) {
          args = arguments[1]
        } else Eif (typeof arguments[1] === 'string') {
          language = arguments[1]
        }
      } else if (arguments.length === 3) {
        Eif (typeof arguments[1] === 'string') {
          language = arguments[1]
        }
        Eif (util.isObject(arguments[2]) || util.isArray(arguments[2])) {
          args = arguments[2]
        }
      }
 
      return getVal(path, key, language, args)
    }
  } catch (e) {
    Vue.utils.warn('not support $t in this Vue version')
  }
 
  // 't' function
  Vue.t = function (key) {
    var ret = key || ''
    var locale = locales[lang]
    if (key && locale) {
      var namespaces = key.split('.')
      for (var i = 0; i < namespaces.length; i++) {
        locale = locale[namespaces[i]]
        if (!locale) {
          ret = key
          break
        } else {
          ret = locale
        }
      }
    }
    return ret
  }
 
  // 'v-t' directive
  Vue.directive('t', {
    isLiteral: true,
    bind: function () {
      Iif (this.el.nodeType !== 1) { return }
 
      this.el.textContent = Vue.t(this.expression)
    }
  })
}