UNPKG

@intlayer/core

Version:

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

52 lines (50 loc) 1.95 kB
import { getPrefix } from "./getPrefix.mjs"; import { DefaultValues } from "@intlayer/config/client"; import configuration from "@intlayer/config/built"; //#region src/localization/validatePrefix.ts /** * Checks whether a given locale is valid based on the configured locales. * * @param locale - The locale value to validate. Can be `undefined` or `null`. * @param options - Optional configuration to override default settings. * @param options.locales - Array of valid locales. Defaults to the configured internationalization locales. * @param options.defaultLocale - The default locale to use as fallback. Defaults to the configured default locale. * @param options.mode - The routing mode (`'prefix'`, `'prefix-all'`, or `'no-prefix'`). Defaults to the configured routing mode. * @returns An object containing the validation result and the locale prefix. * * @example * // Check if 'en' is a valid locale * const { isValid, localePrefix } = validatePrefix('en'); * * @example * // Check with custom options * const { isValid, localePrefix } = validatePrefix('fr', { * locales: ['en', 'fr', 'es'], * defaultLocale: 'en', * mode: 'prefix-all', * }); */ const validatePrefix = (locale, options) => { const { defaultLocale, mode, locales } = { defaultLocale: configuration?.internationalization?.defaultLocale ?? DefaultValues.Internationalization.DEFAULT_LOCALE, mode: configuration?.routing?.mode ?? DefaultValues.Routing.ROUTING_MODE, locales: configuration?.internationalization?.locales ?? DefaultValues.Internationalization.LOCALES, ...options }; const { localePrefix } = getPrefix(locale || defaultLocale, { mode, locales, defaultLocale }); if (localePrefix === locale && locale === void 0) return { isValid: true, localePrefix: void 0 }; return { isValid: locales.some((localeEl) => localeEl === locale), localePrefix }; }; //#endregion export { validatePrefix }; //# sourceMappingURL=validatePrefix.mjs.map