locizify 6.0.4 → 6.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ### 6.0.5
2
+
3
+ - update locize plugin
4
+
1
5
  ### 6.0.4
2
6
 
3
7
  - update locize plugin
@@ -29,35 +29,23 @@ var defaults = {
29
29
  reloadOnSave: true,
30
30
  bindSavedMissing: true
31
31
  };
32
+ i18next.use(_i18nextLocizeBackend["default"]).use((0, _locize.locizeEditorPlugin)());
33
+ i18next.on('editorSaved', function () {
34
+ _i18nextify["default"].forceRerender();
35
+ });
36
+
37
+ function getQsParameterByName(name, url) {
38
+ if (typeof window === 'undefined') return null;
39
+ if (!url) url = window.location.href.toLowerCase(); // eslint-disable-next-line no-useless-escape
32
40
 
33
- function getParameterByName(name) {
34
- var url = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : window.location.href.toLowerCase();
35
41
  name = name.replace(/[\[\]]/g, '\\$&');
36
- var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
37
- results = regex.exec(url);
42
+ var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)');
43
+ var results = regex.exec(url);
38
44
  if (!results) return null;
39
45
  if (!results[2]) return '';
40
46
  return decodeURIComponent(results[2].replace(/\+/g, ' '));
41
47
  }
42
48
 
43
- var isInIframe = typeof window !== 'undefined';
44
-
45
- try {
46
- // eslint-disable-next-line no-undef, no-restricted-globals
47
- isInIframe = self !== top; // eslint-disable-next-line no-empty
48
- } catch (e) {}
49
-
50
- i18next.use(_i18nextLocizeBackend["default"]);
51
-
52
- if (isInIframe) {
53
- i18next.use(_locize.locizePlugin);
54
- } else if (getParameterByName('incontext') === 'true') {
55
- i18next.use(_locize.locizePlugin);
56
- }
57
-
58
- i18next.on('editorSaved', function () {
59
- _i18nextify["default"].forceRerender();
60
- });
61
49
  var originalInit = i18next.init;
62
50
 
63
51
  i18next.init = function () {
@@ -94,7 +82,7 @@ i18next.init = function () {
94
82
  if (value !== undefined && value !== null) backend[attr] = value;
95
83
 
96
84
  if (!value) {
97
- value = getParameterByName(attr.toLowerCase());
85
+ value = getQsParameterByName(attr.toLowerCase());
98
86
  if (value === 'true') value = true;
99
87
  if (value === 'false') value = false;
100
88
  if (attr.toLowerCase() === 'autopilot' && value === '') value = true;
@@ -117,8 +105,8 @@ i18next.init = function () {
117
105
  callback(err, t);
118
106
  }
119
107
 
120
- if (!options.backend.apiKey && getParameterByName('apikey')) {
121
- options.backend.apiKey = getParameterByName('apikey');
108
+ if (!options.backend.apiKey && getQsParameterByName('apikey')) {
109
+ options.backend.apiKey = getQsParameterByName('apikey');
122
110
  }
123
111
 
124
112
  if (!options.backend.autoPilot || options.backend.autoPilot === 'false') return originalInit.call(i18next, _objectSpread(_objectSpread({}, options), enforce), handleI18nextInitialized);
package/dist/es/index.js CHANGED
@@ -7,7 +7,7 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
7
7
 
8
8
  import i18nextify from 'i18nextify';
9
9
  import LocizeBackend from 'i18next-locize-backend';
10
- import { locizePlugin, turnOn, turnOff, setEditorLng } from 'locize';
10
+ import { locizeEditorPlugin, turnOn, turnOff, setEditorLng } from 'locize';
11
11
  var i18next = i18nextify.i18next;
12
12
  var enforce = {
13
13
  saveMissingTo: 'all'
@@ -16,35 +16,23 @@ var defaults = {
16
16
  reloadOnSave: true,
17
17
  bindSavedMissing: true
18
18
  };
19
+ i18next.use(LocizeBackend).use(locizeEditorPlugin());
20
+ i18next.on('editorSaved', function () {
21
+ i18nextify.forceRerender();
22
+ });
23
+
24
+ function getQsParameterByName(name, url) {
25
+ if (typeof window === 'undefined') return null;
26
+ if (!url) url = window.location.href.toLowerCase(); // eslint-disable-next-line no-useless-escape
19
27
 
20
- function getParameterByName(name) {
21
- var url = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : window.location.href.toLowerCase();
22
28
  name = name.replace(/[\[\]]/g, '\\$&');
23
- var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
24
- results = regex.exec(url);
29
+ var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)');
30
+ var results = regex.exec(url);
25
31
  if (!results) return null;
26
32
  if (!results[2]) return '';
27
33
  return decodeURIComponent(results[2].replace(/\+/g, ' '));
28
34
  }
29
35
 
30
- var isInIframe = typeof window !== 'undefined';
31
-
32
- try {
33
- // eslint-disable-next-line no-undef, no-restricted-globals
34
- isInIframe = self !== top; // eslint-disable-next-line no-empty
35
- } catch (e) {}
36
-
37
- i18next.use(LocizeBackend);
38
-
39
- if (isInIframe) {
40
- i18next.use(locizePlugin);
41
- } else if (getParameterByName('incontext') === 'true') {
42
- i18next.use(locizePlugin);
43
- }
44
-
45
- i18next.on('editorSaved', function () {
46
- i18nextify.forceRerender();
47
- });
48
36
  var originalInit = i18next.init;
49
37
 
50
38
  i18next.init = function () {
@@ -81,7 +69,7 @@ i18next.init = function () {
81
69
  if (value !== undefined && value !== null) backend[attr] = value;
82
70
 
83
71
  if (!value) {
84
- value = getParameterByName(attr.toLowerCase());
72
+ value = getQsParameterByName(attr.toLowerCase());
85
73
  if (value === 'true') value = true;
86
74
  if (value === 'false') value = false;
87
75
  if (attr.toLowerCase() === 'autopilot' && value === '') value = true;
@@ -104,8 +92,8 @@ i18next.init = function () {
104
92
  callback(err, t);
105
93
  }
106
94
 
107
- if (!options.backend.apiKey && getParameterByName('apikey')) {
108
- options.backend.apiKey = getParameterByName('apikey');
95
+ if (!options.backend.apiKey && getQsParameterByName('apikey')) {
96
+ options.backend.apiKey = getQsParameterByName('apikey');
109
97
  }
110
98
 
111
99
  if (!options.backend.autoPilot || options.backend.autoPilot === 'false') return originalInit.call(i18next, _objectSpread(_objectSpread({}, options), enforce), handleI18nextInitialized);
@@ -9967,6 +9967,7 @@
9967
9967
  function handler$3(payload) {
9968
9968
  api.initialized = true;
9969
9969
  clearInterval(api.initInterval);
9970
+ delete api.initInterval;
9970
9971
  api.sendCurrentParsedContent();
9971
9972
  api.sendCurrentTargetLanguage();
9972
9973
  }
@@ -12265,7 +12266,33 @@
12265
12266
  return found;
12266
12267
  }
12267
12268
 
12269
+ function getQsParameterByName(name, url) {
12270
+ if (typeof window === 'undefined') return null;
12271
+ if (!url) url = window.location.href.toLowerCase();
12272
+ name = name.replace(/[\[\]]/g, '\\$&');
12273
+ var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)');
12274
+ var results = regex.exec(url);
12275
+ if (!results) return null;
12276
+ if (!results[2]) return '';
12277
+ return decodeURIComponent(results[2].replace(/\+/g, ' '));
12278
+ }
12279
+
12280
+ var mutationTriggeringElements = {};
12281
+
12268
12282
  function ignoreMutation(ele) {
12283
+ if (ele.uniqueID) {
12284
+ var info = mutationTriggeringElements[ele.uniqueID];
12285
+
12286
+ if (info && info.triggered > 10 && info.lastTriggerDate + 500 < Date.now()) {
12287
+ if (!info.warned && console) {
12288
+ console.warn('locize ::: ignoring element change - an element is rerendering too often in short interval', '\n', 'consider adding the "data-locize-editor-ignore:" attribute to the element:', ele);
12289
+ info.warned = true;
12290
+ }
12291
+
12292
+ return true;
12293
+ }
12294
+ }
12295
+
12269
12296
  var ret = ele.dataset && (ele.dataset.i18nextEditorElement === 'true' || ele.dataset.locizeEditorIgnore === 'true');
12270
12297
  if (!ret && ele.parentElement) return ignoreMutation(ele.parentElement);
12271
12298
  return ret;
@@ -12299,6 +12326,14 @@
12299
12326
  return;
12300
12327
  }
12301
12328
 
12329
+ Object.keys(mutationTriggeringElements).forEach(function (k) {
12330
+ var info = mutationTriggeringElements[k];
12331
+
12332
+ if (info.lastTriggerDate + 60000 < Date.now()) {
12333
+ delete mutationTriggeringElements[k];
12334
+ }
12335
+ });
12336
+
12302
12337
  if (mutation.type === 'childList') {
12303
12338
  var notOurs = 0;
12304
12339
 
@@ -12317,8 +12352,21 @@
12317
12352
  }
12318
12353
 
12319
12354
  triggerMutation = true;
12355
+
12356
+ if (mutation.target && mutation.target.uniqueID) {
12357
+ var info = mutationTriggeringElements[mutation.target.uniqueID] || {
12358
+ triggered: 0
12359
+ };
12360
+ info.triggered = info.triggered + 1;
12361
+ info.lastTriggerDate = Date.now();
12362
+ mutationTriggeringElements[mutation.target.uniqueID] = info;
12363
+ }
12364
+
12320
12365
  var includedAlready = targetEles.reduce(function (mem, element) {
12321
- if (mem || element.contains(mutation.target) || !mutation.target.parentElement) return true;
12366
+ if (mem || element.contains(mutation.target) || !mutation.target.parentElement) {
12367
+ return true;
12368
+ }
12369
+
12322
12370
  return false;
12323
12371
  }, false);
12324
12372
 
@@ -12850,105 +12898,116 @@
12850
12898
  isInIframe = self !== top;
12851
12899
  } catch (e) {}
12852
12900
 
12853
- var i18next;
12854
- var locizePlugin = {
12855
- type: '3rdParty',
12856
- init: function init(i18n) {
12857
- var options = i18n.options;
12858
- i18next = i18n;
12859
-
12860
- if (!isInIframe) {
12861
- i18next.use(SubliminalPostProcessor);
12862
-
12863
- if (typeof options.postProcess === 'string') {
12864
- options.postProcess = [options.postProcess, 'subliminal'];
12865
- } else if (Array.isArray(options.postProcess)) {
12866
- options.postProcess.push('subliminal');
12867
- } else {
12868
- options.postProcess = 'subliminal';
12869
- }
12901
+ function configurePostProcessor(i18next, options) {
12902
+ i18next.use(SubliminalPostProcessor);
12870
12903
 
12871
- options.postProcessPassResolved = true;
12872
- }
12904
+ if (typeof options.postProcess === 'string') {
12905
+ options.postProcess = [options.postProcess, 'subliminal'];
12906
+ } else if (Array.isArray(options.postProcess)) {
12907
+ options.postProcess.push('subliminal');
12908
+ } else {
12909
+ options.postProcess = 'subliminal';
12910
+ }
12873
12911
 
12874
- var impl = {
12875
- getResource: function getResource(lng, ns, key) {
12876
- return i18n.getResource(lng, ns, key);
12877
- },
12878
- setResource: function setResource(lng, ns, key, value) {
12879
- return i18n.addResource(lng, ns, key, value, {
12880
- silent: true
12881
- });
12882
- },
12883
- getResourceBundle: function getResourceBundle(lng, ns, cb) {
12884
- i18n.loadNamespaces(ns, function () {
12885
- cb(i18n.getResourceBundle(lng, ns));
12886
- });
12887
- },
12888
- getLng: function getLng() {
12889
- return i18n.languages[0];
12890
- },
12891
- getSourceLng: function getSourceLng() {
12892
- var fallback = i18n.options.fallbackLng;
12893
- if (typeof fallback === 'string') return fallback;
12894
- if (Array.isArray(fallback)) return fallback[fallback.length - 1];
12895
-
12896
- if (fallback && fallback["default"]) {
12897
- if (typeof fallback["default"] === 'string') return fallback;
12898
- if (Array.isArray(fallback["default"])) return fallback["default"][fallback["default"].length - 1];
12899
- }
12912
+ options.postProcessPassResolved = true;
12913
+ }
12900
12914
 
12901
- if (typeof fallback === 'function') {
12902
- var res = fallback(i18n.resolvedLanguage);
12903
- if (typeof res === 'string') return res;
12904
- if (Array.isArray(res)) return res[res.length - 1];
12905
- }
12915
+ function getImplementation(i18n) {
12916
+ var impl = {
12917
+ getResource: function getResource(lng, ns, key) {
12918
+ return i18n.getResource(lng, ns, key);
12919
+ },
12920
+ setResource: function setResource(lng, ns, key, value) {
12921
+ return i18n.addResource(lng, ns, key, value, {
12922
+ silent: true
12923
+ });
12924
+ },
12925
+ getResourceBundle: function getResourceBundle(lng, ns, cb) {
12926
+ i18n.loadNamespaces(ns, function () {
12927
+ cb(i18n.getResourceBundle(lng, ns));
12928
+ });
12929
+ },
12930
+ getLng: function getLng() {
12931
+ return i18n.languages[0];
12932
+ },
12933
+ getSourceLng: function getSourceLng() {
12934
+ var fallback = i18n.options.fallbackLng;
12935
+ if (typeof fallback === 'string') return fallback;
12936
+ if (Array.isArray(fallback)) return fallback[fallback.length - 1];
12906
12937
 
12907
- return 'dev';
12908
- },
12909
- getLocizeDetails: function getLocizeDetails() {
12910
- var backendName;
12938
+ if (fallback && fallback["default"]) {
12939
+ if (typeof fallback["default"] === 'string') return fallback;
12940
+ if (Array.isArray(fallback["default"])) return fallback["default"][fallback["default"].length - 1];
12941
+ }
12911
12942
 
12912
- if (i18n.services.backendConnector.backend && i18n.services.backendConnector.backend.options && i18n.services.backendConnector.backend.options.loadPath && i18n.services.backendConnector.backend.options.loadPath.indexOf('.locize.') > 0) {
12913
- backendName = 'I18NextLocizeBackend';
12914
- } else {
12915
- backendName = i18n.services.backendConnector.backend ? i18n.services.backendConnector.backend.constructor.name : 'options.resources';
12916
- }
12943
+ if (typeof fallback === 'function') {
12944
+ var res = fallback(i18n.resolvedLanguage);
12945
+ if (typeof res === 'string') return res;
12946
+ if (Array.isArray(res)) return res[res.length - 1];
12947
+ }
12917
12948
 
12918
- var opts = {
12919
- backendName: backendName,
12920
- sourceLng: impl.getSourceLng(),
12921
- i18nFormat: i18n.options.compatibilityJSON === 'v3' ? 'i18next_v3' : 'i18next_v4',
12922
- i18nFramework: 'i18next',
12923
- isLocizify: i18n.options.isLocizify,
12924
- defaultNS: i18n.options.defaultNS
12925
- };
12926
- if (!i18n.options.backend && !i18n.options.editor) return opts;
12927
- var pickFrom = i18n.options.backend || i18n.options.editor;
12928
- return _objectSpread$a(_objectSpread$a({}, opts), {}, {
12929
- projectId: pickFrom.projectId,
12930
- version: pickFrom.version
12931
- });
12932
- },
12933
- bindLanguageChange: function bindLanguageChange(cb) {
12934
- i18n.on('languageChanged', cb);
12935
- },
12936
- bindMissingKeyHandler: function bindMissingKeyHandler(cb) {
12937
- i18n.options.missingKeyHandler = function (lng, ns, k, val, isUpdate, opts) {
12938
- if (!isUpdate) cb(lng, ns, k, val);
12939
- };
12940
- },
12941
- triggerRerender: function triggerRerender() {
12942
- i18n.emit('editorSaved');
12949
+ return 'dev';
12950
+ },
12951
+ getLocizeDetails: function getLocizeDetails() {
12952
+ var backendName;
12953
+
12954
+ if (i18n.services.backendConnector.backend && i18n.services.backendConnector.backend.options && i18n.services.backendConnector.backend.options.loadPath && i18n.services.backendConnector.backend.options.loadPath.indexOf('.locize.') > 0) {
12955
+ backendName = 'I18NextLocizeBackend';
12956
+ } else {
12957
+ backendName = i18n.services.backendConnector.backend ? i18n.services.backendConnector.backend.constructor.name : 'options.resources';
12943
12958
  }
12944
- };
12945
12959
 
12946
- if (!isInIframe) {
12947
- start(impl);
12948
- } else {
12949
- startLegacy(impl);
12960
+ var opts = {
12961
+ backendName: backendName,
12962
+ sourceLng: impl.getSourceLng(),
12963
+ i18nFormat: i18n.options.compatibilityJSON === 'v3' ? 'i18next_v3' : 'i18next_v4',
12964
+ i18nFramework: 'i18next',
12965
+ isLocizify: i18n.options.isLocizify,
12966
+ defaultNS: i18n.options.defaultNS
12967
+ };
12968
+ if (!i18n.options.backend && !i18n.options.editor) return opts;
12969
+ var pickFrom = i18n.options.backend || i18n.options.editor;
12970
+ return _objectSpread$a(_objectSpread$a({}, opts), {}, {
12971
+ projectId: pickFrom.projectId,
12972
+ version: pickFrom.version
12973
+ });
12974
+ },
12975
+ bindLanguageChange: function bindLanguageChange(cb) {
12976
+ i18n.on('languageChanged', cb);
12977
+ },
12978
+ bindMissingKeyHandler: function bindMissingKeyHandler(cb) {
12979
+ i18n.options.missingKeyHandler = function (lng, ns, k, val, isUpdate, opts) {
12980
+ if (!isUpdate) cb(lng, ns, k, val);
12981
+ };
12982
+ },
12983
+ triggerRerender: function triggerRerender() {
12984
+ i18n.emit('editorSaved');
12950
12985
  }
12951
- }
12986
+ };
12987
+ return impl;
12988
+ }
12989
+
12990
+ var i18next;
12991
+
12992
+ var locizeEditorPlugin = function locizeEditorPlugin() {
12993
+ var opt = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
12994
+ opt.qsProp = opt.qsProp || 'incontext';
12995
+ return {
12996
+ type: '3rdParty',
12997
+ init: function init(i18n) {
12998
+ var options = i18n.options;
12999
+ i18next = i18n;
13000
+ var showInContext = getQsParameterByName(opt.qsProp) === 'true';
13001
+ if (!isInIframe && showInContext) configurePostProcessor(i18next, options);
13002
+ var impl = getImplementation(i18n);
13003
+
13004
+ if (!isInIframe && showInContext) {
13005
+ start(impl);
13006
+ } else {
13007
+ startLegacy(impl);
13008
+ }
13009
+ }
13010
+ };
12952
13011
  };
12953
13012
 
12954
13013
  function startStandalone() {
@@ -12979,35 +13038,23 @@
12979
13038
  reloadOnSave: true,
12980
13039
  bindSavedMissing: true
12981
13040
  };
13041
+ i18next$1.use(I18NextLocizeBackend).use(locizeEditorPlugin());
13042
+ i18next$1.on('editorSaved', () => {
13043
+ i18nextify.forceRerender();
13044
+ });
13045
+
13046
+ function getQsParameterByName$1(name, url) {
13047
+ if (typeof window === 'undefined') return null;
13048
+ if (!url) url = window.location.href.toLowerCase(); // eslint-disable-next-line no-useless-escape
12982
13049
 
12983
- function getParameterByName(name) {
12984
- var url = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : window.location.href.toLowerCase();
12985
13050
  name = name.replace(/[\[\]]/g, '\\$&');
12986
- var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
12987
- results = regex.exec(url);
13051
+ var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)');
13052
+ var results = regex.exec(url);
12988
13053
  if (!results) return null;
12989
13054
  if (!results[2]) return '';
12990
13055
  return decodeURIComponent(results[2].replace(/\+/g, ' '));
12991
13056
  }
12992
13057
 
12993
- var isInIframe$1 = typeof window !== 'undefined';
12994
-
12995
- try {
12996
- // eslint-disable-next-line no-undef, no-restricted-globals
12997
- isInIframe$1 = self !== top; // eslint-disable-next-line no-empty
12998
- } catch (e) {}
12999
-
13000
- i18next$1.use(I18NextLocizeBackend);
13001
-
13002
- if (isInIframe$1) {
13003
- i18next$1.use(locizePlugin);
13004
- } else if (getParameterByName('incontext') === 'true') {
13005
- i18next$1.use(locizePlugin);
13006
- }
13007
-
13008
- i18next$1.on('editorSaved', () => {
13009
- i18nextify.forceRerender();
13010
- });
13011
13058
  var originalInit = i18next$1.init;
13012
13059
 
13013
13060
  i18next$1.init = function () {
@@ -13042,7 +13089,7 @@
13042
13089
  if (value !== undefined && value !== null) backend[attr] = value;
13043
13090
 
13044
13091
  if (!value) {
13045
- value = getParameterByName(attr.toLowerCase());
13092
+ value = getQsParameterByName$1(attr.toLowerCase());
13046
13093
  if (value === 'true') value = true;
13047
13094
  if (value === 'false') value = false;
13048
13095
  if (attr.toLowerCase() === 'autopilot' && value === '') value = true;
@@ -13065,8 +13112,8 @@
13065
13112
  callback(err, t);
13066
13113
  }
13067
13114
 
13068
- if (!options.backend.apiKey && getParameterByName('apikey')) {
13069
- options.backend.apiKey = getParameterByName('apikey');
13115
+ if (!options.backend.apiKey && getQsParameterByName$1('apikey')) {
13116
+ options.backend.apiKey = getQsParameterByName$1('apikey');
13070
13117
  }
13071
13118
 
13072
13119
  if (!options.backend.autoPilot || options.backend.autoPilot === 'false') return originalInit.call(i18next$1, _objectSpread2(_objectSpread2({}, options), enforce), handleI18nextInitialized);