locize 3.0.5 → 3.1.0
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 +4 -0
- package/README.md +67 -31
- package/dist/cjs/api/handleConfirmInitialized.js +1 -0
- package/dist/cjs/index.d.ts +30 -0
- package/dist/cjs/index.js +2 -0
- package/dist/cjs/locizePlugin.js +103 -77
- package/dist/cjs/observer.js +28 -1
- package/dist/cjs/utils.js +11 -0
- package/dist/esm/api/handleConfirmInitialized.js +1 -0
- package/dist/esm/index.d.ts +30 -0
- package/dist/esm/index.js +3 -2
- package/dist/esm/locizePlugin.js +103 -78
- package/dist/esm/observer.js +28 -1
- package/dist/esm/utils.js +11 -1
- package/dist/umd/locize.js +142 -78
- package/dist/umd/locize.min.js +1 -1
- package/index.d.ts +30 -0
- package/locize.js +142 -78
- package/locize.min.js +1 -1
- package/package.json +1 -1
- package/src/api/handleConfirmInitialized.js +1 -0
- package/src/index.js +3 -1
- package/src/locizePlugin.js +123 -85
- package/src/observer.js +48 -1
- package/src/ui/utils.js +5 -5
- package/src/utils.js +12 -0
package/locize.js
CHANGED
|
@@ -493,6 +493,7 @@
|
|
|
493
493
|
function handler$6(payload) {
|
|
494
494
|
api.initialized = true;
|
|
495
495
|
clearInterval(api.initInterval);
|
|
496
|
+
delete api.initInterval;
|
|
496
497
|
api.sendCurrentParsedContent();
|
|
497
498
|
api.sendCurrentTargetLanguage();
|
|
498
499
|
}
|
|
@@ -2398,8 +2399,29 @@
|
|
|
2398
2399
|
find(el);
|
|
2399
2400
|
return found;
|
|
2400
2401
|
}
|
|
2402
|
+
function getQsParameterByName(name, url) {
|
|
2403
|
+
if (typeof window === 'undefined') return null;
|
|
2404
|
+
if (!url) url = window.location.href.toLowerCase();
|
|
2405
|
+
name = name.replace(/[\[\]]/g, '\\$&');
|
|
2406
|
+
var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)');
|
|
2407
|
+
var results = regex.exec(url);
|
|
2408
|
+
if (!results) return null;
|
|
2409
|
+
if (!results[2]) return '';
|
|
2410
|
+
return decodeURIComponent(results[2].replace(/\+/g, ' '));
|
|
2411
|
+
}
|
|
2401
2412
|
|
|
2413
|
+
var mutationTriggeringElements = {};
|
|
2402
2414
|
function ignoreMutation(ele) {
|
|
2415
|
+
if (ele.uniqueID) {
|
|
2416
|
+
var info = mutationTriggeringElements[ele.uniqueID];
|
|
2417
|
+
if (info && info.triggered > 10 && info.lastTriggerDate + 500 < Date.now()) {
|
|
2418
|
+
if (!info.warned && console) {
|
|
2419
|
+
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);
|
|
2420
|
+
info.warned = true;
|
|
2421
|
+
}
|
|
2422
|
+
return true;
|
|
2423
|
+
}
|
|
2424
|
+
}
|
|
2403
2425
|
var ret = ele.dataset && (ele.dataset.i18nextEditorElement === 'true' || ele.dataset.locizeEditorIgnore === 'true');
|
|
2404
2426
|
if (!ret && ele.parentElement) return ignoreMutation(ele.parentElement);
|
|
2405
2427
|
return ret;
|
|
@@ -2428,6 +2450,12 @@
|
|
|
2428
2450
|
if (mutation.type === 'attributes' && !validAttributes.includes(mutation.attributeName)) {
|
|
2429
2451
|
return;
|
|
2430
2452
|
}
|
|
2453
|
+
Object.keys(mutationTriggeringElements).forEach(function (k) {
|
|
2454
|
+
var info = mutationTriggeringElements[k];
|
|
2455
|
+
if (info.lastTriggerDate + 60000 < Date.now()) {
|
|
2456
|
+
delete mutationTriggeringElements[k];
|
|
2457
|
+
}
|
|
2458
|
+
});
|
|
2431
2459
|
if (mutation.type === 'childList') {
|
|
2432
2460
|
var notOurs = 0;
|
|
2433
2461
|
if (!ignoreMutation(mutation.target)) {
|
|
@@ -2443,8 +2471,18 @@
|
|
|
2443
2471
|
if (notOurs === 0) return;
|
|
2444
2472
|
}
|
|
2445
2473
|
triggerMutation = true;
|
|
2474
|
+
if (mutation.target && mutation.target.uniqueID) {
|
|
2475
|
+
var info = mutationTriggeringElements[mutation.target.uniqueID] || {
|
|
2476
|
+
triggered: 0
|
|
2477
|
+
};
|
|
2478
|
+
info.triggered = info.triggered + 1;
|
|
2479
|
+
info.lastTriggerDate = Date.now();
|
|
2480
|
+
mutationTriggeringElements[mutation.target.uniqueID] = info;
|
|
2481
|
+
}
|
|
2446
2482
|
var includedAlready = targetEles.reduce(function (mem, element) {
|
|
2447
|
-
if (mem || element.contains(mutation.target) || !mutation.target.parentElement)
|
|
2483
|
+
if (mem || element.contains(mutation.target) || !mutation.target.parentElement) {
|
|
2484
|
+
return true;
|
|
2485
|
+
}
|
|
2448
2486
|
return false;
|
|
2449
2487
|
}, false);
|
|
2450
2488
|
if (!includedAlready) {
|
|
@@ -2862,89 +2900,94 @@
|
|
|
2862
2900
|
try {
|
|
2863
2901
|
isInIframe = self !== top;
|
|
2864
2902
|
} catch (e) {}
|
|
2903
|
+
function configurePostProcessor(i18next, options) {
|
|
2904
|
+
i18next.use(SubliminalPostProcessor);
|
|
2905
|
+
if (typeof options.postProcess === 'string') {
|
|
2906
|
+
options.postProcess = [options.postProcess, 'subliminal'];
|
|
2907
|
+
} else if (Array.isArray(options.postProcess)) {
|
|
2908
|
+
options.postProcess.push('subliminal');
|
|
2909
|
+
} else {
|
|
2910
|
+
options.postProcess = 'subliminal';
|
|
2911
|
+
}
|
|
2912
|
+
options.postProcessPassResolved = true;
|
|
2913
|
+
}
|
|
2914
|
+
function getImplementation(i18n) {
|
|
2915
|
+
var impl = {
|
|
2916
|
+
getResource: function getResource(lng, ns, key) {
|
|
2917
|
+
return i18n.getResource(lng, ns, key);
|
|
2918
|
+
},
|
|
2919
|
+
setResource: function setResource(lng, ns, key, value) {
|
|
2920
|
+
return i18n.addResource(lng, ns, key, value, {
|
|
2921
|
+
silent: true
|
|
2922
|
+
});
|
|
2923
|
+
},
|
|
2924
|
+
getResourceBundle: function getResourceBundle(lng, ns, cb) {
|
|
2925
|
+
i18n.loadNamespaces(ns, function () {
|
|
2926
|
+
cb(i18n.getResourceBundle(lng, ns));
|
|
2927
|
+
});
|
|
2928
|
+
},
|
|
2929
|
+
getLng: function getLng() {
|
|
2930
|
+
return i18n.languages[0];
|
|
2931
|
+
},
|
|
2932
|
+
getSourceLng: function getSourceLng() {
|
|
2933
|
+
var fallback = i18n.options.fallbackLng;
|
|
2934
|
+
if (typeof fallback === 'string') return fallback;
|
|
2935
|
+
if (Array.isArray(fallback)) return fallback[fallback.length - 1];
|
|
2936
|
+
if (fallback && fallback["default"]) {
|
|
2937
|
+
if (typeof fallback["default"] === 'string') return fallback;
|
|
2938
|
+
if (Array.isArray(fallback["default"])) return fallback["default"][fallback["default"].length - 1];
|
|
2939
|
+
}
|
|
2940
|
+
if (typeof fallback === 'function') {
|
|
2941
|
+
var res = fallback(i18n.resolvedLanguage);
|
|
2942
|
+
if (typeof res === 'string') return res;
|
|
2943
|
+
if (Array.isArray(res)) return res[res.length - 1];
|
|
2944
|
+
}
|
|
2945
|
+
return 'dev';
|
|
2946
|
+
},
|
|
2947
|
+
getLocizeDetails: function getLocizeDetails() {
|
|
2948
|
+
var backendName;
|
|
2949
|
+
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) {
|
|
2950
|
+
backendName = 'I18NextLocizeBackend';
|
|
2951
|
+
} else {
|
|
2952
|
+
backendName = i18n.services.backendConnector.backend ? i18n.services.backendConnector.backend.constructor.name : 'options.resources';
|
|
2953
|
+
}
|
|
2954
|
+
var opts = {
|
|
2955
|
+
backendName: backendName,
|
|
2956
|
+
sourceLng: impl.getSourceLng(),
|
|
2957
|
+
i18nFormat: i18n.options.compatibilityJSON === 'v3' ? 'i18next_v3' : 'i18next_v4',
|
|
2958
|
+
i18nFramework: 'i18next',
|
|
2959
|
+
isLocizify: i18n.options.isLocizify,
|
|
2960
|
+
defaultNS: i18n.options.defaultNS
|
|
2961
|
+
};
|
|
2962
|
+
if (!i18n.options.backend && !i18n.options.editor) return opts;
|
|
2963
|
+
var pickFrom = i18n.options.backend || i18n.options.editor;
|
|
2964
|
+
return _objectSpread(_objectSpread({}, opts), {}, {
|
|
2965
|
+
projectId: pickFrom.projectId,
|
|
2966
|
+
version: pickFrom.version
|
|
2967
|
+
});
|
|
2968
|
+
},
|
|
2969
|
+
bindLanguageChange: function bindLanguageChange(cb) {
|
|
2970
|
+
i18n.on('languageChanged', cb);
|
|
2971
|
+
},
|
|
2972
|
+
bindMissingKeyHandler: function bindMissingKeyHandler(cb) {
|
|
2973
|
+
i18n.options.missingKeyHandler = function (lng, ns, k, val, isUpdate, opts) {
|
|
2974
|
+
if (!isUpdate) cb(lng, ns, k, val);
|
|
2975
|
+
};
|
|
2976
|
+
},
|
|
2977
|
+
triggerRerender: function triggerRerender() {
|
|
2978
|
+
i18n.emit('editorSaved');
|
|
2979
|
+
}
|
|
2980
|
+
};
|
|
2981
|
+
return impl;
|
|
2982
|
+
}
|
|
2865
2983
|
var i18next;
|
|
2866
2984
|
var locizePlugin = {
|
|
2867
2985
|
type: '3rdParty',
|
|
2868
2986
|
init: function init(i18n) {
|
|
2869
2987
|
var options = i18n.options;
|
|
2870
2988
|
i18next = i18n;
|
|
2871
|
-
if (!isInIframe)
|
|
2872
|
-
|
|
2873
|
-
if (typeof options.postProcess === 'string') {
|
|
2874
|
-
options.postProcess = [options.postProcess, 'subliminal'];
|
|
2875
|
-
} else if (Array.isArray(options.postProcess)) {
|
|
2876
|
-
options.postProcess.push('subliminal');
|
|
2877
|
-
} else {
|
|
2878
|
-
options.postProcess = 'subliminal';
|
|
2879
|
-
}
|
|
2880
|
-
options.postProcessPassResolved = true;
|
|
2881
|
-
}
|
|
2882
|
-
var impl = {
|
|
2883
|
-
getResource: function getResource(lng, ns, key) {
|
|
2884
|
-
return i18n.getResource(lng, ns, key);
|
|
2885
|
-
},
|
|
2886
|
-
setResource: function setResource(lng, ns, key, value) {
|
|
2887
|
-
return i18n.addResource(lng, ns, key, value, {
|
|
2888
|
-
silent: true
|
|
2889
|
-
});
|
|
2890
|
-
},
|
|
2891
|
-
getResourceBundle: function getResourceBundle(lng, ns, cb) {
|
|
2892
|
-
i18n.loadNamespaces(ns, function () {
|
|
2893
|
-
cb(i18n.getResourceBundle(lng, ns));
|
|
2894
|
-
});
|
|
2895
|
-
},
|
|
2896
|
-
getLng: function getLng() {
|
|
2897
|
-
return i18n.languages[0];
|
|
2898
|
-
},
|
|
2899
|
-
getSourceLng: function getSourceLng() {
|
|
2900
|
-
var fallback = i18n.options.fallbackLng;
|
|
2901
|
-
if (typeof fallback === 'string') return fallback;
|
|
2902
|
-
if (Array.isArray(fallback)) return fallback[fallback.length - 1];
|
|
2903
|
-
if (fallback && fallback["default"]) {
|
|
2904
|
-
if (typeof fallback["default"] === 'string') return fallback;
|
|
2905
|
-
if (Array.isArray(fallback["default"])) return fallback["default"][fallback["default"].length - 1];
|
|
2906
|
-
}
|
|
2907
|
-
if (typeof fallback === 'function') {
|
|
2908
|
-
var res = fallback(i18n.resolvedLanguage);
|
|
2909
|
-
if (typeof res === 'string') return res;
|
|
2910
|
-
if (Array.isArray(res)) return res[res.length - 1];
|
|
2911
|
-
}
|
|
2912
|
-
return 'dev';
|
|
2913
|
-
},
|
|
2914
|
-
getLocizeDetails: function getLocizeDetails() {
|
|
2915
|
-
var backendName;
|
|
2916
|
-
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) {
|
|
2917
|
-
backendName = 'I18NextLocizeBackend';
|
|
2918
|
-
} else {
|
|
2919
|
-
backendName = i18n.services.backendConnector.backend ? i18n.services.backendConnector.backend.constructor.name : 'options.resources';
|
|
2920
|
-
}
|
|
2921
|
-
var opts = {
|
|
2922
|
-
backendName: backendName,
|
|
2923
|
-
sourceLng: impl.getSourceLng(),
|
|
2924
|
-
i18nFormat: i18n.options.compatibilityJSON === 'v3' ? 'i18next_v3' : 'i18next_v4',
|
|
2925
|
-
i18nFramework: 'i18next',
|
|
2926
|
-
isLocizify: i18n.options.isLocizify,
|
|
2927
|
-
defaultNS: i18n.options.defaultNS
|
|
2928
|
-
};
|
|
2929
|
-
if (!i18n.options.backend && !i18n.options.editor) return opts;
|
|
2930
|
-
var pickFrom = i18n.options.backend || i18n.options.editor;
|
|
2931
|
-
return _objectSpread(_objectSpread({}, opts), {}, {
|
|
2932
|
-
projectId: pickFrom.projectId,
|
|
2933
|
-
version: pickFrom.version
|
|
2934
|
-
});
|
|
2935
|
-
},
|
|
2936
|
-
bindLanguageChange: function bindLanguageChange(cb) {
|
|
2937
|
-
i18n.on('languageChanged', cb);
|
|
2938
|
-
},
|
|
2939
|
-
bindMissingKeyHandler: function bindMissingKeyHandler(cb) {
|
|
2940
|
-
i18n.options.missingKeyHandler = function (lng, ns, k, val, isUpdate, opts) {
|
|
2941
|
-
if (!isUpdate) cb(lng, ns, k, val);
|
|
2942
|
-
};
|
|
2943
|
-
},
|
|
2944
|
-
triggerRerender: function triggerRerender() {
|
|
2945
|
-
i18n.emit('editorSaved');
|
|
2946
|
-
}
|
|
2947
|
-
};
|
|
2989
|
+
if (!isInIframe) configurePostProcessor(i18next, options);
|
|
2990
|
+
var impl = getImplementation(i18n);
|
|
2948
2991
|
if (!isInIframe) {
|
|
2949
2992
|
start(impl);
|
|
2950
2993
|
} else {
|
|
@@ -2952,6 +2995,25 @@
|
|
|
2952
2995
|
}
|
|
2953
2996
|
}
|
|
2954
2997
|
};
|
|
2998
|
+
var locizeEditorPlugin = function locizeEditorPlugin() {
|
|
2999
|
+
var opt = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
3000
|
+
opt.qsProp = opt.qsProp || 'incontext';
|
|
3001
|
+
return {
|
|
3002
|
+
type: '3rdParty',
|
|
3003
|
+
init: function init(i18n) {
|
|
3004
|
+
var options = i18n.options;
|
|
3005
|
+
i18next = i18n;
|
|
3006
|
+
var showInContext = getQsParameterByName(opt.qsProp) === 'true';
|
|
3007
|
+
if (!isInIframe && showInContext) configurePostProcessor(i18next, options);
|
|
3008
|
+
var impl = getImplementation(i18n);
|
|
3009
|
+
if (!isInIframe && showInContext) {
|
|
3010
|
+
start(impl);
|
|
3011
|
+
} else {
|
|
3012
|
+
startLegacy(impl);
|
|
3013
|
+
}
|
|
3014
|
+
}
|
|
3015
|
+
};
|
|
3016
|
+
};
|
|
2955
3017
|
|
|
2956
3018
|
function startStandalone() {
|
|
2957
3019
|
startLegacy({
|
|
@@ -2977,6 +3039,7 @@
|
|
|
2977
3039
|
PostProcessor: SubliminalPostProcessor,
|
|
2978
3040
|
addLocizeSavedHandler: addLocizeSavedHandler,
|
|
2979
3041
|
locizePlugin: locizePlugin,
|
|
3042
|
+
locizeEditorPlugin: locizeEditorPlugin,
|
|
2980
3043
|
turnOn: turnOn,
|
|
2981
3044
|
turnOff: turnOff,
|
|
2982
3045
|
setEditorLng: setEditorLng,
|
|
@@ -2987,6 +3050,7 @@
|
|
|
2987
3050
|
exports.addLocizeSavedHandler = addLocizeSavedHandler;
|
|
2988
3051
|
exports.containsHiddenMeta = containsHiddenMeta;
|
|
2989
3052
|
exports["default"] = index;
|
|
3053
|
+
exports.locizeEditorPlugin = locizeEditorPlugin;
|
|
2990
3054
|
exports.locizePlugin = locizePlugin;
|
|
2991
3055
|
exports.setEditorLng = setEditorLng;
|
|
2992
3056
|
exports.startStandalone = startStandalone;
|