@intlayer/core
Version:
Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.
57 lines (55 loc) • 2.16 kB
JavaScript
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
let __intlayer_config_built = require("@intlayer/config/built");
__intlayer_config_built = require_rolldown_runtime.__toESM(__intlayer_config_built);
//#region src/localization/getPrefix.ts
/**
* 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 = __intlayer_config_built.default?.internationalization?.defaultLocale, mode = __intlayer_config_built.default?.routing?.mode } = options;
if (!locale) 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
exports.getPrefix = getPrefix;
//# sourceMappingURL=getPrefix.cjs.map