UNPKG

@intlayer/core

Version:

Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.

77 lines (75 loc) 2.98 kB
import { internationalization, routing } from "@intlayer/config/built"; import { DEFAULT_LOCALE, LOCALES, ROUTING_MODE } from "@intlayer/config/defaultValues"; //#region src/localization/getPrefix.ts /** * Resolves routing configuration by merging provided options with configuration defaults. * Single source of truth for default routing config resolution across all localization functions. */ const resolveRoutingConfig = (options = {}) => ({ defaultLocale: internationalization?.defaultLocale ?? DEFAULT_LOCALE, mode: routing?.mode ?? ROUTING_MODE, locales: internationalization?.locales ?? LOCALES, rewrite: routing?.rewrite, domains: routing?.domains, ...options }); /** * Determines the URL prefix for a given locale based on the routing mode configuration. * * Example: * * ```ts * // prefix-no-default mode with default locale * getPrefix('en', { defaultLocale: 'en', mode: 'prefix-no-default' }) * // Returns { prefix: '', localePrefix: undefined } * * // prefix-no-default mode with non-default locale * getPrefix('fr', { defaultLocale: 'en', mode: 'prefix-no-default' }) * // Returns { prefix: '/fr', localePrefix: 'fr' } * * // prefix-all mode * getPrefix('en', { defaultLocale: 'en', mode: 'prefix-all' }) * // Returns { prefix: '/en', localePrefix: locale } * * // search-params mode * getPrefix('en', { defaultLocale: 'en', mode: 'search-params' }) * // Returns { prefix: '', localePrefix: undefined } * * // no-prefix mode * getPrefix('en', { defaultLocale: 'en', mode: 'no-prefix' }) * // Returns { prefix: '', localePrefix: undefined } * ``` * * @param locale - The locale to check for prefix. If not provided, uses configured default locale. * @param options - Configuration options * @param options.defaultLocale - The default locale. Defaults to configured default locale. * @param options.mode - URL routing mode for locale handling. Defaults to configured mode. * @returns An object containing pathPrefix, prefix, and localePrefix for the given locale. */ const getPrefix = (locale, options = {}) => { const { defaultLocale, mode, locales, domains } = resolveRoutingConfig(options); if (process.env["INTLAYER_ROUTING_MODE"] && process.env["INTLAYER_ROUTING_MODE"] !== "prefix-all" && process.env["INTLAYER_ROUTING_MODE"] !== "prefix-no-default" || !locale || !locales.includes(locale)) return { prefix: "", localePrefix: void 0 }; if (process.env["INTLAYER_ROUTING_DOMAINS"] !== "false" && domains) { const localeDomain = domains[locale]; if (localeDomain) { if (Object.values(domains).filter((domain) => domain === localeDomain).length === 1) return { prefix: "", localePrefix: void 0 }; } } if (mode === "prefix-all" || mode === "prefix-no-default" && defaultLocale !== locale) return { prefix: `${locale}/`, localePrefix: locale }; return { prefix: "", localePrefix: void 0 }; }; //#endregion export { getPrefix, resolveRoutingConfig }; //# sourceMappingURL=getPrefix.mjs.map