-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathi18nextLocalStorageCache.js
166 lines (138 loc) · 4.6 KB
/
i18nextLocalStorageCache.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
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global.i18nextLocalStorageCache = factory());
}(this, (function () { 'use strict';
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,
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);
};
}
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var storage = {
setItem: function setItem(key, value) {
if (window.localStorage) {
try {
window.localStorage.setItem(key, value);
} catch (e) {
// f.log('failed to set value for key "' + key + '" to localStorage.');
}
}
},
getItem: function getItem(key, value) {
if (window.localStorage) {
try {
return window.localStorage.getItem(key, value);
} catch (e) {
// f.log('failed to get value for key "' + key + '" from localStorage.');
}
}
return undefined;
}
};
function getDefaults() {
return {
enabled: false,
prefix: 'i18next_res_',
expirationTime: 7 * 24 * 60 * 60 * 1000,
versions: {}
};
}
var Cache = function () {
function Cache(services) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
_classCallCheck(this, Cache);
this.init(services, options);
this.type = 'cache';
this.debouncedStore = debounce(this.store, 10000);
}
_createClass(Cache, [{
key: 'init',
value: function init(services) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
this.services = services;
this.options = defaults(options, this.options || {}, getDefaults());
}
}, {
key: 'load',
value: function load(lngs, callback) {
var _this = this;
var store = {};
var nowMS = new Date().getTime();
if (!window.localStorage || !lngs.length) {
return callback(null, null);
}
var todo = lngs.length;
lngs.forEach(function (lng) {
var local = storage.getItem(_this.options.prefix + lng);
if (local) {
local = JSON.parse(local);
if (
// expiration field is mandatory, and should not be expired
local.i18nStamp && local.i18nStamp + _this.options.expirationTime > nowMS &&
// there should be no language version set, or if it is, it should match the one in translation
_this.options.versions[lng] === local.i18nVersion) {
delete local.i18nVersion;
store[lng] = local;
}
}
todo -= 1;
if (todo === 0) {
callback(null, store);
}
});
return undefined;
}
}, {
key: 'store',
value: function store(storeParam) {
var store = storeParam;
if (window.localStorage) {
for (var m in store) {
// eslint-disable-line
// timestamp
store[m].i18nStamp = new Date().getTime();
// language version (if set)
if (this.options.versions[m]) {
store[m].i18nVersion = this.options.versions[m];
}
// save
storage.setItem(this.options.prefix + m, JSON.stringify(store[m]));
}
}
}
}, {
key: 'save',
value: function save(store) {
this.debouncedStore(store);
}
}]);
return Cache;
}();
Cache.type = 'cache';
return Cache;
})));