@vyxos/astro-i18next
Version:
I18next integration for Astro with dynamic namespace loading.
1 lines • 4.5 kB
Source Map (JSON)
{"version":3,"sources":["../src/utils/config.ts","../src/utils/localization.ts"],"names":[],"mappings":";;;AAmBO,SAAS,gBAAA,GAAuC;AAErD,EAAA,IAAI,OAAO,MAAA,KAAW,WAAA,IAAe,MAAA,CAAO,iBAAA,EAAmB;AAC7D,IAAA,OAAO,MAAA,CAAO,iBAAA;AAAA,EAChB;AAGA,EAAA,IAAI,OAAO,UAAA,KAAe,WAAA,IAAe,UAAA,CAAW,iBAAA,EAAmB;AACrE,IAAA,OAAO,UAAA,CAAW,iBAAA;AAAA,EACpB;AAGA,EAAA,MAAM,IAAI,KAAA;AAAA,IACR;AAAA,GACF;AACF;;;ACTO,SAAS,oBAAA,CAAqB,QAAA,EAAkB,MAAA,GAAiB,EAAA,EAAI;AAC1E,EAAA,MAAM,EAAE,aAAA,EAAe,OAAA,EAAQ,GAAI,gBAAA,EAAiB;AACpD,EAAA,MAAM,kBAAA,GAAqB,QAAA,CAAS,KAAA,CAAM,GAAG,EAAE,CAAC,CAAA;AAChD,EAAA,IAAI,iBAAA,GAAoB,QAAA;AAGxB,EAAA,IAAI,OAAA,CAAQ,QAAA,CAAS,kBAAkB,CAAA,EAAG;AACxC,IAAA,iBAAA,GACE,iBAAA,CAAkB,OAAA,CAAQ,GAAA,GAAM,kBAAA,EAAoB,EAAE,CAAA,IAAK,GAAA;AAAA,EAC/D;AAIA,EAAA,IAAI,OAAA,CAAQ,QAAA,CAAS,MAAM,CAAA,IAAK,WAAW,aAAA,EAAe;AACxD,IAAA,iBAAA,GAAoB,GAAA,GAAM,MAAA,GAAS,iBAAA,CAAkB,OAAA,CAAQ,QAAQ,EAAE,CAAA;AAAA,EACzE;AAEA,EAAA,OAAO,iBAAA;AACT","file":"utils.cjs","sourcesContent":["import { IntegrationOptions } from \"../types\";\n\n/**\n * Retrieves the current locale configuration for the astro-i18next integration.\n *\n * This function provides access to the locale configuration in both SSG (Static Site Generation)\n * and SPA (Single Page Application) modes. The configuration is automatically injected during\n * the build process and stored globally for runtime access.\n *\n * @returns {LocaleConfig} The locale configuration object containing:\n * - `locales`: Array of all supported locale codes (e.g., ['en', 'fr', 'es'])\n * - `defaultLocale`: The default locale code (e.g., 'en')\n * - `prefixDefaultLocale`: Whether to include the default locale in URLs\n *\n * @throws {Error} Throws an error if the configuration is not available, which typically\n * indicates that the astro-i18next integration is not properly configured.\n *\n * @since 0.1.5\n */\nexport function getConfigOptions(): IntegrationOptions {\n // Try browser environment first\n if (typeof window !== \"undefined\" && window.__astroI18nConfig) {\n return window.__astroI18nConfig;\n }\n\n // Try Node.js global environment\n if (typeof globalThis !== \"undefined\" && globalThis.__astroI18nConfig) {\n return globalThis.__astroI18nConfig;\n }\n\n // Fallback - this should not happen in a properly configured setup\n throw new Error(\n \"[astro-i18next] Configuration not available. Make sure the integration is properly configured.\"\n );\n}\n","import { getConfigOptions } from \"./config\";\n\n/**\n * Generates a localized URL pathname based on a given path and target locale.\n *\n * This function ensures that the generated pathname correctly reflects the desired locale\n * according to the rules defined in the configuration. It first strips any existing locale\n * from the input pathname to get a base path. Then, it prepends the target locale to the\n * base path, but only if the target locale is not the default locale.\n *\n * @param {string} [pathname] - The original pathname to be localized. This can be a path\n * that already contains a locale prefix (e.g., \"/en/about\") or a path without one\n * (e.g., \"/about\"). Defaults to an empty string.\n *\n * @param {string} [locale=\"\"] - The target locale code for the new pathname (e.g., \"fr\").\n * If this locale is the default locale, it will not be added to the path.\n * Defaults to an empty string.\n *\n * @returns {string} The fully resolved, localized pathname. For example:\n * - `getLocalizedPathname(\"/fr/about\", \"de\")` returns `\"/de/about\"`\n * - `getLocalizedPathname(\"/about\", \"fr\")` returns `\"/fr/about\"`\n * - `getLocalizedPathname(\"/fr/about\", \"en\")` returns `\"/about\"` (if \"en\" is default)\n *\n * @since 0.1.5\n */\nexport function getLocalizedPathname(pathname: string, locale: string = \"\") {\n const { defaultLocale, locales } = getConfigOptions();\n const localeFromPathname = pathname.split(\"/\")[1];\n let localizedPathname = pathname;\n\n // 1. Strip any existing locale from the pathname to get a clean, unlocalized path.\n if (locales.includes(localeFromPathname)) {\n localizedPathname =\n localizedPathname.replace(\"/\" + localeFromPathname, \"\") || \"/\";\n }\n\n // 2. Add the target locale as a prefix, but only if it's a valid,\n // non-default locale.\n if (locales.includes(locale) && locale !== defaultLocale) {\n localizedPathname = \"/\" + locale + localizedPathname.replace(/^\\/$/, \"\");\n }\n\n return localizedPathname;\n}\n"]}