-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
138 lines (129 loc) · 3.61 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
var LocaleManager = function (name, options) {
this.name = name;
options = options || {};
options.log = options.log || console.error.bind(console);
this.options = options;
this.currentLocale = this.baseLocale = options.baseLocale || 'en';
this.locales = {};
this.locales[this.baseLocale] = {};
this.missing = {};
};
LocaleManager.prototype = {
setLocale: function (locale) {
this.currentLocale = locale;
},
setBaseLocale: function (locale) {
this.baseLocale = locale;
},
add: function (locale, items, options) {
if (!this.locales[locale]) {
this.locales[locale] = {};
}
options = options || {};
var base = this.locales[locale];
if (options.rel) {
for (var i=0; base && i<options.rel.length; i++) {
if (!base[options.rel[i]]) {
base[options.rel[i]] = {};
}
base = base[options.rel[i]];
}
}
extendDefault(base, items, !options.default);
},
addDefault: function (items, options) {
options = options || {};
options.default = true;
this.add(this.baseLocale, items, options);
},
getLoc: function (locale, key, rel, silent) {
var base = this.locales[locale]
, parts = (rel || []).concat(key.split('.'));
while (base && parts.length) {
base = base[parts.shift()];
}
if (!base) {
if (!silent) {
var msg = 'Localize string not found: ' + key
if (this.options.failHard) {
throw new Error(msg);
}
this.options.log(msg);
this.addMissing(locale, key, rel);
if (locale !== this.baseLocale) {
return this.getLoc(this.baseLocale, key, rel, true);
}
}
return '';
}
return base;
},
// key: str, rel: list of rel path
get: function (key, rel) {
return this.getLoc(this.currentLocale, key, rel);
},
addMissing: function (locale, key, rel) {
if (!this.missing[locale]) {
this.missing[locale] = {};
}
var base = this.missing[locale]
, parts = (rel || []).concat(key.split('.'));
while (parts.length - 1 > 0) {
if (!base[parts[0]]) {
base[parts[0]] = {};
}
base = base[parts.shift()];
}
base[parts.shift()] = true;
},
rel: function (name) {
var parts = name.split('.')
, that = this;
return {
get: function (key) {
return that.get(key, parts);
},
add: function (locale, items) {
return that.add(locale, items, {rel: parts});
},
addDefault: function (items) {
return that.add(that.baseLocale, items, {rel: parts, default: true});
},
rel: function (sub) {
return that.rel(sub + '.' + name);
}
};
}
};
// Extend object with items from news, recursively looking into objects
// override: bool
var extendDefault = function(obj, news, override) {
Object.keys(news).forEach(function(key) {
if (!obj.hasOwnProperty(key)) {
obj[key] = news[key];
} else if (typeof(news[key]) === 'object' && typeof(obj[key]) === 'object') {
extendDefault(obj[key], news[key], override);
} else if (override) {
obj[key] = news[key];
}
});
};
var single = new LocaleManager('default')
, registry = {
default: single
};
var getLocales = function (name) {
if (!name) return single;
if (!registry[name]) {
registry[name] = new LocaleManager(name);
}
return registry[name];
};
module.exports = function (rel) {
return single.rel(rel);
};
module.exports.getLocales = getLocales;
module.exports.LocaleManager = LocaleManager;
Object.keys(LocaleManager.prototype).forEach(function(key) {
module.exports[key] = single[key].bind(single);
});