@intlayer/core 5.4.0-canary.0 → 5.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/localization/getPathWithoutLocale.cjs.map +1 -1
- package/dist/cjs/localization/localeMapper.cjs +6 -11
- package/dist/cjs/localization/localeMapper.cjs.map +1 -1
- package/dist/cjs/localization/localeResolver.cjs.map +1 -1
- package/dist/esm/localization/getPathWithoutLocale.mjs.map +1 -1
- package/dist/esm/localization/localeMapper.mjs +6 -11
- package/dist/esm/localization/localeMapper.mjs.map +1 -1
- package/dist/esm/localization/localeResolver.mjs.map +1 -1
- package/dist/types/localization/getPathWithoutLocale.d.ts.map +1 -1
- package/dist/types/localization/localeMapper.d.ts +2 -2
- package/dist/types/localization/localeMapper.d.ts.map +1 -1
- package/dist/types/localization/localeResolver.d.ts.map +1 -1
- package/package.json +10 -10
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/localization/getPathWithoutLocale.ts"],"sourcesContent":["import { type Locales, type LocalesValues } from '@intlayer/config/client';\
|
|
1
|
+
{"version":3,"sources":["../../../src/localization/getPathWithoutLocale.ts"],"sourcesContent":["import configuration from '@intlayer/config/built';\nimport { type Locales, type LocalesValues } from '@intlayer/config/client';\n\nimport { checkIsURLAbsolute } from '../utils/checkIsURLAbsolute';\n\n/**\n * Removes the locale segment from the given URL or pathname if present.\n *\n * This function get the locales from the configuration if not provided.\n *\n * Example:\n *\n * ```ts\n * getPathWithoutLocale('/en/dashboard') // Returns '/dashboard'\n * getPathWithoutLocale('/fr/dashboard') // Returns '/dashboard'\n * getPathWithoutLocale('/dashboard') // Returns '/dashboard'\n * getPathWithoutLocale('dashboard') // Returns 'dashboard'\n * getPathWithoutLocale('https://example.com/en/dashboard') // Returns 'https://example.com/dashboard'\n * getPathWithoutLocale('https://example.com/fr/dashboard') // Returns 'https://example.com/dashboard'\n * getPathWithoutLocale('https://example.com/dashboard') // Returns 'https://example.com/dashboard'\n * ```\n *\n * @param inputUrl - The complete URL string or pathname to process.\n * @param locales - Optional array of supported locales. Defaults to `localesDefault`.\n * @returns The URL string or pathname without the locale segment.\n */\nexport const getPathWithoutLocale = (\n inputUrl: string,\n locales: LocalesValues[] = configuration.internationalization\n .locales as LocalesValues[]\n): string => {\n // Determine if the original URL is absolute (includes protocol)\n const isAbsoluteUrl = checkIsURLAbsolute(inputUrl);\n\n let fixedInputUrl = inputUrl;\n\n if (inputUrl.endsWith('/')) {\n fixedInputUrl = inputUrl.slice(0, -1);\n }\n\n // Initialize a URL object if the URL is absolute\n // For relative URLs, use a dummy base to leverage the URL API\n const url = isAbsoluteUrl\n ? new URL(fixedInputUrl)\n : new URL(fixedInputUrl, 'http://example.com');\n\n const pathname = url.pathname;\n\n // Ensure the pathname starts with '/'\n if (!pathname.startsWith('/')) {\n // If not, return the URL as is\n url.pathname = `/${pathname}`;\n }\n\n // Split the pathname to extract the first segment\n const pathSegments = pathname.split('/');\n const firstSegment = pathSegments[1]; // The segment after the first '/'\n\n // Check if the first segment is a supported locale\n if (locales.includes(firstSegment as Locales)) {\n // Remove the locale segment from the pathname\n pathSegments.splice(1, 1); // Remove the first segment\n\n // Reconstruct the pathname\n const newPathname = pathSegments.join('/') ?? '/';\n url.pathname = newPathname;\n }\n\n if (isAbsoluteUrl) {\n // Return the modified URL as a string\n return url.toString();\n }\n\n // Return the modified URL as a string\n return url.toString().replace('http://example.com', '');\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAA0B;AAG1B,gCAAmC;AAuB5B,MAAM,uBAAuB,CAClC,UACA,UAA2B,aAAAA,QAAc,qBACtC,YACQ;AAEX,QAAM,oBAAgB,8CAAmB,QAAQ;AAEjD,MAAI,gBAAgB;AAEpB,MAAI,SAAS,SAAS,GAAG,GAAG;AAC1B,oBAAgB,SAAS,MAAM,GAAG,EAAE;AAAA,EACtC;AAIA,QAAM,MAAM,gBACR,IAAI,IAAI,aAAa,IACrB,IAAI,IAAI,eAAe,oBAAoB;AAE/C,QAAM,WAAW,IAAI;AAGrB,MAAI,CAAC,SAAS,WAAW,GAAG,GAAG;AAE7B,QAAI,WAAW,IAAI,QAAQ;AAAA,EAC7B;AAGA,QAAM,eAAe,SAAS,MAAM,GAAG;AACvC,QAAM,eAAe,aAAa,CAAC;AAGnC,MAAI,QAAQ,SAAS,YAAuB,GAAG;AAE7C,iBAAa,OAAO,GAAG,CAAC;AAGxB,UAAM,cAAc,aAAa,KAAK,GAAG,KAAK;AAC9C,QAAI,WAAW;AAAA,EACjB;AAEA,MAAI,eAAe;AAEjB,WAAO,IAAI,SAAS;AAAA,EACtB;AAGA,SAAO,IAAI,SAAS,EAAE,QAAQ,sBAAsB,EAAE;AACxD;","names":["configuration"]}
|
|
@@ -33,25 +33,20 @@ __export(localeMapper_exports, {
|
|
|
33
33
|
});
|
|
34
34
|
module.exports = __toCommonJS(localeMapper_exports);
|
|
35
35
|
var import_built = __toESM(require("@intlayer/config/built"));
|
|
36
|
-
const
|
|
37
|
-
const { prefixDefault } = middleware;
|
|
38
|
-
const { locales, defaultLocale } = internationalization;
|
|
39
|
-
const baseLocaleData = {
|
|
40
|
-
defaultLocale,
|
|
41
|
-
locales
|
|
42
|
-
};
|
|
43
|
-
const localeMap = (mapper) => locales.map(
|
|
36
|
+
const localeMap = (mapper, locales = import_built.default.internationalization.locales, defaultLocale = import_built.default.internationalization.defaultLocale, prefixDefault = import_built.default.middleware.prefixDefault) => locales.map(
|
|
44
37
|
(locale) => mapper({
|
|
45
|
-
...baseLocaleData,
|
|
46
38
|
locale,
|
|
39
|
+
defaultLocale,
|
|
40
|
+
locales,
|
|
47
41
|
isDefault: locale === defaultLocale,
|
|
48
42
|
urlPrefix: locale === defaultLocale && !prefixDefault ? "" : `/${locale}`
|
|
49
43
|
})
|
|
50
44
|
);
|
|
51
|
-
const localeFlatMap = (mapper) => locales.flatMap(
|
|
45
|
+
const localeFlatMap = (mapper, locales = import_built.default.internationalization.locales, defaultLocale = import_built.default.internationalization.defaultLocale, prefixDefault = import_built.default.middleware.prefixDefault) => locales.flatMap(
|
|
52
46
|
(locale) => mapper({
|
|
53
|
-
...baseLocaleData,
|
|
54
47
|
locale,
|
|
48
|
+
defaultLocale,
|
|
49
|
+
locales,
|
|
55
50
|
isDefault: locale === defaultLocale,
|
|
56
51
|
urlPrefix: locale === defaultLocale && !prefixDefault ? "" : `/${locale}`
|
|
57
52
|
})
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/localization/localeMapper.ts"],"sourcesContent":["import configuration from '@intlayer/config/built';\nimport { LocalesValues } from '@intlayer/config/client';\n\
|
|
1
|
+
{"version":3,"sources":["../../../src/localization/localeMapper.ts"],"sourcesContent":["import configuration from '@intlayer/config/built';\nimport { LocalesValues } from '@intlayer/config/client';\n\nexport type LocaleData = {\n locale: LocalesValues;\n defaultLocale: LocalesValues;\n isDefault: boolean;\n locales: LocalesValues[];\n urlPrefix: string;\n};\n\n/**\n * Map the locale data to an array of objects\n *\n * @example\n * ```ts\n * const routes = localeMap((localizedData) =>\n * ({\n * path: localizedData.urlPrefix,\n * name: localizedData.locale,\n * isDefault: localizedData.isDefault,\n * locales: localizedData.locales,\n * defaultLocale: localizedData.defaultLocale,\n * }),\n * );\n *\n * // Result\n * [\n * { path: '/', name: 'en', isDefault: true, locales: ['en'], defaultLocale: 'en', urlPrefix: '' },\n * { path: '/fr', name: 'fr', isDefault: false, locales: ['fr'], defaultLocale: 'en', urlPrefix: '/fr' },\n * { path: '/es', name: 'es', isDefault: false, locales: ['es'], defaultLocale: 'en', urlPrefix: '/es' },\n * ]\n * ```\n *\n * @param mapper - The mapper function that returns an object\n * @returns An array of objects\n */\nexport const localeMap = <T extends object>(\n mapper: (locale: LocaleData) => T,\n locales: LocalesValues[] = configuration.internationalization.locales,\n defaultLocale: LocalesValues = configuration.internationalization\n .defaultLocale,\n prefixDefault: boolean = configuration.middleware.prefixDefault\n): T[] =>\n locales.map((locale) =>\n mapper({\n locale,\n defaultLocale,\n locales,\n isDefault: locale === defaultLocale,\n urlPrefix: locale === defaultLocale && !prefixDefault ? '' : `/${locale}`,\n })\n );\n\n/**\n * Flatten the locale map into a single array of objects\n *\n * @example\n * ```ts\n * const routes = localeMap((localizedData) =>\n * [{\n * path: localizedData.urlPrefix,\n * name: localizedData.locale,\n * isDefault: localizedData.isDefault,\n * locales: localizedData.locales,\n * defaultLocale: localizedData.defaultLocale,\n * }],\n * );\n *\n * // Result\n * [\n * { path: '/', name: 'en', isDefault: true, locales: ['en'], defaultLocale: 'en', urlPrefix: '' },\n * { path: '/fr', name: 'fr', isDefault: false, locales: ['fr'], defaultLocale: 'en', urlPrefix: '/fr' },\n * { path: '/es', name: 'es', isDefault: false, locales: ['es'], defaultLocale: 'en', urlPrefix: '/es' },\n * ]\n * ```\n *\n * @param mapper - The mapper function that returns an array of objects\n * @returns An array of objects\n */\nexport const localeFlatMap = <T>(\n mapper: (locale: LocaleData) => T[],\n locales: LocalesValues[] = configuration.internationalization.locales,\n defaultLocale: LocalesValues = configuration.internationalization\n .defaultLocale,\n prefixDefault: boolean = configuration.middleware.prefixDefault\n): T[] =>\n locales.flatMap((locale) =>\n mapper({\n locale,\n defaultLocale,\n locales,\n isDefault: locale === defaultLocale,\n urlPrefix: locale === defaultLocale && !prefixDefault ? '' : `/${locale}`,\n })\n );\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAA0B;AAqCnB,MAAM,YAAY,CACvB,QACA,UAA2B,aAAAA,QAAc,qBAAqB,SAC9D,gBAA+B,aAAAA,QAAc,qBAC1C,eACH,gBAAyB,aAAAA,QAAc,WAAW,kBAElD,QAAQ;AAAA,EAAI,CAAC,WACX,OAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA,WAAW,WAAW;AAAA,IACtB,WAAW,WAAW,iBAAiB,CAAC,gBAAgB,KAAK,IAAI,MAAM;AAAA,EACzE,CAAC;AACH;AA4BK,MAAM,gBAAgB,CAC3B,QACA,UAA2B,aAAAA,QAAc,qBAAqB,SAC9D,gBAA+B,aAAAA,QAAc,qBAC1C,eACH,gBAAyB,aAAAA,QAAc,WAAW,kBAElD,QAAQ;AAAA,EAAQ,CAAC,WACf,OAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA,WAAW,WAAW;AAAA,IACtB,WAAW,WAAW,iBAAiB,CAAC,gBAAgB,KAAK,IAAI,MAAM;AAAA,EACzE,CAAC;AACH;","names":["configuration"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/localization/localeResolver.ts"],"sourcesContent":["import
|
|
1
|
+
{"version":3,"sources":["../../../src/localization/localeResolver.ts"],"sourcesContent":["import configuration from '@intlayer/config/built';\nimport type { LocalesValues } from '@intlayer/config/client';\n\n/**\n * Resolves the most specific locale from a user-provided list,\n * or falls back to the default locale if no match is found.\n */\nexport const localeResolver = (\n selectedLocale: LocalesValues | LocalesValues[],\n locales: LocalesValues[] = configuration.internationalization.locales,\n defaultLocale: LocalesValues = configuration.internationalization\n .defaultLocale\n): LocalesValues => {\n // Ensure we can handle both a single locale or an array of locales uniformly\n const requestedLocales = [selectedLocale].flat();\n\n // Simple helper to normalize locale strings (e.g. \"en-US\" => \"en-us\")\n const normalize = (locale: string): string => locale.trim().toLowerCase();\n\n try {\n // Check each requested locale in order\n for (const requested of requestedLocales) {\n const normalizedRequested = normalize(requested);\n\n // 1) Attempt exact match\n const exactMatch = locales.find(\n (loc) => normalize(loc) === normalizedRequested\n );\n if (exactMatch) {\n return exactMatch;\n }\n\n // 2) Attempt partial match on language subtag\n // e.g. if requested is \"en-US\" and not found,\n // see if \"en\" is available among locales\n const [requestedLang] = normalizedRequested.split('-');\n const partialMatch = locales.find(\n (loc) => normalize(loc).split('-')[0] === requestedLang\n );\n if (partialMatch) {\n return partialMatch;\n }\n }\n } catch (_error) {\n // If anything unexpected happened, fall back to default\n }\n\n // If no match was found, return the default\n return defaultLocale;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAA0B;AAOnB,MAAM,iBAAiB,CAC5B,gBACA,UAA2B,aAAAA,QAAc,qBAAqB,SAC9D,gBAA+B,aAAAA,QAAc,qBAC1C,kBACe;AAElB,QAAM,mBAAmB,CAAC,cAAc,EAAE,KAAK;AAG/C,QAAM,YAAY,CAAC,WAA2B,OAAO,KAAK,EAAE,YAAY;AAExE,MAAI;AAEF,eAAW,aAAa,kBAAkB;AACxC,YAAM,sBAAsB,UAAU,SAAS;AAG/C,YAAM,aAAa,QAAQ;AAAA,QACzB,CAAC,QAAQ,UAAU,GAAG,MAAM;AAAA,MAC9B;AACA,UAAI,YAAY;AACd,eAAO;AAAA,MACT;AAKA,YAAM,CAAC,aAAa,IAAI,oBAAoB,MAAM,GAAG;AACrD,YAAM,eAAe,QAAQ;AAAA,QAC3B,CAAC,QAAQ,UAAU,GAAG,EAAE,MAAM,GAAG,EAAE,CAAC,MAAM;AAAA,MAC5C;AACA,UAAI,cAAc;AAChB,eAAO;AAAA,MACT;AAAA,IACF;AAAA,EACF,SAAS,QAAQ;AAAA,EAEjB;AAGA,SAAO;AACT;","names":["configuration"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/localization/getPathWithoutLocale.ts"],"sourcesContent":["import { type Locales, type LocalesValues } from '@intlayer/config/client';\
|
|
1
|
+
{"version":3,"sources":["../../../src/localization/getPathWithoutLocale.ts"],"sourcesContent":["import configuration from '@intlayer/config/built';\nimport { type Locales, type LocalesValues } from '@intlayer/config/client';\n\nimport { checkIsURLAbsolute } from '../utils/checkIsURLAbsolute';\n\n/**\n * Removes the locale segment from the given URL or pathname if present.\n *\n * This function get the locales from the configuration if not provided.\n *\n * Example:\n *\n * ```ts\n * getPathWithoutLocale('/en/dashboard') // Returns '/dashboard'\n * getPathWithoutLocale('/fr/dashboard') // Returns '/dashboard'\n * getPathWithoutLocale('/dashboard') // Returns '/dashboard'\n * getPathWithoutLocale('dashboard') // Returns 'dashboard'\n * getPathWithoutLocale('https://example.com/en/dashboard') // Returns 'https://example.com/dashboard'\n * getPathWithoutLocale('https://example.com/fr/dashboard') // Returns 'https://example.com/dashboard'\n * getPathWithoutLocale('https://example.com/dashboard') // Returns 'https://example.com/dashboard'\n * ```\n *\n * @param inputUrl - The complete URL string or pathname to process.\n * @param locales - Optional array of supported locales. Defaults to `localesDefault`.\n * @returns The URL string or pathname without the locale segment.\n */\nexport const getPathWithoutLocale = (\n inputUrl: string,\n locales: LocalesValues[] = configuration.internationalization\n .locales as LocalesValues[]\n): string => {\n // Determine if the original URL is absolute (includes protocol)\n const isAbsoluteUrl = checkIsURLAbsolute(inputUrl);\n\n let fixedInputUrl = inputUrl;\n\n if (inputUrl.endsWith('/')) {\n fixedInputUrl = inputUrl.slice(0, -1);\n }\n\n // Initialize a URL object if the URL is absolute\n // For relative URLs, use a dummy base to leverage the URL API\n const url = isAbsoluteUrl\n ? new URL(fixedInputUrl)\n : new URL(fixedInputUrl, 'http://example.com');\n\n const pathname = url.pathname;\n\n // Ensure the pathname starts with '/'\n if (!pathname.startsWith('/')) {\n // If not, return the URL as is\n url.pathname = `/${pathname}`;\n }\n\n // Split the pathname to extract the first segment\n const pathSegments = pathname.split('/');\n const firstSegment = pathSegments[1]; // The segment after the first '/'\n\n // Check if the first segment is a supported locale\n if (locales.includes(firstSegment as Locales)) {\n // Remove the locale segment from the pathname\n pathSegments.splice(1, 1); // Remove the first segment\n\n // Reconstruct the pathname\n const newPathname = pathSegments.join('/') ?? '/';\n url.pathname = newPathname;\n }\n\n if (isAbsoluteUrl) {\n // Return the modified URL as a string\n return url.toString();\n }\n\n // Return the modified URL as a string\n return url.toString().replace('http://example.com', '');\n};\n"],"mappings":"AAAA,OAAO,mBAAmB;AAG1B,SAAS,0BAA0B;AAuB5B,MAAM,uBAAuB,CAClC,UACA,UAA2B,cAAc,qBACtC,YACQ;AAEX,QAAM,gBAAgB,mBAAmB,QAAQ;AAEjD,MAAI,gBAAgB;AAEpB,MAAI,SAAS,SAAS,GAAG,GAAG;AAC1B,oBAAgB,SAAS,MAAM,GAAG,EAAE;AAAA,EACtC;AAIA,QAAM,MAAM,gBACR,IAAI,IAAI,aAAa,IACrB,IAAI,IAAI,eAAe,oBAAoB;AAE/C,QAAM,WAAW,IAAI;AAGrB,MAAI,CAAC,SAAS,WAAW,GAAG,GAAG;AAE7B,QAAI,WAAW,IAAI,QAAQ;AAAA,EAC7B;AAGA,QAAM,eAAe,SAAS,MAAM,GAAG;AACvC,QAAM,eAAe,aAAa,CAAC;AAGnC,MAAI,QAAQ,SAAS,YAAuB,GAAG;AAE7C,iBAAa,OAAO,GAAG,CAAC;AAGxB,UAAM,cAAc,aAAa,KAAK,GAAG,KAAK;AAC9C,QAAI,WAAW;AAAA,EACjB;AAEA,MAAI,eAAe;AAEjB,WAAO,IAAI,SAAS;AAAA,EACtB;AAGA,SAAO,IAAI,SAAS,EAAE,QAAQ,sBAAsB,EAAE;AACxD;","names":[]}
|
|
@@ -1,23 +1,18 @@
|
|
|
1
1
|
import configuration from "@intlayer/config/built";
|
|
2
|
-
const
|
|
3
|
-
const { prefixDefault } = middleware;
|
|
4
|
-
const { locales, defaultLocale } = internationalization;
|
|
5
|
-
const baseLocaleData = {
|
|
6
|
-
defaultLocale,
|
|
7
|
-
locales
|
|
8
|
-
};
|
|
9
|
-
const localeMap = (mapper) => locales.map(
|
|
2
|
+
const localeMap = (mapper, locales = configuration.internationalization.locales, defaultLocale = configuration.internationalization.defaultLocale, prefixDefault = configuration.middleware.prefixDefault) => locales.map(
|
|
10
3
|
(locale) => mapper({
|
|
11
|
-
...baseLocaleData,
|
|
12
4
|
locale,
|
|
5
|
+
defaultLocale,
|
|
6
|
+
locales,
|
|
13
7
|
isDefault: locale === defaultLocale,
|
|
14
8
|
urlPrefix: locale === defaultLocale && !prefixDefault ? "" : `/${locale}`
|
|
15
9
|
})
|
|
16
10
|
);
|
|
17
|
-
const localeFlatMap = (mapper) => locales.flatMap(
|
|
11
|
+
const localeFlatMap = (mapper, locales = configuration.internationalization.locales, defaultLocale = configuration.internationalization.defaultLocale, prefixDefault = configuration.middleware.prefixDefault) => locales.flatMap(
|
|
18
12
|
(locale) => mapper({
|
|
19
|
-
...baseLocaleData,
|
|
20
13
|
locale,
|
|
14
|
+
defaultLocale,
|
|
15
|
+
locales,
|
|
21
16
|
isDefault: locale === defaultLocale,
|
|
22
17
|
urlPrefix: locale === defaultLocale && !prefixDefault ? "" : `/${locale}`
|
|
23
18
|
})
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/localization/localeMapper.ts"],"sourcesContent":["import configuration from '@intlayer/config/built';\nimport { LocalesValues } from '@intlayer/config/client';\n\
|
|
1
|
+
{"version":3,"sources":["../../../src/localization/localeMapper.ts"],"sourcesContent":["import configuration from '@intlayer/config/built';\nimport { LocalesValues } from '@intlayer/config/client';\n\nexport type LocaleData = {\n locale: LocalesValues;\n defaultLocale: LocalesValues;\n isDefault: boolean;\n locales: LocalesValues[];\n urlPrefix: string;\n};\n\n/**\n * Map the locale data to an array of objects\n *\n * @example\n * ```ts\n * const routes = localeMap((localizedData) =>\n * ({\n * path: localizedData.urlPrefix,\n * name: localizedData.locale,\n * isDefault: localizedData.isDefault,\n * locales: localizedData.locales,\n * defaultLocale: localizedData.defaultLocale,\n * }),\n * );\n *\n * // Result\n * [\n * { path: '/', name: 'en', isDefault: true, locales: ['en'], defaultLocale: 'en', urlPrefix: '' },\n * { path: '/fr', name: 'fr', isDefault: false, locales: ['fr'], defaultLocale: 'en', urlPrefix: '/fr' },\n * { path: '/es', name: 'es', isDefault: false, locales: ['es'], defaultLocale: 'en', urlPrefix: '/es' },\n * ]\n * ```\n *\n * @param mapper - The mapper function that returns an object\n * @returns An array of objects\n */\nexport const localeMap = <T extends object>(\n mapper: (locale: LocaleData) => T,\n locales: LocalesValues[] = configuration.internationalization.locales,\n defaultLocale: LocalesValues = configuration.internationalization\n .defaultLocale,\n prefixDefault: boolean = configuration.middleware.prefixDefault\n): T[] =>\n locales.map((locale) =>\n mapper({\n locale,\n defaultLocale,\n locales,\n isDefault: locale === defaultLocale,\n urlPrefix: locale === defaultLocale && !prefixDefault ? '' : `/${locale}`,\n })\n );\n\n/**\n * Flatten the locale map into a single array of objects\n *\n * @example\n * ```ts\n * const routes = localeMap((localizedData) =>\n * [{\n * path: localizedData.urlPrefix,\n * name: localizedData.locale,\n * isDefault: localizedData.isDefault,\n * locales: localizedData.locales,\n * defaultLocale: localizedData.defaultLocale,\n * }],\n * );\n *\n * // Result\n * [\n * { path: '/', name: 'en', isDefault: true, locales: ['en'], defaultLocale: 'en', urlPrefix: '' },\n * { path: '/fr', name: 'fr', isDefault: false, locales: ['fr'], defaultLocale: 'en', urlPrefix: '/fr' },\n * { path: '/es', name: 'es', isDefault: false, locales: ['es'], defaultLocale: 'en', urlPrefix: '/es' },\n * ]\n * ```\n *\n * @param mapper - The mapper function that returns an array of objects\n * @returns An array of objects\n */\nexport const localeFlatMap = <T>(\n mapper: (locale: LocaleData) => T[],\n locales: LocalesValues[] = configuration.internationalization.locales,\n defaultLocale: LocalesValues = configuration.internationalization\n .defaultLocale,\n prefixDefault: boolean = configuration.middleware.prefixDefault\n): T[] =>\n locales.flatMap((locale) =>\n mapper({\n locale,\n defaultLocale,\n locales,\n isDefault: locale === defaultLocale,\n urlPrefix: locale === defaultLocale && !prefixDefault ? '' : `/${locale}`,\n })\n );\n"],"mappings":"AAAA,OAAO,mBAAmB;AAqCnB,MAAM,YAAY,CACvB,QACA,UAA2B,cAAc,qBAAqB,SAC9D,gBAA+B,cAAc,qBAC1C,eACH,gBAAyB,cAAc,WAAW,kBAElD,QAAQ;AAAA,EAAI,CAAC,WACX,OAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA,WAAW,WAAW;AAAA,IACtB,WAAW,WAAW,iBAAiB,CAAC,gBAAgB,KAAK,IAAI,MAAM;AAAA,EACzE,CAAC;AACH;AA4BK,MAAM,gBAAgB,CAC3B,QACA,UAA2B,cAAc,qBAAqB,SAC9D,gBAA+B,cAAc,qBAC1C,eACH,gBAAyB,cAAc,WAAW,kBAElD,QAAQ;AAAA,EAAQ,CAAC,WACf,OAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA,WAAW,WAAW;AAAA,IACtB,WAAW,WAAW,iBAAiB,CAAC,gBAAgB,KAAK,IAAI,MAAM;AAAA,EACzE,CAAC;AACH;","names":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/localization/localeResolver.ts"],"sourcesContent":["import
|
|
1
|
+
{"version":3,"sources":["../../../src/localization/localeResolver.ts"],"sourcesContent":["import configuration from '@intlayer/config/built';\nimport type { LocalesValues } from '@intlayer/config/client';\n\n/**\n * Resolves the most specific locale from a user-provided list,\n * or falls back to the default locale if no match is found.\n */\nexport const localeResolver = (\n selectedLocale: LocalesValues | LocalesValues[],\n locales: LocalesValues[] = configuration.internationalization.locales,\n defaultLocale: LocalesValues = configuration.internationalization\n .defaultLocale\n): LocalesValues => {\n // Ensure we can handle both a single locale or an array of locales uniformly\n const requestedLocales = [selectedLocale].flat();\n\n // Simple helper to normalize locale strings (e.g. \"en-US\" => \"en-us\")\n const normalize = (locale: string): string => locale.trim().toLowerCase();\n\n try {\n // Check each requested locale in order\n for (const requested of requestedLocales) {\n const normalizedRequested = normalize(requested);\n\n // 1) Attempt exact match\n const exactMatch = locales.find(\n (loc) => normalize(loc) === normalizedRequested\n );\n if (exactMatch) {\n return exactMatch;\n }\n\n // 2) Attempt partial match on language subtag\n // e.g. if requested is \"en-US\" and not found,\n // see if \"en\" is available among locales\n const [requestedLang] = normalizedRequested.split('-');\n const partialMatch = locales.find(\n (loc) => normalize(loc).split('-')[0] === requestedLang\n );\n if (partialMatch) {\n return partialMatch;\n }\n }\n } catch (_error) {\n // If anything unexpected happened, fall back to default\n }\n\n // If no match was found, return the default\n return defaultLocale;\n};\n"],"mappings":"AAAA,OAAO,mBAAmB;AAOnB,MAAM,iBAAiB,CAC5B,gBACA,UAA2B,cAAc,qBAAqB,SAC9D,gBAA+B,cAAc,qBAC1C,kBACe;AAElB,QAAM,mBAAmB,CAAC,cAAc,EAAE,KAAK;AAG/C,QAAM,YAAY,CAAC,WAA2B,OAAO,KAAK,EAAE,YAAY;AAExE,MAAI;AAEF,eAAW,aAAa,kBAAkB;AACxC,YAAM,sBAAsB,UAAU,SAAS;AAG/C,YAAM,aAAa,QAAQ;AAAA,QACzB,CAAC,QAAQ,UAAU,GAAG,MAAM;AAAA,MAC9B;AACA,UAAI,YAAY;AACd,eAAO;AAAA,MACT;AAKA,YAAM,CAAC,aAAa,IAAI,oBAAoB,MAAM,GAAG;AACrD,YAAM,eAAe,QAAQ;AAAA,QAC3B,CAAC,QAAQ,UAAU,GAAG,EAAE,MAAM,GAAG,EAAE,CAAC,MAAM;AAAA,MAC5C;AACA,UAAI,cAAc;AAChB,eAAO;AAAA,MACT;AAAA,IACF;AAAA,EACF,SAAS,QAAQ;AAAA,EAEjB;AAGA,SAAO;AACT;","names":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getPathWithoutLocale.d.ts","sourceRoot":"","sources":["../../../src/localization/getPathWithoutLocale.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"getPathWithoutLocale.d.ts","sourceRoot":"","sources":["../../../src/localization/getPathWithoutLocale.ts"],"names":[],"mappings":"AACA,OAAO,EAAgB,KAAK,aAAa,EAAE,MAAM,yBAAyB,CAAC;AAI3E;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,eAAO,MAAM,oBAAoB,GAC/B,UAAU,MAAM,EAChB,UAAS,aAAa,EACO,KAC5B,MA6CF,CAAC"}
|
|
@@ -32,7 +32,7 @@ export type LocaleData = {
|
|
|
32
32
|
* @param mapper - The mapper function that returns an object
|
|
33
33
|
* @returns An array of objects
|
|
34
34
|
*/
|
|
35
|
-
export declare const localeMap: <T extends object>(mapper: (locale: LocaleData) => T) => T[];
|
|
35
|
+
export declare const localeMap: <T extends object>(mapper: (locale: LocaleData) => T, locales?: LocalesValues[], defaultLocale?: LocalesValues, prefixDefault?: boolean) => T[];
|
|
36
36
|
/**
|
|
37
37
|
* Flatten the locale map into a single array of objects
|
|
38
38
|
*
|
|
@@ -59,5 +59,5 @@ export declare const localeMap: <T extends object>(mapper: (locale: LocaleData)
|
|
|
59
59
|
* @param mapper - The mapper function that returns an array of objects
|
|
60
60
|
* @returns An array of objects
|
|
61
61
|
*/
|
|
62
|
-
export declare const localeFlatMap: <T>(mapper: (locale: LocaleData) => T[]) => T[];
|
|
62
|
+
export declare const localeFlatMap: <T>(mapper: (locale: LocaleData) => T[], locales?: LocalesValues[], defaultLocale?: LocalesValues, prefixDefault?: boolean) => T[];
|
|
63
63
|
//# sourceMappingURL=localeMapper.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"localeMapper.d.ts","sourceRoot":"","sources":["../../../src/localization/localeMapper.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;
|
|
1
|
+
{"version":3,"file":"localeMapper.d.ts","sourceRoot":"","sources":["../../../src/localization/localeMapper.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AAExD,MAAM,MAAM,UAAU,GAAG;IACvB,MAAM,EAAE,aAAa,CAAC;IACtB,aAAa,EAAE,aAAa,CAAC;IAC7B,SAAS,EAAE,OAAO,CAAC;IACnB,OAAO,EAAE,aAAa,EAAE,CAAC;IACzB,SAAS,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,eAAO,MAAM,SAAS,GAAI,CAAC,SAAS,MAAM,EACxC,QAAQ,CAAC,MAAM,EAAE,UAAU,KAAK,CAAC,EACjC,UAAS,aAAa,EAA+C,EACrE,gBAAe,aACC,EAChB,gBAAe,OAAgD,KAC9D,CAAC,EASD,CAAC;AAEJ;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,eAAO,MAAM,aAAa,GAAI,CAAC,EAC7B,QAAQ,CAAC,MAAM,EAAE,UAAU,KAAK,CAAC,EAAE,EACnC,UAAS,aAAa,EAA+C,EACrE,gBAAe,aACC,EAChB,gBAAe,OAAgD,KAC9D,CAAC,EASD,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"localeResolver.d.ts","sourceRoot":"","sources":["../../../src/localization/localeResolver.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"localeResolver.d.ts","sourceRoot":"","sources":["../../../src/localization/localeResolver.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AAE7D;;;GAGG;AACH,eAAO,MAAM,cAAc,GACzB,gBAAgB,aAAa,GAAG,aAAa,EAAE,EAC/C,UAAS,aAAa,EAA+C,EACrE,gBAAe,aACC,KACf,aAqCF,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@intlayer/core",
|
|
3
|
-
"version": "5.4.0
|
|
3
|
+
"version": "5.4.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.",
|
|
6
6
|
"keywords": [
|
|
@@ -75,9 +75,9 @@
|
|
|
75
75
|
],
|
|
76
76
|
"dependencies": {
|
|
77
77
|
"negotiator": "^1.0.0",
|
|
78
|
-
"@intlayer/api": "5.4.0
|
|
79
|
-
"@intlayer/config": "5.4.0
|
|
80
|
-
"@intlayer/dictionaries-entry": "5.4.0
|
|
78
|
+
"@intlayer/api": "5.4.0",
|
|
79
|
+
"@intlayer/config": "5.4.0",
|
|
80
|
+
"@intlayer/dictionaries-entry": "5.4.0"
|
|
81
81
|
},
|
|
82
82
|
"devDependencies": {
|
|
83
83
|
"@types/negotiator": "^0.6.3",
|
|
@@ -90,16 +90,16 @@
|
|
|
90
90
|
"tsc-alias": "^1.8.11",
|
|
91
91
|
"tsup": "^8.4.0",
|
|
92
92
|
"typescript": "^5.8.2",
|
|
93
|
-
"@utils/ts-config-types": "1.0.4",
|
|
94
93
|
"@utils/tsup-config": "1.0.4",
|
|
95
94
|
"@utils/ts-config": "1.0.4",
|
|
96
|
-
"@utils/eslint-config": "1.0.4"
|
|
95
|
+
"@utils/eslint-config": "1.0.4",
|
|
96
|
+
"@utils/ts-config-types": "1.0.4"
|
|
97
97
|
},
|
|
98
98
|
"peerDependencies": {
|
|
99
|
-
"@intlayer/
|
|
100
|
-
"@intlayer/
|
|
101
|
-
"@intlayer/api": "5.4.0
|
|
102
|
-
"intlayer": "5.4.0
|
|
99
|
+
"@intlayer/dictionaries-entry": "5.4.0",
|
|
100
|
+
"@intlayer/config": "5.4.0",
|
|
101
|
+
"@intlayer/api": "5.4.0",
|
|
102
|
+
"intlayer": "5.4.0"
|
|
103
103
|
},
|
|
104
104
|
"engines": {
|
|
105
105
|
"node": ">=14.18"
|