@intlayer/core
Version:
Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.
62 lines (60 loc) • 2.42 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_localization_getPrefix = require('./getPrefix.cjs');
//#region src/localization/validatePrefix.ts
/**
* True when the build-time routing mode is known and is NOT 'no-prefix'.
*/
const TREE_SHAKE_NO_PREFIX = process.env["INTLAYER_ROUTING_MODE"] && process.env["INTLAYER_ROUTING_MODE"] !== "no-prefix";
/**
* True when the build-time routing mode is known and is NOT 'search-params'.
*/
const TREE_SHAKE_SEARCH_PARAMS = process.env["INTLAYER_ROUTING_MODE"] && process.env["INTLAYER_ROUTING_MODE"] !== "search-params";
/**
* 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 } = require_localization_getPrefix.resolveRoutingConfig(options);
if (!TREE_SHAKE_NO_PREFIX && mode === "no-prefix" || !TREE_SHAKE_SEARCH_PARAMS && mode === "search-params") return {
isValid: true,
localePrefix: void 0
};
const { localePrefix } = require_localization_getPrefix.getPrefix(locale || defaultLocale, {
mode,
locales,
defaultLocale
});
if (localePrefix === locale && locale === void 0) return {
isValid: true,
localePrefix: void 0
};
if (locale && !/^[a-z]{2,3}(-[a-zA-Z]{2,4})?$/.test(locale)) return {
isValid: true,
localePrefix: void 0
};
return {
isValid: locales.some((localeEl) => localeEl === locale),
localePrefix
};
};
//#endregion
exports.validatePrefix = validatePrefix;
//# sourceMappingURL=validatePrefix.cjs.map