You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
999 B
38 lines
999 B
var Class = require('./Class'); |
|
var safeGet = require('./safeGet'); |
|
var extend = require('./extend'); |
|
var strTpl = require('./strTpl'); |
|
var isStr = require('./isStr'); |
|
var isFn = require('./isFn'); |
|
exports = Class({ |
|
initialize: function I18n(locale, langs) { |
|
this._locale = locale; |
|
this._langs = langs; |
|
}, |
|
set: function(locale, lang) { |
|
if (this._langs[locale]) { |
|
extend(this._langs[locale], lang); |
|
} else { |
|
this._langs[locale] = lang; |
|
} |
|
}, |
|
t: function(path, data) { |
|
var val = ''; |
|
var lang = this._langs[this._locale]; |
|
if (!lang) return ''; |
|
val = safeGet(lang, path); |
|
if (data) { |
|
if (isStr(val)) { |
|
val = strTpl(val, data); |
|
} else if (isFn(val)) { |
|
val = val(data); |
|
} |
|
} |
|
return val || ''; |
|
}, |
|
locale: function(locale) { |
|
this._locale = locale; |
|
} |
|
}); |
|
|
|
module.exports = exports;
|
|
|