-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
82 lines (70 loc) · 1.66 KB
/
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
/*
* Defines the Plugin Class
*/
var Plugin = function(){ };
/*
* Defines the Lang Class
*/
var Lang = function(options){
//This will hold the current lang
this.current_lang = options.lang;
//Holds the lag data
this.lang_data = {};
//Sets the current lang when first start the object
this.setLang(this.current_lang);
};
//This static object will hold all lang files
Lang.files = {};
/*
* This method is used to set the current lang.
*/
Lang.prototype.setLang = function(lang){
for(var i in Lang.files){
if(i.indexOf('./'+this.current_lang+'/') === 0){
var lang_name = i.replace('./'+this.current_lang+'/', '').replace('.js', '');
this.lang_data[lang_name] = Lang.files[i];
}
}
this.current_lang = lang;
this.lang_data = {};
for(var i in Lang.files){
if(i.indexOf('./'+lang+'/') === 0){
var lang_name = i.replace('./'+lang+'/', '').replace('.js', '');
this[lang_name] = Lang.files[i];
}
}
};
/*
* Gets the current lang
*/
Lang.prototype.getLang = function(){
return this.current_lang;
};
/*
* Requires all langs file
*/
Plugin.requireAll = function(r) {
r.keys().forEach(function(d){
Lang.files[d] = r(d);
});
};
/*
* Installs the plugin in the vuejs.
*/
Plugin.install = function(Vue, options){
var o = options || {};
var default_lang = o.default || 'en';
Object.defineProperty(Vue.prototype, '$lang', {
get:function () { return this.$root._lang }
});
if(typeof o != 'object'){
console.error('[vue-lang] the options should be an object type.');
return false;
}
Vue.mixin({
beforeCreate:function() {
Vue.util.defineReactive(this, '_lang', new Lang({lang:default_lang}));
}
});
};
module.exports = Plugin;