gatsby-plugin-react-i18next
Version:
Easily translate your Gatsby website into multiple languages
108 lines (105 loc) • 4.66 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.onCreateNode = void 0;
exports.unstable_shouldOnCreateNode = unstable_shouldOnCreateNode;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
function unstable_shouldOnCreateNode(_ref) {
var node = _ref.node;
// We only care about JSON content.
return node.internal.mediaType === "application/json";
}
var onCreateNode = /*#__PURE__*/function () {
var _ref4 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(_ref2, _ref3) {
var node, actions, loadNodeContent, createNodeId, createContentDigest, reporter, _ref3$localeJsonSourc, localeJsonSourceName, _ref3$verbose, verbose, absolutePath, type, sourceInstanceName, relativeDirectory, name, id, activity, language, content, data, hint, createNode, createParentChildLink, localeNode;
return _regenerator.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
node = _ref2.node, actions = _ref2.actions, loadNodeContent = _ref2.loadNodeContent, createNodeId = _ref2.createNodeId, createContentDigest = _ref2.createContentDigest, reporter = _ref2.reporter;
_ref3$localeJsonSourc = _ref3.localeJsonSourceName, localeJsonSourceName = _ref3$localeJsonSourc === void 0 ? 'locale' : _ref3$localeJsonSourc, _ref3$verbose = _ref3.verbose, verbose = _ref3$verbose === void 0 ? true : _ref3$verbose;
if (unstable_shouldOnCreateNode({
node: node
})) {
_context.next = 4;
break;
}
return _context.abrupt("return");
case 4:
absolutePath = node.absolutePath, type = node.internal.type, sourceInstanceName = node.sourceInstanceName, relativeDirectory = node.relativeDirectory, name = node.name, id = node.id; // Currently only support file resources
if (!(type !== 'File')) {
_context.next = 7;
break;
}
return _context.abrupt("return");
case 7:
if (!(localeJsonSourceName == null)) {
_context.next = 9;
break;
}
return _context.abrupt("return");
case 9:
if (!(sourceInstanceName !== localeJsonSourceName)) {
_context.next = 11;
break;
}
return _context.abrupt("return");
case 11:
if (verbose) {
activity = reporter.activityTimer("gatsby-plugin-react-i18next: create node: " + relativeDirectory + "/" + name);
activity.start();
}
// relativeDirectory name is language name.
language = relativeDirectory;
_context.next = 15;
return loadNodeContent(node);
case 15:
content = _context.sent;
_context.prev = 16;
data = JSON.stringify(JSON.parse(content), undefined, '');
_context.next = 24;
break;
case 20:
_context.prev = 20;
_context.t0 = _context["catch"](16);
hint = node.absolutePath ? "file " + node.absolutePath : "in node " + node.id;
throw new Error("Unable to parse JSON: " + hint);
case 24:
createNode = actions.createNode, createParentChildLink = actions.createParentChildLink;
localeNode = {
id: createNodeId(id + " >>> Locale"),
children: [],
parent: id,
internal: {
content: data,
contentDigest: createContentDigest(data),
type: "Locale"
},
language: language,
ns: name,
data: data,
fileAbsolutePath: absolutePath
};
createNode(localeNode);
// @ts-ignore
// staled issue: https://github.com/gatsbyjs/gatsby/issues/19993
createParentChildLink({
parent: node,
child: localeNode
});
if (verbose && activity) {
activity.end();
}
case 29:
case "end":
return _context.stop();
}
}
}, _callee, null, [[16, 20]]);
}));
return function onCreateNode(_x, _x2) {
return _ref4.apply(this, arguments);
};
}();
exports.onCreateNode = onCreateNode;