@intlayer/core 3.5.0 → 3.5.1
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/getMultilingualUrls.cjs +57 -0
- package/dist/cjs/getMultilingualUrls.cjs.map +1 -0
- package/dist/cjs/getPathWithoutLocale.cjs +32 -12
- package/dist/cjs/getPathWithoutLocale.cjs.map +1 -1
- package/dist/cjs/index.cjs +3 -0
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/localeDetector.cjs.map +1 -1
- package/dist/esm/getMultilingualUrls.mjs +36 -0
- package/dist/esm/getMultilingualUrls.mjs.map +1 -0
- package/dist/esm/getPathWithoutLocale.mjs +29 -11
- package/dist/esm/getPathWithoutLocale.mjs.map +1 -1
- package/dist/esm/index.mjs +2 -0
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm/localeDetector.mjs.map +1 -1
- package/dist/types/getMultilingualUrls.d.ts +25 -0
- package/dist/types/getMultilingualUrls.d.ts.map +1 -0
- package/dist/types/getPathWithoutLocale.d.ts +42 -2
- package/dist/types/getPathWithoutLocale.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/localeDetector.d.ts +1 -1
- package/dist/types/localeDetector.d.ts.map +1 -1
- package/package.json +6 -6
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var getMultilingualUrls_exports = {};
|
|
20
|
+
__export(getMultilingualUrls_exports, {
|
|
21
|
+
getMultilingualUrls: () => getMultilingualUrls
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(getMultilingualUrls_exports);
|
|
24
|
+
var import_client = require("@intlayer/config/client");
|
|
25
|
+
var import_getPathWithoutLocale = require('./getPathWithoutLocale.cjs');
|
|
26
|
+
const { internationalization, middleware } = (0, import_client.getConfiguration)();
|
|
27
|
+
const { locales: localesDefault, defaultLocale: defaultLocaleDefault } = internationalization;
|
|
28
|
+
const { prefixDefault: prefixDefaultDefault } = middleware;
|
|
29
|
+
const getMultilingualUrls = (url, locales = localesDefault, defaultLocale = defaultLocaleDefault, prefixDefault = prefixDefaultDefault) => {
|
|
30
|
+
const urlWithoutLocale = (0, import_getPathWithoutLocale.getPathWithoutLocale)(url, locales);
|
|
31
|
+
const isAbsoluteUrl = (0, import_getPathWithoutLocale.checkIsAbsoluteUrl)(urlWithoutLocale);
|
|
32
|
+
const base = isAbsoluteUrl ? void 0 : "http://example.com";
|
|
33
|
+
const parsedUrl = new URL(urlWithoutLocale, base);
|
|
34
|
+
let pathname = parsedUrl.pathname;
|
|
35
|
+
if (!pathname.startsWith("/")) {
|
|
36
|
+
pathname = `/${pathname}`;
|
|
37
|
+
}
|
|
38
|
+
const baseUrl = isAbsoluteUrl ? `${parsedUrl.protocol}//${parsedUrl.host}` : "";
|
|
39
|
+
const multilingualUrls = locales.reduce(
|
|
40
|
+
(acc, locale) => {
|
|
41
|
+
const isDefaultLocale = locale.toString() === defaultLocale.toString();
|
|
42
|
+
const shouldPrefix = prefixDefault || !isDefaultLocale;
|
|
43
|
+
const localizedPath = shouldPrefix ? `/${locale}${pathname}` : pathname;
|
|
44
|
+
const localizedUrl = isAbsoluteUrl ? `${baseUrl}${localizedPath}${parsedUrl.search}${parsedUrl.hash}` : `${localizedPath}${parsedUrl.search}${parsedUrl.hash}`;
|
|
45
|
+
acc[locale] = localizedUrl;
|
|
46
|
+
return acc;
|
|
47
|
+
},
|
|
48
|
+
{}
|
|
49
|
+
// Initialize an empty object
|
|
50
|
+
);
|
|
51
|
+
return multilingualUrls;
|
|
52
|
+
};
|
|
53
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
54
|
+
0 && (module.exports = {
|
|
55
|
+
getMultilingualUrls
|
|
56
|
+
});
|
|
57
|
+
//# sourceMappingURL=getMultilingualUrls.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/getMultilingualUrls.ts"],"sourcesContent":["import { getConfiguration, Locales } from '@intlayer/config/client';\n// @ts-ignore intlayer declared for module augmentation\nimport type { IConfigLocales } from 'intlayer';\nimport {\n checkIsAbsoluteUrl,\n getPathWithoutLocale,\n} from './getPathWithoutLocale';\n\n// Destructure necessary configurations\nconst { internationalization, middleware } = getConfiguration();\nconst { locales: localesDefault, defaultLocale: defaultLocaleDefault } =\n internationalization;\nconst { prefixDefault: prefixDefaultDefault } = middleware;\n\n/**\n * Generates multilingual URLs by prefixing the given URL with each supported locale.\n * Handles both absolute and relative URLs appropriately.\n *\n * This function get the locales, default locale, and prefix default from the configuration if not provided.\n *\n * Example:\n *\n * ```ts\n * getMultilingualUrls('/dashboard', ['en', 'fr'], 'en', false)\n * // Returns { en: '/dashboard', fr: '/fr/dashboard' }\n * getMultilingualUrls('https://example.com/dashboard', ['en', 'fr'], 'en', true)\n * // Returns { en: 'https://example.com/en/dashboard', fr: 'https://example.com/fr/dashboard' }\n * ```\n *\n * @param url - The original URL string to be prefixed with locales.\n * @param locales - Optional array of supported locales. Defaults to `localesDefault`.\n * @param defaultLocale - The default locale. Defaults to `defaultLocaleDefault`.\n * @param prefixDefault - Whether to prefix the default locale. Defaults to `prefixDefaultDefault`.\n * @returns An object mapping each locale to its corresponding multilingual URL.\n */\nexport const getMultilingualUrls = (\n url: string,\n locales: Locales[] = localesDefault,\n defaultLocale: Locales = defaultLocaleDefault,\n prefixDefault: boolean = prefixDefaultDefault\n): IConfigLocales<string> => {\n // Remove any existing locale segment from the URL\n const urlWithoutLocale = getPathWithoutLocale(url, locales);\n\n // Determine if the original URL is absolute (includes protocol)\n const isAbsoluteUrl = checkIsAbsoluteUrl(urlWithoutLocale);\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 base = isAbsoluteUrl ? undefined : 'http://example.com';\n const parsedUrl = new URL(urlWithoutLocale, base);\n\n // Extract the pathname from the parsed URL\n let pathname = parsedUrl.pathname;\n\n // Ensure the pathname starts with a '/'\n if (!pathname.startsWith('/')) {\n pathname = `/${pathname}`;\n }\n\n // Prepare the base URL (protocol + host) if it's absolute\n const baseUrl = isAbsoluteUrl\n ? `${parsedUrl.protocol}//${parsedUrl.host}`\n : '';\n\n // Generate multilingual URLs by iterating over each locale\n const multilingualUrls = locales.reduce<IConfigLocales<string>>(\n (acc, locale) => {\n // Determine if the current locale is the default locale\n const isDefaultLocale = locale.toString() === defaultLocale.toString();\n\n // Decide whether to prefix the default locale based on `prefixDefault`\n const shouldPrefix = prefixDefault || !isDefaultLocale;\n\n // Construct the new pathname with or without the locale prefix\n const localizedPath = shouldPrefix ? `/${locale}${pathname}` : pathname;\n\n // Combine with the base URL if the original URL was absolute\n const localizedUrl = isAbsoluteUrl\n ? `${baseUrl}${localizedPath}${parsedUrl.search}${parsedUrl.hash}`\n : `${localizedPath}${parsedUrl.search}${parsedUrl.hash}`;\n\n // Assign the constructed URL to the corresponding locale key\n acc[locale as unknown as keyof IConfigLocales<string>] = localizedUrl;\n\n return acc;\n },\n {} as IConfigLocales<string> // Initialize an empty object\n );\n\n return multilingualUrls;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAA0C;AAG1C,kCAGO;AAGP,MAAM,EAAE,sBAAsB,WAAW,QAAI,gCAAiB;AAC9D,MAAM,EAAE,SAAS,gBAAgB,eAAe,qBAAqB,IACnE;AACF,MAAM,EAAE,eAAe,qBAAqB,IAAI;AAuBzC,MAAM,sBAAsB,CACjC,KACA,UAAqB,gBACrB,gBAAyB,sBACzB,gBAAyB,yBACE;AAE3B,QAAM,uBAAmB,kDAAqB,KAAK,OAAO;AAG1D,QAAM,oBAAgB,gDAAmB,gBAAgB;AAIzD,QAAM,OAAO,gBAAgB,SAAY;AACzC,QAAM,YAAY,IAAI,IAAI,kBAAkB,IAAI;AAGhD,MAAI,WAAW,UAAU;AAGzB,MAAI,CAAC,SAAS,WAAW,GAAG,GAAG;AAC7B,eAAW,IAAI,QAAQ;AAAA,EACzB;AAGA,QAAM,UAAU,gBACZ,GAAG,UAAU,QAAQ,KAAK,UAAU,IAAI,KACxC;AAGJ,QAAM,mBAAmB,QAAQ;AAAA,IAC/B,CAAC,KAAK,WAAW;AAEf,YAAM,kBAAkB,OAAO,SAAS,MAAM,cAAc,SAAS;AAGrE,YAAM,eAAe,iBAAiB,CAAC;AAGvC,YAAM,gBAAgB,eAAe,IAAI,MAAM,GAAG,QAAQ,KAAK;AAG/D,YAAM,eAAe,gBACjB,GAAG,OAAO,GAAG,aAAa,GAAG,UAAU,MAAM,GAAG,UAAU,IAAI,KAC9D,GAAG,aAAa,GAAG,UAAU,MAAM,GAAG,UAAU,IAAI;AAGxD,UAAI,MAAiD,IAAI;AAEzD,aAAO;AAAA,IACT;AAAA,IACA,CAAC;AAAA;AAAA,EACH;AAEA,SAAO;AACT;","names":[]}
|
|
@@ -18,24 +18,44 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
19
|
var getPathWithoutLocale_exports = {};
|
|
20
20
|
__export(getPathWithoutLocale_exports, {
|
|
21
|
-
|
|
21
|
+
checkIsAbsoluteUrl: () => checkIsAbsoluteUrl,
|
|
22
|
+
getPathWithoutLocale: () => getPathWithoutLocale,
|
|
23
|
+
removeLocaleFromUrl: () => removeLocaleFromUrl
|
|
22
24
|
});
|
|
23
25
|
module.exports = __toCommonJS(getPathWithoutLocale_exports);
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
)
|
|
29
|
-
|
|
26
|
+
var import_client = require("@intlayer/config/client");
|
|
27
|
+
const { internationalization } = (0, import_client.getConfiguration)();
|
|
28
|
+
const { locales: localesDefault } = internationalization;
|
|
29
|
+
const removeLocaleFromUrl = (inputUrl, locales = localesDefault) => {
|
|
30
|
+
console.log("inputUrl", inputUrl);
|
|
31
|
+
const url = new URL(inputUrl);
|
|
32
|
+
const pathname = url.pathname;
|
|
33
|
+
if (!pathname.startsWith("/")) {
|
|
34
|
+
url.pathname = `/${pathname}`;
|
|
30
35
|
}
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
|
|
36
|
+
const pathSegments = pathname.split("/");
|
|
37
|
+
const firstSegment = pathSegments[1];
|
|
38
|
+
if (locales.includes(firstSegment)) {
|
|
39
|
+
pathSegments.splice(1, 1);
|
|
40
|
+
const newPathname = pathSegments.join("/") ?? "/";
|
|
41
|
+
url.pathname = newPathname;
|
|
34
42
|
}
|
|
35
|
-
return
|
|
43
|
+
return url.toString();
|
|
36
44
|
};
|
|
45
|
+
const getPathWithoutLocale = (inputUrl, locales = localesDefault) => {
|
|
46
|
+
const isAbsoluteUrl = checkIsAbsoluteUrl(inputUrl);
|
|
47
|
+
const url = isAbsoluteUrl ? new URL(inputUrl) : new URL(inputUrl, "http://example.com");
|
|
48
|
+
const urlWithoutLocale = removeLocaleFromUrl(url.toString(), locales);
|
|
49
|
+
if (isAbsoluteUrl) {
|
|
50
|
+
return urlWithoutLocale;
|
|
51
|
+
}
|
|
52
|
+
return urlWithoutLocale.replace("http://example.com", "");
|
|
53
|
+
};
|
|
54
|
+
const checkIsAbsoluteUrl = (url) => /^[a-zA-Z][a-zA-Z\d+\-.]*:/.test(url);
|
|
37
55
|
// Annotate the CommonJS export names for ESM import in node:
|
|
38
56
|
0 && (module.exports = {
|
|
39
|
-
|
|
57
|
+
checkIsAbsoluteUrl,
|
|
58
|
+
getPathWithoutLocale,
|
|
59
|
+
removeLocaleFromUrl
|
|
40
60
|
});
|
|
41
61
|
//# sourceMappingURL=getPathWithoutLocale.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/getPathWithoutLocale.ts"],"sourcesContent":["import { Locales } from '@intlayer/config';\n\nexport const
|
|
1
|
+
{"version":3,"sources":["../../src/getPathWithoutLocale.ts"],"sourcesContent":["import { getConfiguration, type Locales } from '@intlayer/config/client';\n\nconst { internationalization } = getConfiguration();\nconst { locales: localesDefault } = internationalization;\n\n/**\n * Removes the locale segment from the given URL if present.\n *\n * This function get the locales from the configuration if not provided.\n *\n * Example:\n *\n * ```ts\n * removeLocaleFromUrl('https://example.com/en/dashboard') // Returns 'https://example.com/dashboard'\n * removeLocaleFromUrl('https://example.com/fr/dashboard') // Returns 'https://example.com/fr/dashboard'\n * removeLocaleFromUrl('https://example.com/dashboard') // Returns 'https://example.com/dashboard'\n * ```\n *\n * @param inputUrl - The complete URL string to process.\n * @param locales - Optional array of supported locales. Defaults to `localesDefault`.\n * @returns The URL string without the locale segment.\n */\nexport const removeLocaleFromUrl = (\n inputUrl: string,\n locales: Locales[] = localesDefault\n): string => {\n console.log('inputUrl', inputUrl);\n // Parse the input URL\n const url = new URL(inputUrl);\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 // Return the modified URL as a string\n return url.toString();\n};\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: Locales[] = localesDefault\n): string => {\n // Determine if the input is an absolute URL\n const isAbsoluteUrl = checkIsAbsoluteUrl(inputUrl);\n\n // If it's an absolute URL, parse it using the URL constructor\n // Otherwise, handle it as a relative pathname\n const url = isAbsoluteUrl\n ? new URL(inputUrl)\n : new URL(inputUrl, 'http://example.com');\n\n const urlWithoutLocale = removeLocaleFromUrl(url.toString(), locales);\n\n if (isAbsoluteUrl) {\n // If the input was an absolute URL, return the modified URL as a string\n return urlWithoutLocale;\n }\n\n // If the input was a relative pathname, return the modified pathname\n return urlWithoutLocale.replace('http://example.com', '');\n};\n\nexport const checkIsAbsoluteUrl = (url: string): boolean =>\n /^[a-zA-Z][a-zA-Z\\d+\\-.]*:/.test(url);\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAA+C;AAE/C,MAAM,EAAE,qBAAqB,QAAI,gCAAiB;AAClD,MAAM,EAAE,SAAS,eAAe,IAAI;AAmB7B,MAAM,sBAAsB,CACjC,UACA,UAAqB,mBACV;AACX,UAAQ,IAAI,YAAY,QAAQ;AAEhC,QAAM,MAAM,IAAI,IAAI,QAAQ;AAE5B,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;AAGA,SAAO,IAAI,SAAS;AACtB;AAuBO,MAAM,uBAAuB,CAClC,UACA,UAAqB,mBACV;AAEX,QAAM,gBAAgB,mBAAmB,QAAQ;AAIjD,QAAM,MAAM,gBACR,IAAI,IAAI,QAAQ,IAChB,IAAI,IAAI,UAAU,oBAAoB;AAE1C,QAAM,mBAAmB,oBAAoB,IAAI,SAAS,GAAG,OAAO;AAEpE,MAAI,eAAe;AAEjB,WAAO;AAAA,EACT;AAGA,SAAO,iBAAiB,QAAQ,sBAAsB,EAAE;AAC1D;AAEO,MAAM,qBAAqB,CAAC,QACjC,4BAA4B,KAAK,GAAG;","names":[]}
|
package/dist/cjs/index.cjs
CHANGED
|
@@ -25,6 +25,7 @@ __export(src_exports, {
|
|
|
25
25
|
getHTMLLang: () => import_getHTMLLang.getHTMLLang,
|
|
26
26
|
getHTMLTextDir: () => import_getHTMLTextDir.getHTMLTextDir,
|
|
27
27
|
getLocaleName: () => import_getLocaleName.getLocaleName,
|
|
28
|
+
getMultilingualUrls: () => import_getMultilingualUrls.getMultilingualUrls,
|
|
28
29
|
getPathWithoutLocale: () => import_getPathWithoutLocale.getPathWithoutLocale,
|
|
29
30
|
getTranslationContent: () => import_content_transformers.getTranslationContent,
|
|
30
31
|
isSameKeyPath: () => import_isSameKeyPath.isSameKeyPath,
|
|
@@ -42,6 +43,7 @@ var import_localeList = require('./localeList.cjs');
|
|
|
42
43
|
var import_isSameKeyPath = require('./utils/isSameKeyPath.cjs');
|
|
43
44
|
var import_localeDetector = require('./localeDetector.cjs');
|
|
44
45
|
var import_getPathWithoutLocale = require('./getPathWithoutLocale.cjs');
|
|
46
|
+
var import_getMultilingualUrls = require('./getMultilingualUrls.cjs');
|
|
45
47
|
// Annotate the CommonJS export names for ESM import in node:
|
|
46
48
|
0 && (module.exports = {
|
|
47
49
|
NodeType,
|
|
@@ -51,6 +53,7 @@ var import_getPathWithoutLocale = require('./getPathWithoutLocale.cjs');
|
|
|
51
53
|
getHTMLLang,
|
|
52
54
|
getHTMLTextDir,
|
|
53
55
|
getLocaleName,
|
|
56
|
+
getMultilingualUrls,
|
|
54
57
|
getPathWithoutLocale,
|
|
55
58
|
getTranslationContent,
|
|
56
59
|
isSameKeyPath,
|
package/dist/cjs/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export type {\n CustomizableLanguageContent,\n LanguageContent,\n TranslationContent,\n EnumerationContent,\n QuantityContent,\n CustomLocales as Locales,\n} from './transpiler/content_transformers/index';\nexport {\n t,\n enu,\n getTranslationContent,\n findMatchingCondition,\n getEnumerationContent,\n} from './transpiler/content_transformers/index';\nexport type {\n ContentValue,\n Content,\n FlatContentValue,\n FlatContent,\n TypedNode,\n DeclarationContent,\n KeyPath,\n ObjectNode,\n ArrayNode,\n RecursiveDictionaryValue,\n TranslationNode,\n EnumerationNode,\n DictionaryValue,\n Dictionary,\n} from './types/index';\nexport { NodeType } from './types/index';\nexport { getLocaleName } from './getLocaleName';\nexport { getHTMLTextDir } from './getHTMLTextDir';\nexport { getHTMLLang } from './getHTMLLang';\nexport { localeList } from './localeList';\nexport { isSameKeyPath } from './utils/isSameKeyPath';\nexport { localeDetector } from './localeDetector';\nexport { getPathWithoutLocale } from './getPathWithoutLocale';\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAQA,kCAMO;AAiBP,mBAAyB;AACzB,2BAA8B;AAC9B,4BAA+B;AAC/B,yBAA4B;AAC5B,wBAA2B;AAC3B,2BAA8B;AAC9B,4BAA+B;AAC/B,kCAAqC;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export type {\n CustomizableLanguageContent,\n LanguageContent,\n TranslationContent,\n EnumerationContent,\n QuantityContent,\n CustomLocales as Locales,\n} from './transpiler/content_transformers/index';\nexport {\n t,\n enu,\n getTranslationContent,\n findMatchingCondition,\n getEnumerationContent,\n} from './transpiler/content_transformers/index';\nexport type {\n ContentValue,\n Content,\n FlatContentValue,\n FlatContent,\n TypedNode,\n DeclarationContent,\n KeyPath,\n ObjectNode,\n ArrayNode,\n RecursiveDictionaryValue,\n TranslationNode,\n EnumerationNode,\n DictionaryValue,\n Dictionary,\n} from './types/index';\nexport { NodeType } from './types/index';\nexport { getLocaleName } from './getLocaleName';\nexport { getHTMLTextDir } from './getHTMLTextDir';\nexport { getHTMLLang } from './getHTMLLang';\nexport { localeList } from './localeList';\nexport { isSameKeyPath } from './utils/isSameKeyPath';\nexport { localeDetector } from './localeDetector';\nexport { getPathWithoutLocale } from './getPathWithoutLocale';\nexport { getMultilingualUrls } from './getMultilingualUrls';\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAQA,kCAMO;AAiBP,mBAAyB;AACzB,2BAA8B;AAC9B,4BAA+B;AAC/B,yBAA4B;AAC5B,wBAA2B;AAC3B,2BAA8B;AAC9B,4BAA+B;AAC/B,kCAAqC;AACrC,iCAAoC;","names":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/localeDetector.ts"],"sourcesContent":["import { match } from '@formatjs/intl-localematcher';\nimport
|
|
1
|
+
{"version":3,"sources":["../../src/localeDetector.ts"],"sourcesContent":["import { match } from '@formatjs/intl-localematcher';\nimport { getConfiguration, type Locales } from '@intlayer/config/client';\nimport Negotiator from 'negotiator';\n\nconst { locales: envLocales, defaultLocale: defaultLocaleEnv } =\n getConfiguration().internationalization;\n\n/**\n * Detects the locale from the request headers\n *\n * Headers are provided by the browser and can be used to determine the user's preferred language\n */\nexport const localeDetector = (\n headers: Record<string, string | undefined>,\n locales: Locales[] = envLocales,\n defaultLocale: Locales = defaultLocaleEnv\n): Locales => {\n const languages = new Negotiator({ headers }).languages();\n\n // match can only use specifically formatted locales\n // https://stackoverflow.com/questions/76447732/nextjs-13-i18n-incorrect-locale-information-provided\n try {\n return match(languages, locales, defaultLocale) as Locales;\n } catch (_error) {\n return defaultLocale;\n }\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,gCAAsB;AACtB,oBAA+C;AAC/C,wBAAuB;AAEvB,MAAM,EAAE,SAAS,YAAY,eAAe,iBAAiB,QAC3D,gCAAiB,EAAE;AAOd,MAAM,iBAAiB,CAC5B,SACA,UAAqB,YACrB,gBAAyB,qBACb;AACZ,QAAM,YAAY,IAAI,kBAAAA,QAAW,EAAE,QAAQ,CAAC,EAAE,UAAU;AAIxD,MAAI;AACF,eAAO,iCAAM,WAAW,SAAS,aAAa;AAAA,EAChD,SAAS,QAAQ;AACf,WAAO;AAAA,EACT;AACF;","names":["Negotiator"]}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { getConfiguration } from "@intlayer/config/client";
|
|
2
|
+
import {
|
|
3
|
+
checkIsAbsoluteUrl,
|
|
4
|
+
getPathWithoutLocale
|
|
5
|
+
} from './getPathWithoutLocale.mjs';
|
|
6
|
+
const { internationalization, middleware } = getConfiguration();
|
|
7
|
+
const { locales: localesDefault, defaultLocale: defaultLocaleDefault } = internationalization;
|
|
8
|
+
const { prefixDefault: prefixDefaultDefault } = middleware;
|
|
9
|
+
const getMultilingualUrls = (url, locales = localesDefault, defaultLocale = defaultLocaleDefault, prefixDefault = prefixDefaultDefault) => {
|
|
10
|
+
const urlWithoutLocale = getPathWithoutLocale(url, locales);
|
|
11
|
+
const isAbsoluteUrl = checkIsAbsoluteUrl(urlWithoutLocale);
|
|
12
|
+
const base = isAbsoluteUrl ? void 0 : "http://example.com";
|
|
13
|
+
const parsedUrl = new URL(urlWithoutLocale, base);
|
|
14
|
+
let pathname = parsedUrl.pathname;
|
|
15
|
+
if (!pathname.startsWith("/")) {
|
|
16
|
+
pathname = `/${pathname}`;
|
|
17
|
+
}
|
|
18
|
+
const baseUrl = isAbsoluteUrl ? `${parsedUrl.protocol}//${parsedUrl.host}` : "";
|
|
19
|
+
const multilingualUrls = locales.reduce(
|
|
20
|
+
(acc, locale) => {
|
|
21
|
+
const isDefaultLocale = locale.toString() === defaultLocale.toString();
|
|
22
|
+
const shouldPrefix = prefixDefault || !isDefaultLocale;
|
|
23
|
+
const localizedPath = shouldPrefix ? `/${locale}${pathname}` : pathname;
|
|
24
|
+
const localizedUrl = isAbsoluteUrl ? `${baseUrl}${localizedPath}${parsedUrl.search}${parsedUrl.hash}` : `${localizedPath}${parsedUrl.search}${parsedUrl.hash}`;
|
|
25
|
+
acc[locale] = localizedUrl;
|
|
26
|
+
return acc;
|
|
27
|
+
},
|
|
28
|
+
{}
|
|
29
|
+
// Initialize an empty object
|
|
30
|
+
);
|
|
31
|
+
return multilingualUrls;
|
|
32
|
+
};
|
|
33
|
+
export {
|
|
34
|
+
getMultilingualUrls
|
|
35
|
+
};
|
|
36
|
+
//# sourceMappingURL=getMultilingualUrls.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/getMultilingualUrls.ts"],"sourcesContent":["import { getConfiguration, Locales } from '@intlayer/config/client';\n// @ts-ignore intlayer declared for module augmentation\nimport type { IConfigLocales } from 'intlayer';\nimport {\n checkIsAbsoluteUrl,\n getPathWithoutLocale,\n} from './getPathWithoutLocale';\n\n// Destructure necessary configurations\nconst { internationalization, middleware } = getConfiguration();\nconst { locales: localesDefault, defaultLocale: defaultLocaleDefault } =\n internationalization;\nconst { prefixDefault: prefixDefaultDefault } = middleware;\n\n/**\n * Generates multilingual URLs by prefixing the given URL with each supported locale.\n * Handles both absolute and relative URLs appropriately.\n *\n * This function get the locales, default locale, and prefix default from the configuration if not provided.\n *\n * Example:\n *\n * ```ts\n * getMultilingualUrls('/dashboard', ['en', 'fr'], 'en', false)\n * // Returns { en: '/dashboard', fr: '/fr/dashboard' }\n * getMultilingualUrls('https://example.com/dashboard', ['en', 'fr'], 'en', true)\n * // Returns { en: 'https://example.com/en/dashboard', fr: 'https://example.com/fr/dashboard' }\n * ```\n *\n * @param url - The original URL string to be prefixed with locales.\n * @param locales - Optional array of supported locales. Defaults to `localesDefault`.\n * @param defaultLocale - The default locale. Defaults to `defaultLocaleDefault`.\n * @param prefixDefault - Whether to prefix the default locale. Defaults to `prefixDefaultDefault`.\n * @returns An object mapping each locale to its corresponding multilingual URL.\n */\nexport const getMultilingualUrls = (\n url: string,\n locales: Locales[] = localesDefault,\n defaultLocale: Locales = defaultLocaleDefault,\n prefixDefault: boolean = prefixDefaultDefault\n): IConfigLocales<string> => {\n // Remove any existing locale segment from the URL\n const urlWithoutLocale = getPathWithoutLocale(url, locales);\n\n // Determine if the original URL is absolute (includes protocol)\n const isAbsoluteUrl = checkIsAbsoluteUrl(urlWithoutLocale);\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 base = isAbsoluteUrl ? undefined : 'http://example.com';\n const parsedUrl = new URL(urlWithoutLocale, base);\n\n // Extract the pathname from the parsed URL\n let pathname = parsedUrl.pathname;\n\n // Ensure the pathname starts with a '/'\n if (!pathname.startsWith('/')) {\n pathname = `/${pathname}`;\n }\n\n // Prepare the base URL (protocol + host) if it's absolute\n const baseUrl = isAbsoluteUrl\n ? `${parsedUrl.protocol}//${parsedUrl.host}`\n : '';\n\n // Generate multilingual URLs by iterating over each locale\n const multilingualUrls = locales.reduce<IConfigLocales<string>>(\n (acc, locale) => {\n // Determine if the current locale is the default locale\n const isDefaultLocale = locale.toString() === defaultLocale.toString();\n\n // Decide whether to prefix the default locale based on `prefixDefault`\n const shouldPrefix = prefixDefault || !isDefaultLocale;\n\n // Construct the new pathname with or without the locale prefix\n const localizedPath = shouldPrefix ? `/${locale}${pathname}` : pathname;\n\n // Combine with the base URL if the original URL was absolute\n const localizedUrl = isAbsoluteUrl\n ? `${baseUrl}${localizedPath}${parsedUrl.search}${parsedUrl.hash}`\n : `${localizedPath}${parsedUrl.search}${parsedUrl.hash}`;\n\n // Assign the constructed URL to the corresponding locale key\n acc[locale as unknown as keyof IConfigLocales<string>] = localizedUrl;\n\n return acc;\n },\n {} as IConfigLocales<string> // Initialize an empty object\n );\n\n return multilingualUrls;\n};\n"],"mappings":"AAAA,SAAS,wBAAiC;AAG1C;AAAA,EACE;AAAA,EACA;AAAA,OACK;AAGP,MAAM,EAAE,sBAAsB,WAAW,IAAI,iBAAiB;AAC9D,MAAM,EAAE,SAAS,gBAAgB,eAAe,qBAAqB,IACnE;AACF,MAAM,EAAE,eAAe,qBAAqB,IAAI;AAuBzC,MAAM,sBAAsB,CACjC,KACA,UAAqB,gBACrB,gBAAyB,sBACzB,gBAAyB,yBACE;AAE3B,QAAM,mBAAmB,qBAAqB,KAAK,OAAO;AAG1D,QAAM,gBAAgB,mBAAmB,gBAAgB;AAIzD,QAAM,OAAO,gBAAgB,SAAY;AACzC,QAAM,YAAY,IAAI,IAAI,kBAAkB,IAAI;AAGhD,MAAI,WAAW,UAAU;AAGzB,MAAI,CAAC,SAAS,WAAW,GAAG,GAAG;AAC7B,eAAW,IAAI,QAAQ;AAAA,EACzB;AAGA,QAAM,UAAU,gBACZ,GAAG,UAAU,QAAQ,KAAK,UAAU,IAAI,KACxC;AAGJ,QAAM,mBAAmB,QAAQ;AAAA,IAC/B,CAAC,KAAK,WAAW;AAEf,YAAM,kBAAkB,OAAO,SAAS,MAAM,cAAc,SAAS;AAGrE,YAAM,eAAe,iBAAiB,CAAC;AAGvC,YAAM,gBAAgB,eAAe,IAAI,MAAM,GAAG,QAAQ,KAAK;AAG/D,YAAM,eAAe,gBACjB,GAAG,OAAO,GAAG,aAAa,GAAG,UAAU,MAAM,GAAG,UAAU,IAAI,KAC9D,GAAG,aAAa,GAAG,UAAU,MAAM,GAAG,UAAU,IAAI;AAGxD,UAAI,MAAiD,IAAI;AAEzD,aAAO;AAAA,IACT;AAAA,IACA,CAAC;AAAA;AAAA,EACH;AAEA,SAAO;AACT;","names":[]}
|
|
@@ -1,17 +1,35 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
)
|
|
6
|
-
|
|
1
|
+
import { getConfiguration } from "@intlayer/config/client";
|
|
2
|
+
const { internationalization } = getConfiguration();
|
|
3
|
+
const { locales: localesDefault } = internationalization;
|
|
4
|
+
const removeLocaleFromUrl = (inputUrl, locales = localesDefault) => {
|
|
5
|
+
console.log("inputUrl", inputUrl);
|
|
6
|
+
const url = new URL(inputUrl);
|
|
7
|
+
const pathname = url.pathname;
|
|
8
|
+
if (!pathname.startsWith("/")) {
|
|
9
|
+
url.pathname = `/${pathname}`;
|
|
7
10
|
}
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
+
const pathSegments = pathname.split("/");
|
|
12
|
+
const firstSegment = pathSegments[1];
|
|
13
|
+
if (locales.includes(firstSegment)) {
|
|
14
|
+
pathSegments.splice(1, 1);
|
|
15
|
+
const newPathname = pathSegments.join("/") ?? "/";
|
|
16
|
+
url.pathname = newPathname;
|
|
11
17
|
}
|
|
12
|
-
return
|
|
18
|
+
return url.toString();
|
|
13
19
|
};
|
|
20
|
+
const getPathWithoutLocale = (inputUrl, locales = localesDefault) => {
|
|
21
|
+
const isAbsoluteUrl = checkIsAbsoluteUrl(inputUrl);
|
|
22
|
+
const url = isAbsoluteUrl ? new URL(inputUrl) : new URL(inputUrl, "http://example.com");
|
|
23
|
+
const urlWithoutLocale = removeLocaleFromUrl(url.toString(), locales);
|
|
24
|
+
if (isAbsoluteUrl) {
|
|
25
|
+
return urlWithoutLocale;
|
|
26
|
+
}
|
|
27
|
+
return urlWithoutLocale.replace("http://example.com", "");
|
|
28
|
+
};
|
|
29
|
+
const checkIsAbsoluteUrl = (url) => /^[a-zA-Z][a-zA-Z\d+\-.]*:/.test(url);
|
|
14
30
|
export {
|
|
15
|
-
|
|
31
|
+
checkIsAbsoluteUrl,
|
|
32
|
+
getPathWithoutLocale,
|
|
33
|
+
removeLocaleFromUrl
|
|
16
34
|
};
|
|
17
35
|
//# sourceMappingURL=getPathWithoutLocale.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/getPathWithoutLocale.ts"],"sourcesContent":["import { Locales } from '@intlayer/config';\n\nexport const
|
|
1
|
+
{"version":3,"sources":["../../src/getPathWithoutLocale.ts"],"sourcesContent":["import { getConfiguration, type Locales } from '@intlayer/config/client';\n\nconst { internationalization } = getConfiguration();\nconst { locales: localesDefault } = internationalization;\n\n/**\n * Removes the locale segment from the given URL if present.\n *\n * This function get the locales from the configuration if not provided.\n *\n * Example:\n *\n * ```ts\n * removeLocaleFromUrl('https://example.com/en/dashboard') // Returns 'https://example.com/dashboard'\n * removeLocaleFromUrl('https://example.com/fr/dashboard') // Returns 'https://example.com/fr/dashboard'\n * removeLocaleFromUrl('https://example.com/dashboard') // Returns 'https://example.com/dashboard'\n * ```\n *\n * @param inputUrl - The complete URL string to process.\n * @param locales - Optional array of supported locales. Defaults to `localesDefault`.\n * @returns The URL string without the locale segment.\n */\nexport const removeLocaleFromUrl = (\n inputUrl: string,\n locales: Locales[] = localesDefault\n): string => {\n console.log('inputUrl', inputUrl);\n // Parse the input URL\n const url = new URL(inputUrl);\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 // Return the modified URL as a string\n return url.toString();\n};\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: Locales[] = localesDefault\n): string => {\n // Determine if the input is an absolute URL\n const isAbsoluteUrl = checkIsAbsoluteUrl(inputUrl);\n\n // If it's an absolute URL, parse it using the URL constructor\n // Otherwise, handle it as a relative pathname\n const url = isAbsoluteUrl\n ? new URL(inputUrl)\n : new URL(inputUrl, 'http://example.com');\n\n const urlWithoutLocale = removeLocaleFromUrl(url.toString(), locales);\n\n if (isAbsoluteUrl) {\n // If the input was an absolute URL, return the modified URL as a string\n return urlWithoutLocale;\n }\n\n // If the input was a relative pathname, return the modified pathname\n return urlWithoutLocale.replace('http://example.com', '');\n};\n\nexport const checkIsAbsoluteUrl = (url: string): boolean =>\n /^[a-zA-Z][a-zA-Z\\d+\\-.]*:/.test(url);\n"],"mappings":"AAAA,SAAS,wBAAsC;AAE/C,MAAM,EAAE,qBAAqB,IAAI,iBAAiB;AAClD,MAAM,EAAE,SAAS,eAAe,IAAI;AAmB7B,MAAM,sBAAsB,CACjC,UACA,UAAqB,mBACV;AACX,UAAQ,IAAI,YAAY,QAAQ;AAEhC,QAAM,MAAM,IAAI,IAAI,QAAQ;AAE5B,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;AAGA,SAAO,IAAI,SAAS;AACtB;AAuBO,MAAM,uBAAuB,CAClC,UACA,UAAqB,mBACV;AAEX,QAAM,gBAAgB,mBAAmB,QAAQ;AAIjD,QAAM,MAAM,gBACR,IAAI,IAAI,QAAQ,IAChB,IAAI,IAAI,UAAU,oBAAoB;AAE1C,QAAM,mBAAmB,oBAAoB,IAAI,SAAS,GAAG,OAAO;AAEpE,MAAI,eAAe;AAEjB,WAAO;AAAA,EACT;AAGA,SAAO,iBAAiB,QAAQ,sBAAsB,EAAE;AAC1D;AAEO,MAAM,qBAAqB,CAAC,QACjC,4BAA4B,KAAK,GAAG;","names":[]}
|
package/dist/esm/index.mjs
CHANGED
|
@@ -13,6 +13,7 @@ import { localeList } from './localeList.mjs';
|
|
|
13
13
|
import { isSameKeyPath } from './utils/isSameKeyPath.mjs';
|
|
14
14
|
import { localeDetector } from './localeDetector.mjs';
|
|
15
15
|
import { getPathWithoutLocale } from './getPathWithoutLocale.mjs';
|
|
16
|
+
import { getMultilingualUrls } from './getMultilingualUrls.mjs';
|
|
16
17
|
export {
|
|
17
18
|
NodeType,
|
|
18
19
|
enu,
|
|
@@ -21,6 +22,7 @@ export {
|
|
|
21
22
|
getHTMLLang,
|
|
22
23
|
getHTMLTextDir,
|
|
23
24
|
getLocaleName,
|
|
25
|
+
getMultilingualUrls,
|
|
24
26
|
getPathWithoutLocale,
|
|
25
27
|
getTranslationContent,
|
|
26
28
|
isSameKeyPath,
|
package/dist/esm/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export type {\n CustomizableLanguageContent,\n LanguageContent,\n TranslationContent,\n EnumerationContent,\n QuantityContent,\n CustomLocales as Locales,\n} from './transpiler/content_transformers/index';\nexport {\n t,\n enu,\n getTranslationContent,\n findMatchingCondition,\n getEnumerationContent,\n} from './transpiler/content_transformers/index';\nexport type {\n ContentValue,\n Content,\n FlatContentValue,\n FlatContent,\n TypedNode,\n DeclarationContent,\n KeyPath,\n ObjectNode,\n ArrayNode,\n RecursiveDictionaryValue,\n TranslationNode,\n EnumerationNode,\n DictionaryValue,\n Dictionary,\n} from './types/index';\nexport { NodeType } from './types/index';\nexport { getLocaleName } from './getLocaleName';\nexport { getHTMLTextDir } from './getHTMLTextDir';\nexport { getHTMLLang } from './getHTMLLang';\nexport { localeList } from './localeList';\nexport { isSameKeyPath } from './utils/isSameKeyPath';\nexport { localeDetector } from './localeDetector';\nexport { getPathWithoutLocale } from './getPathWithoutLocale';\n"],"mappings":"AAQA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAiBP,SAAS,gBAAgB;AACzB,SAAS,qBAAqB;AAC9B,SAAS,sBAAsB;AAC/B,SAAS,mBAAmB;AAC5B,SAAS,kBAAkB;AAC3B,SAAS,qBAAqB;AAC9B,SAAS,sBAAsB;AAC/B,SAAS,4BAA4B;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["export type {\n CustomizableLanguageContent,\n LanguageContent,\n TranslationContent,\n EnumerationContent,\n QuantityContent,\n CustomLocales as Locales,\n} from './transpiler/content_transformers/index';\nexport {\n t,\n enu,\n getTranslationContent,\n findMatchingCondition,\n getEnumerationContent,\n} from './transpiler/content_transformers/index';\nexport type {\n ContentValue,\n Content,\n FlatContentValue,\n FlatContent,\n TypedNode,\n DeclarationContent,\n KeyPath,\n ObjectNode,\n ArrayNode,\n RecursiveDictionaryValue,\n TranslationNode,\n EnumerationNode,\n DictionaryValue,\n Dictionary,\n} from './types/index';\nexport { NodeType } from './types/index';\nexport { getLocaleName } from './getLocaleName';\nexport { getHTMLTextDir } from './getHTMLTextDir';\nexport { getHTMLLang } from './getHTMLLang';\nexport { localeList } from './localeList';\nexport { isSameKeyPath } from './utils/isSameKeyPath';\nexport { localeDetector } from './localeDetector';\nexport { getPathWithoutLocale } from './getPathWithoutLocale';\nexport { getMultilingualUrls } from './getMultilingualUrls';\n"],"mappings":"AAQA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAiBP,SAAS,gBAAgB;AACzB,SAAS,qBAAqB;AAC9B,SAAS,sBAAsB;AAC/B,SAAS,mBAAmB;AAC5B,SAAS,kBAAkB;AAC3B,SAAS,qBAAqB;AAC9B,SAAS,sBAAsB;AAC/B,SAAS,4BAA4B;AACrC,SAAS,2BAA2B;","names":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/localeDetector.ts"],"sourcesContent":["import { match } from '@formatjs/intl-localematcher';\nimport
|
|
1
|
+
{"version":3,"sources":["../../src/localeDetector.ts"],"sourcesContent":["import { match } from '@formatjs/intl-localematcher';\nimport { getConfiguration, type Locales } from '@intlayer/config/client';\nimport Negotiator from 'negotiator';\n\nconst { locales: envLocales, defaultLocale: defaultLocaleEnv } =\n getConfiguration().internationalization;\n\n/**\n * Detects the locale from the request headers\n *\n * Headers are provided by the browser and can be used to determine the user's preferred language\n */\nexport const localeDetector = (\n headers: Record<string, string | undefined>,\n locales: Locales[] = envLocales,\n defaultLocale: Locales = defaultLocaleEnv\n): Locales => {\n const languages = new Negotiator({ headers }).languages();\n\n // match can only use specifically formatted locales\n // https://stackoverflow.com/questions/76447732/nextjs-13-i18n-incorrect-locale-information-provided\n try {\n return match(languages, locales, defaultLocale) as Locales;\n } catch (_error) {\n return defaultLocale;\n }\n};\n"],"mappings":"AAAA,SAAS,aAAa;AACtB,SAAS,wBAAsC;AAC/C,OAAO,gBAAgB;AAEvB,MAAM,EAAE,SAAS,YAAY,eAAe,iBAAiB,IAC3D,iBAAiB,EAAE;AAOd,MAAM,iBAAiB,CAC5B,SACA,UAAqB,YACrB,gBAAyB,qBACb;AACZ,QAAM,YAAY,IAAI,WAAW,EAAE,QAAQ,CAAC,EAAE,UAAU;AAIxD,MAAI;AACF,WAAO,MAAM,WAAW,SAAS,aAAa;AAAA,EAChD,SAAS,QAAQ;AACf,WAAO;AAAA,EACT;AACF;","names":[]}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Locales } from '@intlayer/config/client';
|
|
2
|
+
import type { IConfigLocales } from 'intlayer';
|
|
3
|
+
/**
|
|
4
|
+
* Generates multilingual URLs by prefixing the given URL with each supported locale.
|
|
5
|
+
* Handles both absolute and relative URLs appropriately.
|
|
6
|
+
*
|
|
7
|
+
* This function get the locales, default locale, and prefix default from the configuration if not provided.
|
|
8
|
+
*
|
|
9
|
+
* Example:
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* getMultilingualUrls('/dashboard', ['en', 'fr'], 'en', false)
|
|
13
|
+
* // Returns { en: '/dashboard', fr: '/fr/dashboard' }
|
|
14
|
+
* getMultilingualUrls('https://example.com/dashboard', ['en', 'fr'], 'en', true)
|
|
15
|
+
* // Returns { en: 'https://example.com/en/dashboard', fr: 'https://example.com/fr/dashboard' }
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* @param url - The original URL string to be prefixed with locales.
|
|
19
|
+
* @param locales - Optional array of supported locales. Defaults to `localesDefault`.
|
|
20
|
+
* @param defaultLocale - The default locale. Defaults to `defaultLocaleDefault`.
|
|
21
|
+
* @param prefixDefault - Whether to prefix the default locale. Defaults to `prefixDefaultDefault`.
|
|
22
|
+
* @returns An object mapping each locale to its corresponding multilingual URL.
|
|
23
|
+
*/
|
|
24
|
+
export declare const getMultilingualUrls: (url: string, locales?: Locales[], defaultLocale?: Locales, prefixDefault?: boolean) => IConfigLocales<string>;
|
|
25
|
+
//# sourceMappingURL=getMultilingualUrls.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getMultilingualUrls.d.ts","sourceRoot":"","sources":["../../src/getMultilingualUrls.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,OAAO,EAAE,MAAM,yBAAyB,CAAC;AAEpE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAY/C;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,eAAO,MAAM,mBAAmB,QACzB,MAAM,YACF,OAAO,EAAE,kBACH,OAAO,kBACP,OAAO,KACrB,cAAc,CAAC,MAAM,CAmDvB,CAAC"}
|
|
@@ -1,3 +1,43 @@
|
|
|
1
|
-
import { Locales } from '@intlayer/config';
|
|
2
|
-
|
|
1
|
+
import { type Locales } from '@intlayer/config/client';
|
|
2
|
+
/**
|
|
3
|
+
* Removes the locale segment from the given URL if present.
|
|
4
|
+
*
|
|
5
|
+
* This function get the locales from the configuration if not provided.
|
|
6
|
+
*
|
|
7
|
+
* Example:
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* removeLocaleFromUrl('https://example.com/en/dashboard') // Returns 'https://example.com/dashboard'
|
|
11
|
+
* removeLocaleFromUrl('https://example.com/fr/dashboard') // Returns 'https://example.com/fr/dashboard'
|
|
12
|
+
* removeLocaleFromUrl('https://example.com/dashboard') // Returns 'https://example.com/dashboard'
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* @param inputUrl - The complete URL string to process.
|
|
16
|
+
* @param locales - Optional array of supported locales. Defaults to `localesDefault`.
|
|
17
|
+
* @returns The URL string without the locale segment.
|
|
18
|
+
*/
|
|
19
|
+
export declare const removeLocaleFromUrl: (inputUrl: string, locales?: Locales[]) => string;
|
|
20
|
+
/**
|
|
21
|
+
* Removes the locale segment from the given URL or pathname if present.
|
|
22
|
+
*
|
|
23
|
+
* This function get the locales from the configuration if not provided.
|
|
24
|
+
*
|
|
25
|
+
* Example:
|
|
26
|
+
*
|
|
27
|
+
* ```ts
|
|
28
|
+
* getPathWithoutLocale('/en/dashboard') // Returns '/dashboard'
|
|
29
|
+
* getPathWithoutLocale('/fr/dashboard') // Returns '/dashboard'
|
|
30
|
+
* getPathWithoutLocale('/dashboard') // Returns '/dashboard'
|
|
31
|
+
* getPathWithoutLocale('dashboard') // Returns 'dashboard'
|
|
32
|
+
* getPathWithoutLocale('https://example.com/en/dashboard') // Returns 'https://example.com/dashboard'
|
|
33
|
+
* getPathWithoutLocale('https://example.com/fr/dashboard') // Returns 'https://example.com/dashboard'
|
|
34
|
+
* getPathWithoutLocale('https://example.com/dashboard') // Returns 'https://example.com/dashboard'
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* @param inputUrl - The complete URL string or pathname to process.
|
|
38
|
+
* @param locales - Optional array of supported locales. Defaults to `localesDefault`.
|
|
39
|
+
* @returns The URL string or pathname without the locale segment.
|
|
40
|
+
*/
|
|
41
|
+
export declare const getPathWithoutLocale: (inputUrl: string, locales?: Locales[]) => string;
|
|
42
|
+
export declare const checkIsAbsoluteUrl: (url: string) => boolean;
|
|
3
43
|
//# sourceMappingURL=getPathWithoutLocale.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getPathWithoutLocale.d.ts","sourceRoot":"","sources":["../../src/getPathWithoutLocale.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"getPathWithoutLocale.d.ts","sourceRoot":"","sources":["../../src/getPathWithoutLocale.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,KAAK,OAAO,EAAE,MAAM,yBAAyB,CAAC;AAKzE;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,mBAAmB,aACpB,MAAM,YACP,OAAO,EAAE,KACjB,MA6BF,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,eAAO,MAAM,oBAAoB,aACrB,MAAM,YACP,OAAO,EAAE,KACjB,MAmBF,CAAC;AAEF,eAAO,MAAM,kBAAkB,QAAS,MAAM,KAAG,OACV,CAAC"}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -9,4 +9,5 @@ export { localeList } from './localeList';
|
|
|
9
9
|
export { isSameKeyPath } from './utils/isSameKeyPath';
|
|
10
10
|
export { localeDetector } from './localeDetector';
|
|
11
11
|
export { getPathWithoutLocale } from './getPathWithoutLocale';
|
|
12
|
+
export { getMultilingualUrls } from './getMultilingualUrls';
|
|
12
13
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,2BAA2B,EAC3B,eAAe,EACf,kBAAkB,EAClB,kBAAkB,EAClB,eAAe,EACf,aAAa,IAAI,OAAO,GACzB,MAAM,yCAAyC,CAAC;AACjD,OAAO,EACL,CAAC,EACD,GAAG,EACH,qBAAqB,EACrB,qBAAqB,EACrB,qBAAqB,GACtB,MAAM,yCAAyC,CAAC;AACjD,YAAY,EACV,YAAY,EACZ,OAAO,EACP,gBAAgB,EAChB,WAAW,EACX,SAAS,EACT,kBAAkB,EAClB,OAAO,EACP,UAAU,EACV,SAAS,EACT,wBAAwB,EACxB,eAAe,EACf,eAAe,EACf,eAAe,EACf,UAAU,GACX,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AACtD,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,2BAA2B,EAC3B,eAAe,EACf,kBAAkB,EAClB,kBAAkB,EAClB,eAAe,EACf,aAAa,IAAI,OAAO,GACzB,MAAM,yCAAyC,CAAC;AACjD,OAAO,EACL,CAAC,EACD,GAAG,EACH,qBAAqB,EACrB,qBAAqB,EACrB,qBAAqB,GACtB,MAAM,yCAAyC,CAAC;AACjD,YAAY,EACV,YAAY,EACZ,OAAO,EACP,gBAAgB,EAChB,WAAW,EACX,SAAS,EACT,kBAAkB,EAClB,OAAO,EACP,UAAU,EACV,SAAS,EACT,wBAAwB,EACxB,eAAe,EACf,eAAe,EACf,eAAe,EACf,UAAU,GACX,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AACtD,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AAC9D,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"localeDetector.d.ts","sourceRoot":"","sources":["../../src/localeDetector.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"localeDetector.d.ts","sourceRoot":"","sources":["../../src/localeDetector.ts"],"names":[],"mappings":"AACA,OAAO,EAAoB,KAAK,OAAO,EAAE,MAAM,yBAAyB,CAAC;AAMzE;;;;GAIG;AACH,eAAO,MAAM,cAAc,YAChB,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,YAClC,OAAO,EAAE,kBACH,OAAO,KACrB,OAUF,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@intlayer/core",
|
|
3
|
-
"version": "3.5.
|
|
3
|
+
"version": "3.5.1",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "IntLayer - Layer of abstraction between the business logic and the data access layer. Manage internationalization in a simple way, through TypeScript, JavaScript or JSON declaration file.",
|
|
6
6
|
"keywords": [
|
|
@@ -61,7 +61,7 @@
|
|
|
61
61
|
"@formatjs/intl-localematcher": "^0.5.4",
|
|
62
62
|
"chokidar": "^3.6.0",
|
|
63
63
|
"negotiator": "^0.6.3",
|
|
64
|
-
"@intlayer/config": "^3.5.
|
|
64
|
+
"@intlayer/config": "^3.5.1"
|
|
65
65
|
},
|
|
66
66
|
"devDependencies": {
|
|
67
67
|
"@types/negotiator": "^0.6.3",
|
|
@@ -74,14 +74,14 @@
|
|
|
74
74
|
"tsc-alias": "^1.8.10",
|
|
75
75
|
"tsup": "^8.3.5",
|
|
76
76
|
"typescript": "^5.7.2",
|
|
77
|
-
"@utils/ts-config": "^1.0.4",
|
|
78
77
|
"@utils/ts-config-types": "^1.0.4",
|
|
79
|
-
"@utils/
|
|
80
|
-
"@utils/
|
|
78
|
+
"@utils/tsup-config": "^1.0.4",
|
|
79
|
+
"@utils/ts-config": "^1.0.4",
|
|
80
|
+
"@utils/eslint-config": "^1.0.4"
|
|
81
81
|
},
|
|
82
82
|
"peerDependencies": {
|
|
83
83
|
"react": ">=16.0.0 <19.0.0",
|
|
84
|
-
"@intlayer/config": "^3.5.
|
|
84
|
+
"@intlayer/config": "^3.5.1"
|
|
85
85
|
},
|
|
86
86
|
"engines": {
|
|
87
87
|
"node": ">=14.18"
|