-
-
Notifications
You must be signed in to change notification settings - Fork 628
/
ResourceStore.js
169 lines (136 loc) · 4.29 KB
/
ResourceStore.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import EventEmitter from './EventEmitter.js';
import * as utils from './utils.js';
class ResourceStore extends EventEmitter {
constructor(data, options = { ns: ['translation'], defaultNS: 'translation' }) {
super();
this.data = data || {};
this.options = options;
if (this.options.keySeparator === undefined) {
this.options.keySeparator = '.';
}
if (this.options.ignoreJSONStructure === undefined) {
this.options.ignoreJSONStructure = true;
}
}
addNamespaces(ns) {
if (this.options.ns.indexOf(ns) < 0) {
this.options.ns.push(ns);
}
}
removeNamespaces(ns) {
const index = this.options.ns.indexOf(ns);
if (index > -1) {
this.options.ns.splice(index, 1);
}
}
getResource(lng, ns, key, options = {}) {
const keySeparator =
options.keySeparator !== undefined ? options.keySeparator : this.options.keySeparator;
const ignoreJSONStructure =
options.ignoreJSONStructure !== undefined
? options.ignoreJSONStructure
: this.options.ignoreJSONStructure;
let path;
if (lng.indexOf('.') > -1) {
path = lng.split('.');
} else {
path = [lng, ns];
if (key) {
if (Array.isArray(key)) {
path.push(...key);
} else if (typeof key === 'string' && keySeparator) {
path.push(...key.split(keySeparator));
} else {
path.push(key);
}
}
}
const result = utils.getPath(this.data, path);
if (!result && !ns && !key && lng.indexOf('.') > -1) {
lng = path[0];
ns = path[1];
key = path.slice(2).join('.');
}
if (result || !ignoreJSONStructure || typeof key !== 'string') return result;
return utils.deepFind(this.data && this.data[lng] && this.data[lng][ns], key, keySeparator);
}
addResource(lng, ns, key, value, options = { silent: false }) {
const keySeparator =
options.keySeparator !== undefined ? options.keySeparator : this.options.keySeparator;
let path = [lng, ns];
if (key) path = path.concat(keySeparator ? key.split(keySeparator) : key);
if (lng.indexOf('.') > -1) {
path = lng.split('.');
value = ns;
ns = path[1];
}
this.addNamespaces(ns);
utils.setPath(this.data, path, value);
if (!options.silent) this.emit('added', lng, ns, key, value);
}
addResources(lng, ns, resources, options = { silent: false }) {
/* eslint no-restricted-syntax: 0 */
for (const m in resources) {
if (
typeof resources[m] === 'string' ||
Object.prototype.toString.apply(resources[m]) === '[object Array]'
)
this.addResource(lng, ns, m, resources[m], { silent: true });
}
if (!options.silent) this.emit('added', lng, ns, resources);
}
addResourceBundle(
lng,
ns,
resources,
deep,
overwrite,
options = { silent: false, skipCopy: false },
) {
let path = [lng, ns];
if (lng.indexOf('.') > -1) {
path = lng.split('.');
deep = resources;
resources = ns;
ns = path[1];
}
this.addNamespaces(ns);
let pack = utils.getPath(this.data, path) || {};
if (!options.skipCopy) resources = JSON.parse(JSON.stringify(resources)); // make a copy to fix #2081
if (deep) {
utils.deepExtend(pack, resources, overwrite);
} else {
pack = { ...pack, ...resources };
}
utils.setPath(this.data, path, pack);
if (!options.silent) this.emit('added', lng, ns, resources);
}
removeResourceBundle(lng, ns) {
if (this.hasResourceBundle(lng, ns)) {
delete this.data[lng][ns];
}
this.removeNamespaces(ns);
this.emit('removed', lng, ns);
}
hasResourceBundle(lng, ns) {
return this.getResource(lng, ns) !== undefined;
}
getResourceBundle(lng, ns) {
if (!ns) ns = this.options.defaultNS;
// COMPATIBILITY: remove extend in v2.1.0
if (this.options.compatibilityAPI === 'v1') return { ...{}, ...this.getResource(lng, ns) };
return this.getResource(lng, ns);
}
getDataByLanguage(lng) {
return this.data[lng];
}
hasLanguageSomeTranslations(lng) {
const data = this.getDataByLanguage(lng);
const n = (data && Object.keys(data)) || [];
return !!n.find((v) => data[v] && Object.keys(data[v]).length > 0);
}
toJSON() {
return this.data;
}
}
export default ResourceStore;