intor 2.4.14 → 2.4.16
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/core/export/index.js +2 -1
- package/dist/core/src/core/locale/canonicalize-locale.js +23 -0
- package/dist/core/src/core/locale/match-locale.js +57 -0
- package/dist/core/src/core/locale/parse-locale.js +27 -0
- package/dist/core/src/routing/inbound/resolve-locale/resolve-locale.js +3 -3
- package/dist/express/src/core/locale/canonicalize-locale.js +23 -0
- package/dist/express/src/core/locale/match-locale.js +57 -0
- package/dist/express/src/core/locale/parse-locale.js +27 -0
- package/dist/express/src/routing/inbound/resolve-locale/resolve-locale.js +3 -3
- package/dist/fastify/src/core/locale/canonicalize-locale.js +23 -0
- package/dist/fastify/src/core/locale/match-locale.js +57 -0
- package/dist/fastify/src/core/locale/parse-locale.js +27 -0
- package/dist/fastify/src/routing/inbound/resolve-locale/resolve-locale.js +3 -3
- package/dist/hono/src/core/locale/canonicalize-locale.js +23 -0
- package/dist/hono/src/core/locale/match-locale.js +57 -0
- package/dist/hono/src/core/locale/parse-locale.js +27 -0
- package/dist/hono/src/routing/inbound/resolve-locale/resolve-locale.js +3 -3
- package/dist/next/src/adapters/next/server/get-locale.js +4 -4
- package/dist/next/src/core/locale/canonicalize-locale.js +23 -0
- package/dist/next/src/core/locale/match-locale.js +57 -0
- package/dist/next/src/core/locale/parse-locale.js +27 -0
- package/dist/next/src/routing/inbound/resolve-locale/resolve-locale.js +3 -3
- package/dist/react/src/client/react/provider/intor-provider.js +6 -0
- package/dist/react/src/client/shared/helpers/get-client-locale.js +2 -2
- package/dist/react/src/core/locale/canonicalize-locale.js +23 -0
- package/dist/react/src/core/locale/match-locale.js +57 -0
- package/dist/react/src/core/locale/parse-locale.js +27 -0
- package/dist/svelte/src/client/shared/helpers/get-client-locale.js +2 -2
- package/dist/svelte/src/client/svelte/provider/create-intor-store.js +3 -1
- package/dist/svelte/src/core/locale/canonicalize-locale.js +23 -0
- package/dist/svelte/src/core/locale/match-locale.js +57 -0
- package/dist/svelte/src/core/locale/parse-locale.js +27 -0
- package/dist/svelte-kit/src/core/locale/canonicalize-locale.js +23 -0
- package/dist/svelte-kit/src/core/locale/match-locale.js +57 -0
- package/dist/svelte-kit/src/core/locale/parse-locale.js +27 -0
- package/dist/svelte-kit/src/routing/inbound/resolve-locale/resolve-locale.js +3 -3
- package/dist/types/export/index.d.ts +2 -1
- package/dist/types/src/core/index.d.ts +2 -1
- package/dist/types/src/core/locale/canonicalize-locale.d.ts +11 -0
- package/dist/types/src/core/locale/index.d.ts +1 -0
- package/dist/types/src/core/locale/match-locale.d.ts +16 -0
- package/dist/types/src/core/locale/parse-locale.d.ts +14 -0
- package/dist/types/src/core/utils/index.d.ts +1 -1
- package/dist/types/src/core/utils/normalizers/index.d.ts +0 -1
- package/dist/vue/src/client/shared/helpers/get-client-locale.js +2 -2
- package/dist/vue/src/client/vue/provider/intor-provider.js +21 -14
- package/dist/vue/src/core/locale/canonicalize-locale.js +23 -0
- package/dist/vue/src/core/locale/match-locale.js +57 -0
- package/dist/vue/src/core/locale/parse-locale.js +27 -0
- package/package.json +27 -27
- package/dist/core/src/core/utils/normalizers/normalize-locale.js +0 -59
- package/dist/express/src/core/utils/normalizers/normalize-locale.js +0 -59
- package/dist/fastify/src/core/utils/normalizers/normalize-locale.js +0 -59
- package/dist/hono/src/core/utils/normalizers/normalize-locale.js +0 -59
- package/dist/next/src/core/utils/normalizers/normalize-locale.js +0 -59
- package/dist/react/src/core/utils/normalizers/normalize-locale.js +0 -59
- package/dist/svelte/src/core/utils/normalizers/normalize-locale.js +0 -59
- package/dist/svelte-kit/src/core/utils/normalizers/normalize-locale.js +0 -59
- package/dist/types/src/core/utils/normalizers/normalize-locale.d.ts +0 -23
- package/dist/vue/src/core/utils/normalizers/normalize-locale.js +0 -59
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
export { LOCALE_PLACEHOLDER } from '../src/core/constants/locale-placeholder.js';
|
|
2
2
|
export { IntorError, IntorErrorCode } from '../src/core/error/intor-error.js';
|
|
3
|
+
export { matchLocale } from '../src/core/locale/match-locale.js';
|
|
3
4
|
import 'logry';
|
|
4
5
|
export { clearLoggerPool } from '../src/core/logger/global-logger-pool.js';
|
|
5
6
|
import 'p-limit';
|
|
6
7
|
export { mergeMessages } from '../src/core/messages/merge-messages.js';
|
|
7
|
-
|
|
8
|
+
export { Translator } from 'intor-translator';
|
|
8
9
|
export { defineIntorConfig } from '../src/config/define-intor-config.js';
|
|
9
10
|
import '../src/config/constants/cookie.js';
|
|
10
11
|
export { localizePathname } from '../src/routing/pathname/localize-pathname.js';
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Canonicalizes a BCP 47 locale string.
|
|
3
|
+
*
|
|
4
|
+
* - Uses `Intl.getCanonicalLocales` when available.
|
|
5
|
+
* - Returns the original input if `Intl` is unavailable.
|
|
6
|
+
* - Returns `undefined` for invalid locale input.
|
|
7
|
+
*
|
|
8
|
+
* This function performs normalization only.
|
|
9
|
+
* It does not perform matching or fallback.
|
|
10
|
+
*/
|
|
11
|
+
function canonicalizeLocale(input) {
|
|
12
|
+
try {
|
|
13
|
+
if (typeof Intl === "undefined" || !Intl.getCanonicalLocales) {
|
|
14
|
+
return input;
|
|
15
|
+
}
|
|
16
|
+
return Intl.getCanonicalLocales(input)[0];
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export { canonicalizeLocale };
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { canonicalizeLocale } from './canonicalize-locale.js';
|
|
2
|
+
import { parseLocale } from './parse-locale.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Matches a locale candidate against a list of supported locales.
|
|
6
|
+
*
|
|
7
|
+
* Resolution order:
|
|
8
|
+
* 1. Exact canonical match
|
|
9
|
+
* 2. Same language + same script
|
|
10
|
+
* 3. Same language only (only if candidate has no script)
|
|
11
|
+
*
|
|
12
|
+
* Returns `undefined` if no match is found.
|
|
13
|
+
*
|
|
14
|
+
* Notes:
|
|
15
|
+
* - Matching is deterministic and order-sensitive.
|
|
16
|
+
* - Does not perform automatic fallback to default locale.
|
|
17
|
+
* - Does not cross script boundaries.
|
|
18
|
+
*/
|
|
19
|
+
function matchLocale(locale, supportedLocales = []) {
|
|
20
|
+
if (!locale || supportedLocales.length === 0)
|
|
21
|
+
return;
|
|
22
|
+
const canonicalCandidate = canonicalizeLocale(locale);
|
|
23
|
+
if (!canonicalCandidate)
|
|
24
|
+
return;
|
|
25
|
+
const candidateParts = parseLocale(canonicalCandidate);
|
|
26
|
+
const supportedCanonical = supportedLocales.flatMap((original) => {
|
|
27
|
+
const canonical = canonicalizeLocale(original);
|
|
28
|
+
if (!canonical)
|
|
29
|
+
return [];
|
|
30
|
+
return [{ original, canonical, parts: parseLocale(canonical) }];
|
|
31
|
+
});
|
|
32
|
+
// 1. Exact match
|
|
33
|
+
for (const s of supportedCanonical) {
|
|
34
|
+
if (s.canonical === canonicalCandidate) {
|
|
35
|
+
return s.original;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
// 2. Same language + same script
|
|
39
|
+
if (candidateParts.script) {
|
|
40
|
+
for (const s of supportedCanonical) {
|
|
41
|
+
if (s.parts.language === candidateParts.language &&
|
|
42
|
+
s.parts.script === candidateParts.script) {
|
|
43
|
+
return s.original;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
// 3. Same language only
|
|
49
|
+
for (const s of supportedCanonical) {
|
|
50
|
+
if (s.parts.language === candidateParts.language) {
|
|
51
|
+
return s.original;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export { matchLocale };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parses a canonical locale tag into structural components.
|
|
3
|
+
*
|
|
4
|
+
* - Uses `Intl.Locale` when available.
|
|
5
|
+
* - Falls back to minimal parsing (language only) if unavailable.
|
|
6
|
+
*
|
|
7
|
+
* This function does not validate or match locales.
|
|
8
|
+
*/
|
|
9
|
+
function parseLocale(tag) {
|
|
10
|
+
try {
|
|
11
|
+
if (typeof Intl !== "undefined" && typeof Intl.Locale === "function") {
|
|
12
|
+
const locale = new Intl.Locale(tag);
|
|
13
|
+
return {
|
|
14
|
+
language: locale.language,
|
|
15
|
+
script: locale.script,
|
|
16
|
+
region: locale.region,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
// fallback below
|
|
22
|
+
}
|
|
23
|
+
const parts = tag.split("-");
|
|
24
|
+
return { language: parts[0].toLowerCase() };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export { parseLocale };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import '../../../core/error/intor-error.js';
|
|
2
|
-
import {
|
|
2
|
+
import { matchLocale } from '../../../core/locale/match-locale.js';
|
|
3
3
|
import 'logry';
|
|
4
4
|
import 'p-limit';
|
|
5
5
|
import 'intor-translator';
|
|
@@ -15,7 +15,7 @@ function resolveLocale(config, context) {
|
|
|
15
15
|
const { localeSources } = config.routing.inbound;
|
|
16
16
|
for (const source of localeSources) {
|
|
17
17
|
const locale = context[source]?.locale;
|
|
18
|
-
const normalized =
|
|
18
|
+
const normalized = matchLocale(locale, config.supportedLocales);
|
|
19
19
|
if (!normalized)
|
|
20
20
|
continue;
|
|
21
21
|
return {
|
|
@@ -24,7 +24,7 @@ function resolveLocale(config, context) {
|
|
|
24
24
|
};
|
|
25
25
|
}
|
|
26
26
|
// Fallback: detected is always available
|
|
27
|
-
const fallback =
|
|
27
|
+
const fallback = matchLocale(context.detected.locale, config.supportedLocales) ||
|
|
28
28
|
config.defaultLocale;
|
|
29
29
|
return {
|
|
30
30
|
locale: fallback,
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Canonicalizes a BCP 47 locale string.
|
|
3
|
+
*
|
|
4
|
+
* - Uses `Intl.getCanonicalLocales` when available.
|
|
5
|
+
* - Returns the original input if `Intl` is unavailable.
|
|
6
|
+
* - Returns `undefined` for invalid locale input.
|
|
7
|
+
*
|
|
8
|
+
* This function performs normalization only.
|
|
9
|
+
* It does not perform matching or fallback.
|
|
10
|
+
*/
|
|
11
|
+
function canonicalizeLocale(input) {
|
|
12
|
+
try {
|
|
13
|
+
if (typeof Intl === "undefined" || !Intl.getCanonicalLocales) {
|
|
14
|
+
return input;
|
|
15
|
+
}
|
|
16
|
+
return Intl.getCanonicalLocales(input)[0];
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export { canonicalizeLocale };
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { canonicalizeLocale } from './canonicalize-locale.js';
|
|
2
|
+
import { parseLocale } from './parse-locale.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Matches a locale candidate against a list of supported locales.
|
|
6
|
+
*
|
|
7
|
+
* Resolution order:
|
|
8
|
+
* 1. Exact canonical match
|
|
9
|
+
* 2. Same language + same script
|
|
10
|
+
* 3. Same language only (only if candidate has no script)
|
|
11
|
+
*
|
|
12
|
+
* Returns `undefined` if no match is found.
|
|
13
|
+
*
|
|
14
|
+
* Notes:
|
|
15
|
+
* - Matching is deterministic and order-sensitive.
|
|
16
|
+
* - Does not perform automatic fallback to default locale.
|
|
17
|
+
* - Does not cross script boundaries.
|
|
18
|
+
*/
|
|
19
|
+
function matchLocale(locale, supportedLocales = []) {
|
|
20
|
+
if (!locale || supportedLocales.length === 0)
|
|
21
|
+
return;
|
|
22
|
+
const canonicalCandidate = canonicalizeLocale(locale);
|
|
23
|
+
if (!canonicalCandidate)
|
|
24
|
+
return;
|
|
25
|
+
const candidateParts = parseLocale(canonicalCandidate);
|
|
26
|
+
const supportedCanonical = supportedLocales.flatMap((original) => {
|
|
27
|
+
const canonical = canonicalizeLocale(original);
|
|
28
|
+
if (!canonical)
|
|
29
|
+
return [];
|
|
30
|
+
return [{ original, canonical, parts: parseLocale(canonical) }];
|
|
31
|
+
});
|
|
32
|
+
// 1. Exact match
|
|
33
|
+
for (const s of supportedCanonical) {
|
|
34
|
+
if (s.canonical === canonicalCandidate) {
|
|
35
|
+
return s.original;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
// 2. Same language + same script
|
|
39
|
+
if (candidateParts.script) {
|
|
40
|
+
for (const s of supportedCanonical) {
|
|
41
|
+
if (s.parts.language === candidateParts.language &&
|
|
42
|
+
s.parts.script === candidateParts.script) {
|
|
43
|
+
return s.original;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
// 3. Same language only
|
|
49
|
+
for (const s of supportedCanonical) {
|
|
50
|
+
if (s.parts.language === candidateParts.language) {
|
|
51
|
+
return s.original;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export { matchLocale };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parses a canonical locale tag into structural components.
|
|
3
|
+
*
|
|
4
|
+
* - Uses `Intl.Locale` when available.
|
|
5
|
+
* - Falls back to minimal parsing (language only) if unavailable.
|
|
6
|
+
*
|
|
7
|
+
* This function does not validate or match locales.
|
|
8
|
+
*/
|
|
9
|
+
function parseLocale(tag) {
|
|
10
|
+
try {
|
|
11
|
+
if (typeof Intl !== "undefined" && typeof Intl.Locale === "function") {
|
|
12
|
+
const locale = new Intl.Locale(tag);
|
|
13
|
+
return {
|
|
14
|
+
language: locale.language,
|
|
15
|
+
script: locale.script,
|
|
16
|
+
region: locale.region,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
// fallback below
|
|
22
|
+
}
|
|
23
|
+
const parts = tag.split("-");
|
|
24
|
+
return { language: parts[0].toLowerCase() };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export { parseLocale };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import '../../../core/error/intor-error.js';
|
|
2
|
-
import {
|
|
2
|
+
import { matchLocale } from '../../../core/locale/match-locale.js';
|
|
3
3
|
import 'logry';
|
|
4
4
|
import 'p-limit';
|
|
5
5
|
import 'intor-translator';
|
|
@@ -15,7 +15,7 @@ function resolveLocale(config, context) {
|
|
|
15
15
|
const { localeSources } = config.routing.inbound;
|
|
16
16
|
for (const source of localeSources) {
|
|
17
17
|
const locale = context[source]?.locale;
|
|
18
|
-
const normalized =
|
|
18
|
+
const normalized = matchLocale(locale, config.supportedLocales);
|
|
19
19
|
if (!normalized)
|
|
20
20
|
continue;
|
|
21
21
|
return {
|
|
@@ -24,7 +24,7 @@ function resolveLocale(config, context) {
|
|
|
24
24
|
};
|
|
25
25
|
}
|
|
26
26
|
// Fallback: detected is always available
|
|
27
|
-
const fallback =
|
|
27
|
+
const fallback = matchLocale(context.detected.locale, config.supportedLocales) ||
|
|
28
28
|
config.defaultLocale;
|
|
29
29
|
return {
|
|
30
30
|
locale: fallback,
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Canonicalizes a BCP 47 locale string.
|
|
3
|
+
*
|
|
4
|
+
* - Uses `Intl.getCanonicalLocales` when available.
|
|
5
|
+
* - Returns the original input if `Intl` is unavailable.
|
|
6
|
+
* - Returns `undefined` for invalid locale input.
|
|
7
|
+
*
|
|
8
|
+
* This function performs normalization only.
|
|
9
|
+
* It does not perform matching or fallback.
|
|
10
|
+
*/
|
|
11
|
+
function canonicalizeLocale(input) {
|
|
12
|
+
try {
|
|
13
|
+
if (typeof Intl === "undefined" || !Intl.getCanonicalLocales) {
|
|
14
|
+
return input;
|
|
15
|
+
}
|
|
16
|
+
return Intl.getCanonicalLocales(input)[0];
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export { canonicalizeLocale };
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { canonicalizeLocale } from './canonicalize-locale.js';
|
|
2
|
+
import { parseLocale } from './parse-locale.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Matches a locale candidate against a list of supported locales.
|
|
6
|
+
*
|
|
7
|
+
* Resolution order:
|
|
8
|
+
* 1. Exact canonical match
|
|
9
|
+
* 2. Same language + same script
|
|
10
|
+
* 3. Same language only (only if candidate has no script)
|
|
11
|
+
*
|
|
12
|
+
* Returns `undefined` if no match is found.
|
|
13
|
+
*
|
|
14
|
+
* Notes:
|
|
15
|
+
* - Matching is deterministic and order-sensitive.
|
|
16
|
+
* - Does not perform automatic fallback to default locale.
|
|
17
|
+
* - Does not cross script boundaries.
|
|
18
|
+
*/
|
|
19
|
+
function matchLocale(locale, supportedLocales = []) {
|
|
20
|
+
if (!locale || supportedLocales.length === 0)
|
|
21
|
+
return;
|
|
22
|
+
const canonicalCandidate = canonicalizeLocale(locale);
|
|
23
|
+
if (!canonicalCandidate)
|
|
24
|
+
return;
|
|
25
|
+
const candidateParts = parseLocale(canonicalCandidate);
|
|
26
|
+
const supportedCanonical = supportedLocales.flatMap((original) => {
|
|
27
|
+
const canonical = canonicalizeLocale(original);
|
|
28
|
+
if (!canonical)
|
|
29
|
+
return [];
|
|
30
|
+
return [{ original, canonical, parts: parseLocale(canonical) }];
|
|
31
|
+
});
|
|
32
|
+
// 1. Exact match
|
|
33
|
+
for (const s of supportedCanonical) {
|
|
34
|
+
if (s.canonical === canonicalCandidate) {
|
|
35
|
+
return s.original;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
// 2. Same language + same script
|
|
39
|
+
if (candidateParts.script) {
|
|
40
|
+
for (const s of supportedCanonical) {
|
|
41
|
+
if (s.parts.language === candidateParts.language &&
|
|
42
|
+
s.parts.script === candidateParts.script) {
|
|
43
|
+
return s.original;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
// 3. Same language only
|
|
49
|
+
for (const s of supportedCanonical) {
|
|
50
|
+
if (s.parts.language === candidateParts.language) {
|
|
51
|
+
return s.original;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export { matchLocale };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parses a canonical locale tag into structural components.
|
|
3
|
+
*
|
|
4
|
+
* - Uses `Intl.Locale` when available.
|
|
5
|
+
* - Falls back to minimal parsing (language only) if unavailable.
|
|
6
|
+
*
|
|
7
|
+
* This function does not validate or match locales.
|
|
8
|
+
*/
|
|
9
|
+
function parseLocale(tag) {
|
|
10
|
+
try {
|
|
11
|
+
if (typeof Intl !== "undefined" && typeof Intl.Locale === "function") {
|
|
12
|
+
const locale = new Intl.Locale(tag);
|
|
13
|
+
return {
|
|
14
|
+
language: locale.language,
|
|
15
|
+
script: locale.script,
|
|
16
|
+
region: locale.region,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
// fallback below
|
|
22
|
+
}
|
|
23
|
+
const parts = tag.split("-");
|
|
24
|
+
return { language: parts[0].toLowerCase() };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export { parseLocale };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import '../../../core/error/intor-error.js';
|
|
2
|
-
import {
|
|
2
|
+
import { matchLocale } from '../../../core/locale/match-locale.js';
|
|
3
3
|
import 'logry';
|
|
4
4
|
import 'p-limit';
|
|
5
5
|
import 'intor-translator';
|
|
@@ -15,7 +15,7 @@ function resolveLocale(config, context) {
|
|
|
15
15
|
const { localeSources } = config.routing.inbound;
|
|
16
16
|
for (const source of localeSources) {
|
|
17
17
|
const locale = context[source]?.locale;
|
|
18
|
-
const normalized =
|
|
18
|
+
const normalized = matchLocale(locale, config.supportedLocales);
|
|
19
19
|
if (!normalized)
|
|
20
20
|
continue;
|
|
21
21
|
return {
|
|
@@ -24,7 +24,7 @@ function resolveLocale(config, context) {
|
|
|
24
24
|
};
|
|
25
25
|
}
|
|
26
26
|
// Fallback: detected is always available
|
|
27
|
-
const fallback =
|
|
27
|
+
const fallback = matchLocale(context.detected.locale, config.supportedLocales) ||
|
|
28
28
|
config.defaultLocale;
|
|
29
29
|
return {
|
|
30
30
|
locale: fallback,
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Canonicalizes a BCP 47 locale string.
|
|
3
|
+
*
|
|
4
|
+
* - Uses `Intl.getCanonicalLocales` when available.
|
|
5
|
+
* - Returns the original input if `Intl` is unavailable.
|
|
6
|
+
* - Returns `undefined` for invalid locale input.
|
|
7
|
+
*
|
|
8
|
+
* This function performs normalization only.
|
|
9
|
+
* It does not perform matching or fallback.
|
|
10
|
+
*/
|
|
11
|
+
function canonicalizeLocale(input) {
|
|
12
|
+
try {
|
|
13
|
+
if (typeof Intl === "undefined" || !Intl.getCanonicalLocales) {
|
|
14
|
+
return input;
|
|
15
|
+
}
|
|
16
|
+
return Intl.getCanonicalLocales(input)[0];
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export { canonicalizeLocale };
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { canonicalizeLocale } from './canonicalize-locale.js';
|
|
2
|
+
import { parseLocale } from './parse-locale.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Matches a locale candidate against a list of supported locales.
|
|
6
|
+
*
|
|
7
|
+
* Resolution order:
|
|
8
|
+
* 1. Exact canonical match
|
|
9
|
+
* 2. Same language + same script
|
|
10
|
+
* 3. Same language only (only if candidate has no script)
|
|
11
|
+
*
|
|
12
|
+
* Returns `undefined` if no match is found.
|
|
13
|
+
*
|
|
14
|
+
* Notes:
|
|
15
|
+
* - Matching is deterministic and order-sensitive.
|
|
16
|
+
* - Does not perform automatic fallback to default locale.
|
|
17
|
+
* - Does not cross script boundaries.
|
|
18
|
+
*/
|
|
19
|
+
function matchLocale(locale, supportedLocales = []) {
|
|
20
|
+
if (!locale || supportedLocales.length === 0)
|
|
21
|
+
return;
|
|
22
|
+
const canonicalCandidate = canonicalizeLocale(locale);
|
|
23
|
+
if (!canonicalCandidate)
|
|
24
|
+
return;
|
|
25
|
+
const candidateParts = parseLocale(canonicalCandidate);
|
|
26
|
+
const supportedCanonical = supportedLocales.flatMap((original) => {
|
|
27
|
+
const canonical = canonicalizeLocale(original);
|
|
28
|
+
if (!canonical)
|
|
29
|
+
return [];
|
|
30
|
+
return [{ original, canonical, parts: parseLocale(canonical) }];
|
|
31
|
+
});
|
|
32
|
+
// 1. Exact match
|
|
33
|
+
for (const s of supportedCanonical) {
|
|
34
|
+
if (s.canonical === canonicalCandidate) {
|
|
35
|
+
return s.original;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
// 2. Same language + same script
|
|
39
|
+
if (candidateParts.script) {
|
|
40
|
+
for (const s of supportedCanonical) {
|
|
41
|
+
if (s.parts.language === candidateParts.language &&
|
|
42
|
+
s.parts.script === candidateParts.script) {
|
|
43
|
+
return s.original;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
// 3. Same language only
|
|
49
|
+
for (const s of supportedCanonical) {
|
|
50
|
+
if (s.parts.language === candidateParts.language) {
|
|
51
|
+
return s.original;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export { matchLocale };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parses a canonical locale tag into structural components.
|
|
3
|
+
*
|
|
4
|
+
* - Uses `Intl.Locale` when available.
|
|
5
|
+
* - Falls back to minimal parsing (language only) if unavailable.
|
|
6
|
+
*
|
|
7
|
+
* This function does not validate or match locales.
|
|
8
|
+
*/
|
|
9
|
+
function parseLocale(tag) {
|
|
10
|
+
try {
|
|
11
|
+
if (typeof Intl !== "undefined" && typeof Intl.Locale === "function") {
|
|
12
|
+
const locale = new Intl.Locale(tag);
|
|
13
|
+
return {
|
|
14
|
+
language: locale.language,
|
|
15
|
+
script: locale.script,
|
|
16
|
+
region: locale.region,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
// fallback below
|
|
22
|
+
}
|
|
23
|
+
const parts = tag.split("-");
|
|
24
|
+
return { language: parts[0].toLowerCase() };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export { parseLocale };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import '../../../core/error/intor-error.js';
|
|
2
|
-
import {
|
|
2
|
+
import { matchLocale } from '../../../core/locale/match-locale.js';
|
|
3
3
|
import 'logry';
|
|
4
4
|
import 'p-limit';
|
|
5
5
|
import 'intor-translator';
|
|
@@ -15,7 +15,7 @@ function resolveLocale(config, context) {
|
|
|
15
15
|
const { localeSources } = config.routing.inbound;
|
|
16
16
|
for (const source of localeSources) {
|
|
17
17
|
const locale = context[source]?.locale;
|
|
18
|
-
const normalized =
|
|
18
|
+
const normalized = matchLocale(locale, config.supportedLocales);
|
|
19
19
|
if (!normalized)
|
|
20
20
|
continue;
|
|
21
21
|
return {
|
|
@@ -24,7 +24,7 @@ function resolveLocale(config, context) {
|
|
|
24
24
|
};
|
|
25
25
|
}
|
|
26
26
|
// Fallback: detected is always available
|
|
27
|
-
const fallback =
|
|
27
|
+
const fallback = matchLocale(context.detected.locale, config.supportedLocales) ||
|
|
28
28
|
config.defaultLocale;
|
|
29
29
|
return {
|
|
30
30
|
locale: fallback,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { headers, cookies } from 'next/headers';
|
|
2
2
|
import { INTOR_HEADERS } from '../../../core/constants/headers.js';
|
|
3
3
|
import '../../../core/error/intor-error.js';
|
|
4
|
-
import {
|
|
4
|
+
import { matchLocale } from '../../../core/locale/match-locale.js';
|
|
5
5
|
import 'logry';
|
|
6
6
|
import 'p-limit';
|
|
7
7
|
import 'intor-translator';
|
|
@@ -23,9 +23,9 @@ const getLocale = async (config) => {
|
|
|
23
23
|
const cookieStore = await cookies();
|
|
24
24
|
const cookieLocale = cookieStore.get(config.cookie.name)?.value;
|
|
25
25
|
if (cookieLocale) {
|
|
26
|
-
const
|
|
27
|
-
if (
|
|
28
|
-
return
|
|
26
|
+
const matched = matchLocale(cookieLocale, config.supportedLocales);
|
|
27
|
+
if (matched)
|
|
28
|
+
return matched;
|
|
29
29
|
}
|
|
30
30
|
// Explicit default
|
|
31
31
|
return config.defaultLocale;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Canonicalizes a BCP 47 locale string.
|
|
3
|
+
*
|
|
4
|
+
* - Uses `Intl.getCanonicalLocales` when available.
|
|
5
|
+
* - Returns the original input if `Intl` is unavailable.
|
|
6
|
+
* - Returns `undefined` for invalid locale input.
|
|
7
|
+
*
|
|
8
|
+
* This function performs normalization only.
|
|
9
|
+
* It does not perform matching or fallback.
|
|
10
|
+
*/
|
|
11
|
+
function canonicalizeLocale(input) {
|
|
12
|
+
try {
|
|
13
|
+
if (typeof Intl === "undefined" || !Intl.getCanonicalLocales) {
|
|
14
|
+
return input;
|
|
15
|
+
}
|
|
16
|
+
return Intl.getCanonicalLocales(input)[0];
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export { canonicalizeLocale };
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { canonicalizeLocale } from './canonicalize-locale.js';
|
|
2
|
+
import { parseLocale } from './parse-locale.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Matches a locale candidate against a list of supported locales.
|
|
6
|
+
*
|
|
7
|
+
* Resolution order:
|
|
8
|
+
* 1. Exact canonical match
|
|
9
|
+
* 2. Same language + same script
|
|
10
|
+
* 3. Same language only (only if candidate has no script)
|
|
11
|
+
*
|
|
12
|
+
* Returns `undefined` if no match is found.
|
|
13
|
+
*
|
|
14
|
+
* Notes:
|
|
15
|
+
* - Matching is deterministic and order-sensitive.
|
|
16
|
+
* - Does not perform automatic fallback to default locale.
|
|
17
|
+
* - Does not cross script boundaries.
|
|
18
|
+
*/
|
|
19
|
+
function matchLocale(locale, supportedLocales = []) {
|
|
20
|
+
if (!locale || supportedLocales.length === 0)
|
|
21
|
+
return;
|
|
22
|
+
const canonicalCandidate = canonicalizeLocale(locale);
|
|
23
|
+
if (!canonicalCandidate)
|
|
24
|
+
return;
|
|
25
|
+
const candidateParts = parseLocale(canonicalCandidate);
|
|
26
|
+
const supportedCanonical = supportedLocales.flatMap((original) => {
|
|
27
|
+
const canonical = canonicalizeLocale(original);
|
|
28
|
+
if (!canonical)
|
|
29
|
+
return [];
|
|
30
|
+
return [{ original, canonical, parts: parseLocale(canonical) }];
|
|
31
|
+
});
|
|
32
|
+
// 1. Exact match
|
|
33
|
+
for (const s of supportedCanonical) {
|
|
34
|
+
if (s.canonical === canonicalCandidate) {
|
|
35
|
+
return s.original;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
// 2. Same language + same script
|
|
39
|
+
if (candidateParts.script) {
|
|
40
|
+
for (const s of supportedCanonical) {
|
|
41
|
+
if (s.parts.language === candidateParts.language &&
|
|
42
|
+
s.parts.script === candidateParts.script) {
|
|
43
|
+
return s.original;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
// 3. Same language only
|
|
49
|
+
for (const s of supportedCanonical) {
|
|
50
|
+
if (s.parts.language === candidateParts.language) {
|
|
51
|
+
return s.original;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export { matchLocale };
|