@intlayer/core
Version:
Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.
1 lines • 2.43 kB
Source Map (JSON)
{"version":3,"sources":["../../../src/localization/getLocalizedUrl.ts"],"sourcesContent":["import type { LocalesValues } from '@intlayer/config/client';\nimport configuration from '@intlayer/config/built';\n\nimport { getMultilingualUrls } from './getMultilingualUrls';\n\n/**\n * Generate URL by prefixing the given URL with the referenced locale.\n * Handles both absolute and relative URLs appropriately.\n *\n * This function get the locales, default locale, and prefix default from the configuration if not provided.\n *\n * Example:\n *\n * ```ts\n * getCurrentUrl('/about', currentLocale, ['en', 'fr'], 'en', false);\n * // Returns '/fr/about' for the French locale\n * // Returns '/about' for the default locale\n * // Returns '/about' for the Italian locale\n *\n * getCurrentUrl('https://example.com/about', currentLocale, ['en', 'fr'], 'en', false);\n * // Returns 'https://example.com/fr/about' for the French locale\n * // Returns 'https://example.com/about' for the default locale\n * // Returns 'https://example.com/about' for the Italian locale\n * ```\n *\n * @param url - The original URL string to be prefixed with locales.\n * @param currentLocale - The current locale.\n * @param locales - Optional array of supported locales. Defaults to `localesDefault`.\n * @param defaultLocale - The default locale. Defaults to `defaultLocaleDefault`.\n * @param prefixDefault - Whether to prefix the default locale. Defaults to `prefixDefaultDefault`.\n * @returns An object mapping each locale to its corresponding multilingual URL.\n */\nexport const getLocalizedUrl = (\n url: string,\n currentLocale: LocalesValues,\n locales: LocalesValues[] = configuration.internationalization.locales,\n defaultLocale: LocalesValues = configuration.internationalization\n .defaultLocale,\n prefixDefault: boolean = configuration.middleware.prefixDefault\n): string => {\n // Remove any existing locale segment from the URL\n const urlWithoutLocale = getMultilingualUrls(\n url,\n locales,\n defaultLocale,\n prefixDefault\n );\n\n return urlWithoutLocale[\n currentLocale as unknown as keyof typeof urlWithoutLocale\n ];\n};\n"],"mappings":"AACA,OAAO,mBAAmB;AAE1B,SAAS,2BAA2B;AA6B7B,MAAM,kBAAkB,CAC7B,KACA,eACA,UAA2B,cAAc,qBAAqB,SAC9D,gBAA+B,cAAc,qBAC1C,eACH,gBAAyB,cAAc,WAAW,kBACvC;AAEX,QAAM,mBAAmB;AAAA,IACvB;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AAEA,SAAO,iBACL,aACF;AACF;","names":[]}