-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlocizeLastUsed.js
284 lines (280 loc) · 12.4 KB
/
locizeLastUsed.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.locizeLastUsed = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _utils = require("./utils.js");
var _request = _interopRequireDefault(require("./request.js"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
var getDefaults = function getDefaults() {
return {
lastUsedPath: 'https://api.locize.app/used/{{projectId}}/{{version}}/{{lng}}/{{ns}}',
referenceLng: 'en',
crossDomain: true,
setContentTypeJSON: false,
version: 'latest',
debounceSubmit: 90000,
allowedHosts: ['localhost']
};
};
var locizeLastUsed = {
init: function init(options) {
var isI18next = options.t && typeof options.t === 'function';
if (isI18next && !options.options.locizeLastUsed.projectId && options.options.backend.projectId) {
options.options.locizeLastUsed.projectId = options.options.backend.projectId;
}
if (isI18next && !options.options.locizeLastUsed.version && options.options.backend.version) {
options.options.locizeLastUsed.version = options.options.backend.version;
}
if (isI18next && !options.options.locizeLastUsed.apiKey && options.options.backend.apiKey) {
options.options.locizeLastUsed.apiKey = options.options.backend.apiKey;
}
if (isI18next && !options.options.locizeLastUsed.referenceLng && options.options.backend.referenceLng) {
options.options.locizeLastUsed.referenceLng = options.options.backend.referenceLng;
}
if (isI18next && !options.options.locizeLastUsed.referenceLng && options.options.fallbackLng && Array.isArray(options.options.fallbackLng) && options.options.fallbackLng[0] !== 'dev') {
options.options.locizeLastUsed.referenceLng = options.options.fallbackLng[0];
}
this.options = isI18next ? (0, _utils.defaults)(options.options.locizeLastUsed, this.options || {}, getDefaults()) : (0, _utils.defaults)(options, this.options || {}, getDefaults());
var hostname = typeof window !== 'undefined' && window.location && window.location.hostname;
if (hostname) {
this.isAllowed = this.options.allowedHosts.indexOf(hostname) > -1;
} else {
this.isAllowed = true;
}
this.submitting = null;
this.pending = {};
this.done = {};
this.submit = (0, _utils.debounce)(this.submit, this.options.debounceSubmit);
if (isI18next) this.interceptI18next(options);
},
interceptI18next: function interceptI18next(i18next) {
var _this = this;
var origGetResource = i18next.services.resourceStore.getResource;
i18next.services.resourceStore.getResource = function (lng, ns, key, options) {
if (key) _this.used(ns, key);
return origGetResource.call(i18next.services.resourceStore, lng, ns, key, options);
};
},
used: function used(ns, key, callback) {
var _this2 = this;
['pending', 'done'].forEach(function (k) {
if (_this2.done[ns] && _this2.done[ns][key]) return;
if (!_this2[k][ns]) _this2[k][ns] = {};
_this2[k][ns][key] = true;
});
this.submit(callback);
},
submit: function submit(callback) {
var _this3 = this;
if (!this.isAllowed) return callback && callback(new Error('not allowed'));
if (this.submitting) return this.submit(callback);
var isMissing = (0, _utils.isMissingOption)(this.options, ['projectId', 'version', 'apiKey', 'referenceLng']);
if (isMissing) return callback && callback(new Error(isMissing));
this.submitting = this.pending;
this.pending = {};
var namespaces = Object.keys(this.submitting);
var todo = namespaces.length;
var doneOne = function doneOne(err) {
todo--;
if (!todo) {
_this3.submitting = null;
if (callback) callback(err);
}
};
namespaces.forEach(function (ns) {
var keys = Object.keys(_this3.submitting[ns]);
var url = (0, _utils.replaceIn)(_this3.options.lastUsedPath, ['projectId', 'version', 'lng', 'ns'], (0, _utils.defaults)({
lng: _this3.options.referenceLng,
ns: ns
}, _this3.options));
if (keys.length) {
(0, _request.default)((0, _utils.defaults)({
authorize: true
}, _this3.options), url, keys, doneOne);
} else {
doneOne();
}
});
}
};
locizeLastUsed.type = '3rdParty';
var _default = exports.default = locizeLastUsed;
module.exports = exports.default;
},{"./request.js":2,"./utils.js":3}],2:[function(require,module,exports){
(function (global){(function (){
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
var fetchApi = typeof fetch === 'function' ? fetch : undefined;
if (typeof global !== 'undefined' && global.fetch) {
fetchApi = global.fetch;
} else if (typeof window !== 'undefined' && window.fetch) {
fetchApi = window.fetch;
}
var XmlHttpRequestApi;
if (typeof XMLHttpRequest === 'function' || (typeof XMLHttpRequest === "undefined" ? "undefined" : _typeof(XMLHttpRequest)) === 'object') {
if (typeof global !== 'undefined' && global.XMLHttpRequest) {
XmlHttpRequestApi = global.XMLHttpRequest;
} else if (typeof window !== 'undefined' && window.XMLHttpRequest) {
XmlHttpRequestApi = window.XMLHttpRequest;
}
}
var ActiveXObjectApi;
if (typeof ActiveXObject === 'function') {
if (typeof global !== 'undefined' && global.ActiveXObject) {
ActiveXObjectApi = global.ActiveXObject;
} else if (typeof window !== 'undefined' && window.ActiveXObject) {
ActiveXObjectApi = window.ActiveXObject;
}
}
if (typeof fetchApi !== 'function') fetchApi = undefined;
if (!fetchApi && !XmlHttpRequestApi && !ActiveXObjectApi) {
try {
fetchApi = require('cross-fetch');
} catch (e) {}
}
var requestWithFetch = function requestWithFetch(options, url, payload, callback) {
var resolver = function resolver(response) {
var resourceNotExisting = response.headers && response.headers.get('x-cache') === 'Error from cloudfront';
if (!response.ok) return callback(response.statusText || 'Error', {
status: response.status,
resourceNotExisting: resourceNotExisting
});
response.text().then(function (data) {
callback(null, {
status: response.status,
data: data,
resourceNotExisting: resourceNotExisting
});
}).catch(callback);
};
var headers = {
Authorization: options.authorize && options.apiKey ? options.apiKey : undefined,
'Content-Type': 'application/json'
};
if (typeof window === 'undefined' && typeof global !== 'undefined' && typeof global.process !== 'undefined' && global.process.versions && global.process.versions.node) {
headers['User-Agent'] = "locize-lastused (node/".concat(global.process.version, "; ").concat(global.process.platform, " ").concat(global.process.arch, ")");
}
if (typeof fetch === 'function') {
fetch(url, {
method: payload ? 'POST' : 'GET',
body: payload ? JSON.stringify(payload) : undefined,
headers: headers
}).then(resolver).catch(callback);
} else {
fetchApi(url, {
method: payload ? 'POST' : 'GET',
body: payload ? JSON.stringify(payload) : undefined,
headers: headers
}).then(resolver).catch(callback);
}
};
var requestWithXmlHttpRequest = function requestWithXmlHttpRequest(options, url, payload, callback) {
try {
var x = XmlHttpRequestApi ? new XmlHttpRequestApi() : new ActiveXObjectApi('MSXML2.XMLHTTP.3.0');
x.open(payload ? 'POST' : 'GET', url, 1);
if (!options.crossDomain) {
x.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
}
if (options.authorize && options.apiKey) {
x.setRequestHeader('Authorization', options.apiKey);
}
if (payload || options.setContentTypeJSON) {
x.setRequestHeader('Content-Type', 'application/json');
}
x.onreadystatechange = function () {
var resourceNotExisting = x.getResponseHeader('x-cache') === 'Error from cloudfront';
x.readyState > 3 && callback(x.status >= 400 ? x.statusText : null, {
status: x.status,
data: x.responseText,
resourceNotExisting: resourceNotExisting
});
};
x.send(JSON.stringify(payload));
} catch (e) {
console && console.log(e);
}
};
var request = function request(options, url, payload, callback) {
if (typeof payload === 'function') {
callback = payload;
payload = undefined;
}
callback = callback || function () {};
if (fetchApi) {
return requestWithFetch(options, url, payload, callback);
}
if (typeof XMLHttpRequest === 'function' || (typeof XMLHttpRequest === "undefined" ? "undefined" : _typeof(XMLHttpRequest)) === 'object' || typeof ActiveXObject === 'function') {
return requestWithXmlHttpRequest(options, url, payload, callback);
}
callback(new Error('No fetch and no xhr implementation found!'));
};
var _default = exports.default = request;
module.exports = exports.default;
}).call(this)}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
},{"cross-fetch":4}],3:[function(require,module,exports){
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.debounce = debounce;
exports.defaults = defaults;
exports.isMissingOption = isMissingOption;
exports.replaceIn = replaceIn;
var arr = [];
var each = arr.forEach;
var slice = arr.slice;
function defaults(obj) {
each.call(slice.call(arguments, 1), function (source) {
if (source) {
for (var prop in source) {
if (obj[prop] === undefined) obj[prop] = source[prop];
}
}
});
return obj;
}
function debounce(func, wait, immediate) {
var timeout;
return function () {
var context = this;
var args = arguments;
var later = function later() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
}
function isMissingOption(obj, props) {
return props.reduce(function (mem, p) {
if (mem) return mem;
if (!obj || !obj[p] || typeof obj[p] !== 'string' || !obj[p].toLowerCase() === p.toLowerCase()) {
var err = "i18next-lastused :: got \"".concat(obj[p], "\" in options for ").concat(p, " which is invalid.");
console.warn(err);
return err;
}
return false;
}, false);
}
function replaceIn(str, arr, options) {
var ret = str;
arr.forEach(function (s) {
var regexp = new RegExp("{{".concat(s, "}}"), 'g');
ret = ret.replace(regexp, options[s]);
});
return ret;
}
},{}],4:[function(require,module,exports){
},{}]},{},[1])(1)
});