UNPKG

@vyxos/astro-i18next

Version:

I18next integration for Astro with dynamic namespace loading.

38 lines (33 loc) 1.34 kB
import i18next from 'i18next'; import { createRequire } from 'module'; // src/utils/helpers.ts // src/constants.ts var INTEGRATION_NAME = "@vyxos/astro-i18next"; // src/utils/helpers.ts function getConfig() { if (!i18next.options.integrationOptions) { throw new Error(`[${INTEGRATION_NAME}] Configuration object empty.`); } return JSON.parse( JSON.stringify(i18next.options.integrationOptions) ); } var isBrowser = typeof window !== "undefined"; isBrowser ? null : createRequire(import.meta.url); // src/utils/localization.ts function getLocalizedPathname(pathname, locale) { const { lng, supportedLngs } = getConfig(); const targetLocale = locale || i18next.language || ""; const localeFromPathname = pathname.split("/")[1]; let localizedPathname = pathname; if (Array.isArray(supportedLngs) && supportedLngs.includes(localeFromPathname)) { localizedPathname = localizedPathname.replace("/" + localeFromPathname, "") || "/"; } if (Array.isArray(supportedLngs) && supportedLngs.includes(targetLocale) && (lng === void 0 || targetLocale !== lng && lng !== "cimode")) { localizedPathname = "/" + targetLocale + localizedPathname.replace(/^\/$/, ""); } return localizedPathname; } export { getConfig, getLocalizedPathname }; //# sourceMappingURL=utils.js.map //# sourceMappingURL=utils.js.map