@intlayer/core
Version:
Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.
119 lines (117 loc) • 4.65 kB
JavaScript
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
let __intlayer_types = require("@intlayer/types");
let __intlayer_config_built = require("@intlayer/config/built");
__intlayer_config_built = require_rolldown_runtime.__toESM(__intlayer_config_built);
//#region src/localization/localeMapper.ts
/**
* Determine if the locale should be prefixed in the URL based on routing mode
*/
const shouldPrefixLocale = (locale, defaultLocale, mode) => {
if (mode === "no-prefix" || mode === "search-params") return false;
if (mode === "prefix-all") return true;
return locale !== defaultLocale;
};
/**
* Map the locale data to an array of objects
*
* @example
* ```ts
* const routes = localeMap((localizedData) =>
* ({
* path: localizedData.urlPrefix,
* name: localizedData.locale,
* isDefault: localizedData.isDefault,
* locales: localizedData.locales,
* defaultLocale: localizedData.defaultLocale,
* }),
* );
*
* // Result
* [
* { path: '/', name: 'en', isDefault: true, locales: ['en'], defaultLocale: 'en', urlPrefix: '' },
* { path: '/fr', name: 'fr', isDefault: false, locales: ['fr'], defaultLocale: 'en', urlPrefix: '/fr' },
* { path: '/es', name: 'es', isDefault: false, locales: ['es'], defaultLocale: 'en', urlPrefix: '/es' },
* ]
* ```
*
* @param mapper - The mapper function that returns an object
* @returns An array of objects
*/
const localeMap = (mapper, locales = __intlayer_config_built.default?.internationalization.locales ?? [], defaultLocale = __intlayer_config_built.default?.internationalization.defaultLocale ?? __intlayer_types.Locales.ENGLISH, mode = __intlayer_config_built.default?.routing?.mode ?? "prefix-no-default") => (locales ?? []).map((locale) => mapper({
locale,
defaultLocale,
locales,
isDefault: locale === defaultLocale,
urlPrefix: shouldPrefixLocale(locale, defaultLocale, mode) ? `/${locale}` : ""
}));
/**
* Flatten the locale map into a single array of objects
*
* @example
* ```ts
* const routes = localeMap((localizedData) =>
* [{
* path: localizedData.urlPrefix,
* name: localizedData.locale,
* isDefault: localizedData.isDefault,
* locales: localizedData.locales,
* defaultLocale: localizedData.defaultLocale,
* }],
* );
*
* // Result
* [
* path: '/', name: 'en', isDefault: true, locales: ['en'], defaultLocale: 'en', urlPrefix: '' ,
* path: '/fr', name: 'fr', isDefault: false, locales: ['fr'], defaultLocale: 'en', urlPrefix: '/fr' ,
* path: '/es', name: 'es', isDefault: false, locales: ['es'], defaultLocale: 'en', urlPrefix: '/es' ,
* ]
* ```
*
* @param mapper - The mapper function that returns an array of objects
* @returns An array of objects
*/
const localeFlatMap = (mapper, locales = __intlayer_config_built.default?.internationalization.locales ?? [], defaultLocale = __intlayer_config_built.default?.internationalization.defaultLocale ?? __intlayer_types.Locales.ENGLISH, mode = __intlayer_config_built.default?.routing?.mode ?? "prefix-no-default") => locales.flatMap((locale) => mapper({
locale,
defaultLocale,
locales,
isDefault: locale === defaultLocale,
urlPrefix: shouldPrefixLocale(locale, defaultLocale, mode) ? `/${locale}` : ""
}));
/**
* Creates a record object mapping locales to values transformed by the mapper function
*
* @example
* ```ts
* const translations = localeRecord(({ locale }) =>
* require(`./translations/${locale}.json`)
* );
*
* // Result
*
* en: ... , // Content of translations/en.json
* fr: ... , // Content of translations/fr.json
* es: ...
*
* ```
*
* @param mapper - Function that takes locale data and returns a value for that locale
* @param locales - Array of locale codes to map over (defaults to configured locales)
* @param defaultLocale - The default locale (defaults to configured default)
* @param mode - URL routing mode for locale handling (defaults to configured value)
* @returns Record mapping locale codes to mapped values
*/
const localeRecord = (mapper, locales = __intlayer_config_built.default?.internationalization.locales ?? [], defaultLocale = __intlayer_config_built.default?.internationalization.defaultLocale ?? __intlayer_types.Locales.ENGLISH, mode = __intlayer_config_built.default?.routing?.mode ?? "prefix-no-default") => (locales ?? []).reduce((acc, locale) => {
acc[locale] = mapper({
locale,
defaultLocale,
locales,
isDefault: locale === defaultLocale,
urlPrefix: shouldPrefixLocale(locale, defaultLocale, mode) ? `/${locale}` : ""
});
return acc;
}, {});
//#endregion
exports.localeFlatMap = localeFlatMap;
exports.localeMap = localeMap;
exports.localeRecord = localeRecord;
//# sourceMappingURL=localeMapper.cjs.map