next-translate-routes-multi-domain
Version:
Flexible and translated routes for Next.js without custom server
86 lines • 3.33 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Link = void 0;
var link_1 = __importDefault(require("next/link"));
var router_1 = require("next/router");
var react_1 = __importDefault(require("react"));
var fileUrlToUrl_1 = require("./fileUrlToUrl");
var getLocale_1 = require("./getLocale");
var ntrData_1 = require("./ntrData");
var removeLangPrefix_1 = require("./removeLangPrefix");
var urlToFileUrl_1 = require("./urlToFileUrl");
/**
* Link component that handle route translations
*/
var Link = function (_a) {
var href = _a.href, as = _a.as, propLocale = _a.locale, props = __rest(_a, ["href", "as", "locale"]);
var router = (0, router_1.useRouter)();
var locale = (0, getLocale_1.getLocale)(router, propLocale);
var locales = router.locales || (0, ntrData_1.getNtrData)().locales;
var unPrefixedHref = typeof href === 'string' ? (0, removeLangPrefix_1.removeLangPrefix)(href) : href;
if (!propLocale && typeof href === 'string' && unPrefixedHref !== href) {
var hrefLocale = href.split('/')[1];
if (hrefLocale && locales.includes(hrefLocale)) {
locale = hrefLocale;
}
}
var translatedUrl;
var parsedUrl;
/**
* Href can be:
* - an external url
* - a correct file url
* - a wrong file url (not matching any page)
* - a correct translated url
* - a wrong translated url
*/
try {
translatedUrl = (0, fileUrlToUrl_1.fileUrlToUrl)(unPrefixedHref, locale);
// Href is a correct file url
parsedUrl = unPrefixedHref;
}
catch (_b) {
parsedUrl = (0, urlToFileUrl_1.urlToFileUrl)(unPrefixedHref, locale);
if (parsedUrl) {
try {
translatedUrl = (0, fileUrlToUrl_1.fileUrlToUrl)(parsedUrl, locale);
// Href is a correct translated url
}
catch (_c) {
// Href is a wrong file url or an external url
}
}
else {
// Href is a wrong url or an external url
}
}
return react_1.default.createElement(link_1.default, __assign({ href: parsedUrl || href, as: as || translatedUrl, locale: locale }, props));
};
exports.Link = Link;
exports.default = exports.Link;
//# sourceMappingURL=link.js.map