locize-xcstrings
Version:
xcstrings2locize and locize2xcstrings converter xcstrings utils
91 lines (90 loc) • 7.39 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = xcstrings2locize;
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); }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var checkForComment = function checkForComment(data, result, l, key) {
var keyMeta = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : '';
if (l === result.sourceLng && data.strings[key].comment) {
var _result$resources$l, _ref;
(_result$resources$l = result.resources[l])[_ref = "".concat(key).concat(keyMeta)] || (_result$resources$l[_ref] = {});
result.resources[l]["".concat(key).concat(keyMeta)].context = {
text: data.strings[key].comment
};
}
};
function xcstrings2locize(data) {
if (typeof data === 'string') data = JSON.parse(data);
var result = {
sourceLng: data.sourceLanguage,
resources: _defineProperty({}, data.sourceLanguage, {}),
version: data.version
};
var lngs = [];
var keys = Object.keys(data.strings);
keys.forEach(function (key) {
var lngsForKey = Object.keys(data.strings[key].localizations || {});
lngsForKey.forEach(function (l) {
if (lngs.indexOf(l) < 0) {
var _result$resources;
lngs.push(l);
(_result$resources = result.resources)[l] || (_result$resources[l] = {});
}
});
if (lngsForKey.length === 0) {
var _result$resources$res;
(_result$resources$res = result.resources[result.sourceLng])[key] || (_result$resources$res[key] = {});
}
lngsForKey.forEach(function (l) {
var _data$strings$key$loc, _data$strings$key$loc2, _data$strings$key$loc3, _data$strings$key$loc5, _data$strings$key$loc6;
if (((_data$strings$key$loc = data.strings[key].localizations[l]) === null || _data$strings$key$loc === void 0 || (_data$strings$key$loc = _data$strings$key$loc.stringUnit) === null || _data$strings$key$loc === void 0 ? void 0 : _data$strings$key$loc.value) !== undefined) {
var _result$resources$l2;
(_result$resources$l2 = result.resources[l])[key] || (_result$resources$l2[key] = {});
result.resources[l][key].value = data.strings[key].localizations[l].stringUnit.value;
checkForComment(data, result, l, key);
} else if ((_data$strings$key$loc2 = data.strings[key].localizations[l]) !== null && _data$strings$key$loc2 !== void 0 && _data$strings$key$loc2.variations && (_data$strings$key$loc3 = data.strings[key].localizations[l]) !== null && _data$strings$key$loc3 !== void 0 && (_data$strings$key$loc3 = _data$strings$key$loc3.variations) !== null && _data$strings$key$loc3 !== void 0 && _data$strings$key$loc3.plural) {
var pluralForms = Object.keys(data.strings[key].localizations[l].variations.plural);
pluralForms.forEach(function (pf, i) {
var _data$strings$key$loc4;
var keyMeta = "[variations.plural.".concat(pf, "]");
if (((_data$strings$key$loc4 = data.strings[key].localizations[l].variations.plural[pf]) === null || _data$strings$key$loc4 === void 0 || (_data$strings$key$loc4 = _data$strings$key$loc4.stringUnit) === null || _data$strings$key$loc4 === void 0 ? void 0 : _data$strings$key$loc4.value) !== undefined) {
var _result$resources$l3, _ref2;
(_result$resources$l3 = result.resources[l])[_ref2 = "".concat(key).concat(keyMeta)] || (_result$resources$l3[_ref2] = {});
result.resources[l]["".concat(key).concat(keyMeta)].value = data.strings[key].localizations[l].variations.plural[pf].stringUnit.value;
}
if (i === 0) checkForComment(data, result, l, key, keyMeta);
});
} else if ((_data$strings$key$loc5 = data.strings[key].localizations[l]) !== null && _data$strings$key$loc5 !== void 0 && _data$strings$key$loc5.variations && (_data$strings$key$loc6 = data.strings[key].localizations[l]) !== null && _data$strings$key$loc6 !== void 0 && (_data$strings$key$loc6 = _data$strings$key$loc6.variations) !== null && _data$strings$key$loc6 !== void 0 && _data$strings$key$loc6.device) {
var devices = Object.keys(data.strings[key].localizations[l].variations.device);
devices.forEach(function (d, i) {
var _data$strings$key$loc7, _data$strings$key$loc8;
var keyMeta = "[variations.device.".concat(d, "]");
if (((_data$strings$key$loc7 = data.strings[key].localizations[l].variations.device[d]) === null || _data$strings$key$loc7 === void 0 || (_data$strings$key$loc7 = _data$strings$key$loc7.stringUnit) === null || _data$strings$key$loc7 === void 0 ? void 0 : _data$strings$key$loc7.value) !== undefined) {
var _result$resources$l4, _ref3;
(_result$resources$l4 = result.resources[l])[_ref3 = "".concat(key).concat(keyMeta)] || (_result$resources$l4[_ref3] = {});
result.resources[l]["".concat(key).concat(keyMeta)].value = data.strings[key].localizations[l].variations.device[d].stringUnit.value;
if (i === 0) checkForComment(data, result, l, key, keyMeta);
} else if ((_data$strings$key$loc8 = data.strings[key].localizations[l].variations.device[d]) !== null && _data$strings$key$loc8 !== void 0 && (_data$strings$key$loc8 = _data$strings$key$loc8.variations) !== null && _data$strings$key$loc8 !== void 0 && _data$strings$key$loc8.plural) {
var subPluralForms = Object.keys(data.strings[key].localizations[l].variations.device[d].variations.plural);
subPluralForms.forEach(function (pf, j) {
var _data$strings$key$loc9;
var subKeyMeta = "[variations.device.".concat(d, "[variations.plural.").concat(pf, "]]");
if (((_data$strings$key$loc9 = data.strings[key].localizations[l].variations.device[d].variations.plural[pf]) === null || _data$strings$key$loc9 === void 0 || (_data$strings$key$loc9 = _data$strings$key$loc9.stringUnit) === null || _data$strings$key$loc9 === void 0 ? void 0 : _data$strings$key$loc9.value) !== undefined) {
var _result$resources$l5, _ref4;
(_result$resources$l5 = result.resources[l])[_ref4 = "".concat(key).concat(subKeyMeta)] || (_result$resources$l5[_ref4] = {});
result.resources[l]["".concat(key).concat(subKeyMeta)].value = data.strings[key].localizations[l].variations.device[d].variations.plural[pf].stringUnit.value;
}
if (i === 0 && j === 0) checkForComment(data, result, l, key, subKeyMeta);
});
}
});
}
});
});
return result;
}
module.exports = exports.default;