Skip to content

Commit

Permalink
Prepare for 0.1.5 release
Browse files Browse the repository at this point in the history
  • Loading branch information
mthh committed Mar 28, 2023
1 parent c793834 commit c455428
Show file tree
Hide file tree
Showing 11 changed files with 6,369 additions and 330 deletions.
1 change: 1 addition & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"plugins": ["@babel/plugin-proposal-optional-chaining"],
"env": {
"development": {
"presets": [
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const localize = locI18next.init(i18next, {
optionsAttr: 'i18n-options',
useOptionsAttr: false,
parseDefaultValueFromContent: true
document: window.document,
});
```
*Using default values :*
Expand Down
221 changes: 120 additions & 101 deletions dist/commonjs/main.js
Original file line number Diff line number Diff line change
@@ -1,122 +1,141 @@
'use strict';
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
value: true
});
exports["default"] = void 0;

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var defaults = {
selectorAttr: 'data-i18n',
targetAttr: 'i18n-target',
optionsAttr: 'i18n-options',
useOptionsAttr: false,
parseDefaultValueFromContent: true,
document: document
selectorAttr: 'data-i18n',
targetAttr: 'i18n-target',
optionsAttr: 'i18n-options',
useOptionsAttr: false,
parseDefaultValueFromContent: true,
document: document
};

function init(i18next) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

options = _extends({}, defaults, options);
var extendDefault = function extendDefault(o, val) {
return options.parseDefaultValueFromContent ? _extends({}, o, { defaultValue: val }) : o;
};
function parse(elem, key, opts) {
var attr = 'text';

if (key.indexOf('[') == 0) {
var parts = key.split(']');
key = parts[1];
attr = parts[0].substr(1, parts[0].length - 1);
}

key = key.indexOf(';') == key.length - 1 ? key.substr(0, key.length - 2) : key;

if (attr === 'html') {
elem.innerHTML = i18next.t(key, extendDefault(opts, elem.innerHTML));
} else if (attr === 'text') {
elem.textContent = i18next.t(key, extendDefault(opts, elem.textContent));
} else if (attr === 'prepend') {
var startIdx = elem.innerHTML.indexOf('<loc-i18n>');
var endIdx = elem.innerHTML.indexOf('</loc-i18n>') + 11;
if (startIdx > -1 && endIdx > 6) {
elem.innerHTML = [elem.innerHTML.substring(0, startIdx), elem.innerHTML.slice(endIdx)].join('');
}
elem.innerHTML = ['<loc-i18n>', i18next.t(key, extendDefault(opts, elem.innerHTML)), '</loc-i18n>', elem.innerHTML].join('');
} else if (attr === 'append') {
var _startIdx = elem.innerHTML.indexOf('<loc-i18n>');
var _endIdx = elem.innerHTML.indexOf('</loc-i18n>') + 11;
if (_startIdx > -1 && _endIdx > 6) {
elem.innerHTML = [elem.innerHTML.substring(0, _startIdx), elem.innerHTML.slice(_endIdx)].join('');
}
elem.innerHTML = [elem.innerHTML, '<loc-i18n>', i18next.t(key, extendDefault(opts, elem.innerHTML), '</loc-i18n>')].join('');
} else if (attr.indexOf('data-') === 0) {
var dataAttr = attr.substr('data-'.length);
var translated = i18next.t(key, extendDefault(opts, elem.getAttribute(dataAttr)));
// we change into the data cache
elem.setAttribute(dataAttr, translated);
// we change into the dom
elem.setAttribute(attr, translated);
} else {
elem.setAttribute(attr, i18next.t(key, extendDefault(opts, elem.getAttribute(attr))));
}
};

function relaxedJsonParse(badJSON) {
return JSON.parse(badJSON.replace(/:\s*"([^"]*)"/g, function (match, p1) {
return ': "' + p1.replace(/:/g, '@colon@') + '"';
}).replace(/:\s*'([^']*)'/g, function (match, p1) {
return ': "' + p1.replace(/:/g, '@colon@') + '"';
}).replace(/(['"])?([a-z0-9A-Z_]+)(['"])?\s*:/g, '"$2": ').replace(/@colon@/g, ':'));
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
options = _objectSpread({}, defaults, options);

var extendDefault = function extendDefault(o, val) {
return options.parseDefaultValueFromContent ? _objectSpread({}, o, {
defaultValue: val
}) : o;
};

function parse(elem, key, opts) {
var attr = 'text';

if (key.indexOf('[') == 0) {
var parts = key.split(']');
key = parts[1];
attr = parts[0].substr(1, parts[0].length - 1);
}

function _loc(elem, opts) {
var key = elem.getAttribute(options.selectorAttr);
// if (!key && typeof key !== 'undefined' && key !== false)
// key = elem.textContent || elem.innerHTML;
if (!key) return;
key = key.indexOf(';') == key.length - 1 ? key.substr(0, key.length - 2) : key;

if (attr === 'html') {
elem.innerHTML = i18next.t(key, extendDefault(opts, elem.innerHTML));
} else if (attr === 'text') {
elem.textContent = i18next.t(key, extendDefault(opts, elem.textContent));
} else if (attr === 'prepend') {
var startIdx = elem.innerHTML.indexOf('<loc-i18n>');
var endIdx = elem.innerHTML.indexOf('</loc-i18n>') + 11;

if (startIdx > -1 && endIdx > 6) {
elem.innerHTML = [elem.innerHTML.substring(0, startIdx), elem.innerHTML.slice(endIdx)].join('');
}

elem.innerHTML = ['<loc-i18n>', i18next.t(key, extendDefault(opts, elem.innerHTML)), '</loc-i18n>', elem.innerHTML].join('');
} else if (attr === 'append') {
var _startIdx = elem.innerHTML.indexOf('<loc-i18n>');

var _endIdx = elem.innerHTML.indexOf('</loc-i18n>') + 11;

if (_startIdx > -1 && _endIdx > 6) {
elem.innerHTML = [elem.innerHTML.substring(0, _startIdx), elem.innerHTML.slice(_endIdx)].join('');
}

var target = elem,
targetSelector = elem.getAttribute(options.targetAttr);
elem.innerHTML = [elem.innerHTML, '<loc-i18n>', i18next.t(key, extendDefault(opts, elem.innerHTML), '</loc-i18n>')].join('');
} else if (attr.indexOf('data-') === 0) {
var dataAttr = attr.substr('data-'.length);
var translated = i18next.t(key, extendDefault(opts, elem.getAttribute(dataAttr))); // we change into the data cache

if (targetSelector != null) target = elem.querySelector(targetSelector) || elem;
elem.setAttribute(dataAttr, translated); // we change into the dom

if (!opts && options.useOptionsAttr === true) opts = relaxedJsonParse(elem.getAttribute(options.optionsAttr) || '{}');
elem.setAttribute(attr, translated);
} else {
elem.setAttribute(attr, i18next.t(key, extendDefault(opts, elem.getAttribute(attr))));
}
}

;

function relaxedJsonParse(badJSON) {
return JSON.parse(badJSON.replace(/:\s*"([^"]*)"/g, function (match, p1) {
return ': "' + p1.replace(/:/g, '@colon@') + '"';
}).replace(/:\s*'([^']*)'/g, function (match, p1) {
return ': "' + p1.replace(/:/g, '@colon@') + '"';
}).replace(/(['"])?([a-z0-9A-Z_]+)(['"])?\s*:/g, '"$2": ').replace(/@colon@/g, ':'));
}

function _loc(elem, opts) {
var key = elem.getAttribute(options.selectorAttr); // if (!key && typeof key !== 'undefined' && key !== false)
// key = elem.textContent || elem.innerHTML;

if (!key) return;
var target = elem,
targetSelector = elem.getAttribute(options.targetAttr);
if (targetSelector != null) target = elem.querySelector(targetSelector) || elem;
if (!opts && options.useOptionsAttr === true) opts = relaxedJsonParse(elem.getAttribute(options.optionsAttr) || '{}');
opts = opts || {};

if (key.indexOf(';') >= 0) {
var keys = key.split(';');

for (var ix = 0, l_ix = keys.length; ix < l_ix; ix++) {
if (keys[ix] != '') parse(target, keys[ix], opts);
}
} else {
parse(target, key, opts);
}

if (options.useOptionsAttr === true) {
var clone = {};
clone = _objectSpread({
clone: clone
}, opts);
delete clone.lng;
elem.setAttribute(options.optionsAttr, JSON.stringify(clone));
}
}

function handle(selector, opts) {
var document = (opts === null || opts === void 0 ? void 0 : opts.document) || options.document;
var elems = document.querySelectorAll(selector);

opts = opts || {};
for (var i = 0; i < elems.length; i++) {
var elem = elems[i];
var childs = elem.querySelectorAll('[' + options.selectorAttr + ']');

if (key.indexOf(';') >= 0) {
var keys = key.split(';');
for (var ix = 0, l_ix = keys.length; ix < l_ix; ix++) {
if (keys[ix] != '') parse(target, keys[ix], opts);
}
} else {
parse(target, key, opts);
}
for (var j = childs.length - 1; j > -1; j--) {
_loc(childs[j], opts);
}

if (options.useOptionsAttr === true) {
var clone = {};
clone = _extends({ clone: clone }, opts);
delete clone.lng;
elem.setAttribute(options.optionsAttr, JSON.stringify(clone));
}
_loc(elem, opts);
}
}

function handle(selector, opts) {
var elems = options.document.querySelectorAll(selector);
for (var i = 0; i < elems.length; i++) {
var elem = elems[i];
var childs = elem.querySelectorAll('[' + options.selectorAttr + ']');
for (var j = childs.length - 1; j > -1; j--) {
_loc(childs[j], opts);
}
_loc(elem, opts);
}
};
return handle;
;
return handle;
}

exports.default = {
init: init
};
var _default = {
init: init
};
exports["default"] = _default;
Loading

0 comments on commit c455428

Please sign in to comment.