@inlang/paraglide-js 2.0.10 → 2.0.12
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/compiler/compiler-options.d.ts +9 -0
- package/dist/compiler/compiler-options.d.ts.map +1 -1
- package/dist/compiler/compiler-options.js +1 -0
- package/dist/compiler/create-paraglide.d.ts +20 -14
- package/dist/compiler/create-paraglide.d.ts.map +1 -1
- package/dist/compiler/create-paraglide.js +18 -14
- package/dist/compiler/create-paraglide.test.js +7 -11
- package/dist/compiler/index.d.ts +1 -1
- package/dist/compiler/index.d.ts.map +1 -1
- package/dist/compiler/index.js +1 -1
- package/dist/compiler/runtime/assert-is-locale.test.js +3 -3
- package/dist/compiler/runtime/create-runtime.d.ts +1 -0
- package/dist/compiler/runtime/create-runtime.d.ts.map +1 -1
- package/dist/compiler/runtime/create-runtime.js +1 -0
- package/dist/compiler/runtime/extract-locale-from-cookie.test.js +7 -11
- package/dist/compiler/runtime/extract-locale-from-request.test.js +57 -77
- package/dist/compiler/runtime/extract-locale-from-url.test.js +51 -61
- package/dist/compiler/runtime/generate-static-localized-urls.test.js +48 -58
- package/dist/compiler/runtime/get-locale.test.js +54 -69
- package/dist/compiler/runtime/get-url-origin.test.js +2 -4
- package/dist/compiler/runtime/localize-href.test.js +48 -58
- package/dist/compiler/runtime/localize-url.test.js +258 -294
- package/dist/compiler/runtime/set-locale.d.ts.map +1 -1
- package/dist/compiler/runtime/set-locale.js +6 -3
- package/dist/compiler/runtime/set-locale.test.js +108 -70
- package/dist/compiler/runtime/track-message-call.test.js +1 -1
- package/dist/compiler/runtime/variables.d.ts +2 -0
- package/dist/compiler/runtime/variables.d.ts.map +1 -1
- package/dist/compiler/runtime/variables.js +2 -0
- package/dist/compiler/server/middleware.test.js +85 -109
- package/dist/services/env-variables/index.js +1 -1
- package/package.json +5 -5
|
@@ -3,23 +3,21 @@ import { createParaglide } from "../create-paraglide.js";
|
|
|
3
3
|
import { newProject } from "@inlang/sdk";
|
|
4
4
|
test("normal named groups", async () => {
|
|
5
5
|
const runtime = await createParaglide({
|
|
6
|
-
|
|
6
|
+
blob: await newProject({
|
|
7
7
|
settings: {
|
|
8
8
|
baseLocale: "en",
|
|
9
9
|
locales: ["en", "de"],
|
|
10
10
|
},
|
|
11
11
|
}),
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
],
|
|
22
|
-
},
|
|
12
|
+
urlPatterns: [
|
|
13
|
+
{
|
|
14
|
+
pattern: "https://example.com/bookstore/item/:id",
|
|
15
|
+
localized: [
|
|
16
|
+
["de", "https://example.com/buchladen/artikel/:id"],
|
|
17
|
+
["en", "https://example.com/bookstore/item/:id"],
|
|
18
|
+
],
|
|
19
|
+
},
|
|
20
|
+
],
|
|
23
21
|
});
|
|
24
22
|
expect(runtime.extractLocaleFromUrl(`https://example.com/bookstore/item/123`)).toBe("en");
|
|
25
23
|
expect(runtime.extractLocaleFromUrl(`https://example.com/buchladen/artikel/123`)).toBe("de");
|
|
@@ -27,23 +25,21 @@ test("normal named groups", async () => {
|
|
|
27
25
|
});
|
|
28
26
|
test("handles relative named groups", async () => {
|
|
29
27
|
const runtime = await createParaglide({
|
|
30
|
-
|
|
28
|
+
blob: await newProject({
|
|
31
29
|
settings: {
|
|
32
30
|
baseLocale: "en",
|
|
33
31
|
locales: ["en", "de"],
|
|
34
32
|
},
|
|
35
33
|
}),
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
],
|
|
46
|
-
},
|
|
34
|
+
urlPatterns: [
|
|
35
|
+
{
|
|
36
|
+
pattern: "/bookstore/item/:id",
|
|
37
|
+
localized: [
|
|
38
|
+
["de", "/buchladen/artikel/:id"],
|
|
39
|
+
["en", "/bookstore/item/:id"],
|
|
40
|
+
],
|
|
41
|
+
},
|
|
42
|
+
],
|
|
47
43
|
});
|
|
48
44
|
expect(runtime.extractLocaleFromUrl(`https://example.com/bookstore/item/123`)).toBe("en");
|
|
49
45
|
expect(runtime.extractLocaleFromUrl(`https://example.com/buchladen/artikel/123`)).toBe("de");
|
|
@@ -51,69 +47,63 @@ test("handles relative named groups", async () => {
|
|
|
51
47
|
});
|
|
52
48
|
test("wildcards", async () => {
|
|
53
49
|
const runtime = await createParaglide({
|
|
54
|
-
|
|
50
|
+
blob: await newProject({
|
|
55
51
|
settings: {
|
|
56
52
|
baseLocale: "en",
|
|
57
53
|
locales: ["en", "de"],
|
|
58
54
|
},
|
|
59
55
|
}),
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
{
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
],
|
|
70
|
-
},
|
|
56
|
+
urlPatterns: [
|
|
57
|
+
{
|
|
58
|
+
pattern: "https://{:subdomain.}?:domain.:tld/:path*",
|
|
59
|
+
localized: [
|
|
60
|
+
["de", "https://de.:domain.:tld/startseite/ueber-uns"],
|
|
61
|
+
["en", "https://:domain.:tld/home/about-us"],
|
|
62
|
+
],
|
|
63
|
+
},
|
|
64
|
+
],
|
|
71
65
|
});
|
|
72
66
|
expect(runtime.extractLocaleFromUrl(`https://de.example.com/startseite/ueber-uns`)).toBe("de");
|
|
73
67
|
expect(runtime.extractLocaleFromUrl(`https://example.com/home/about-us`)).toBe("en");
|
|
74
68
|
});
|
|
75
69
|
test("optional parameters", async () => {
|
|
76
70
|
const runtime = await createParaglide({
|
|
77
|
-
|
|
71
|
+
blob: await newProject({
|
|
78
72
|
settings: {
|
|
79
73
|
baseLocale: "en",
|
|
80
74
|
locales: ["en", "de"],
|
|
81
75
|
},
|
|
82
76
|
}),
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
],
|
|
93
|
-
},
|
|
77
|
+
urlPatterns: [
|
|
78
|
+
{
|
|
79
|
+
pattern: "https://example.com/:locale?",
|
|
80
|
+
localized: [
|
|
81
|
+
["de", "https://example.com/de"],
|
|
82
|
+
["en", "https://example.com/en"],
|
|
83
|
+
],
|
|
84
|
+
},
|
|
85
|
+
],
|
|
94
86
|
});
|
|
95
87
|
expect(runtime.extractLocaleFromUrl(`https://example.com/de`)).toBe("de");
|
|
96
88
|
expect(runtime.extractLocaleFromUrl(`https://example.com/en`)).toBe("en");
|
|
97
89
|
});
|
|
98
90
|
test("regex works", async () => {
|
|
99
91
|
const { extractLocaleFromUrl } = await createParaglide({
|
|
100
|
-
|
|
92
|
+
blob: await newProject({
|
|
101
93
|
settings: {
|
|
102
94
|
baseLocale: "en",
|
|
103
95
|
locales: ["en", "de"],
|
|
104
96
|
},
|
|
105
97
|
}),
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
],
|
|
116
|
-
},
|
|
98
|
+
urlPatterns: [
|
|
99
|
+
{
|
|
100
|
+
pattern: "https://example.com/:item(phone)",
|
|
101
|
+
localized: [
|
|
102
|
+
["en", "https://example.com/:item(phone)"],
|
|
103
|
+
["de", "https://example.com/de/:item(phone)"],
|
|
104
|
+
],
|
|
105
|
+
},
|
|
106
|
+
],
|
|
117
107
|
});
|
|
118
108
|
expect(extractLocaleFromUrl(`https://example.com/de/phone`)).toBe("de");
|
|
119
109
|
expect(extractLocaleFromUrl(`https://example.com/phone`)).toBe("en");
|
|
@@ -122,7 +112,7 @@ test("regex works", async () => {
|
|
|
122
112
|
});
|
|
123
113
|
test("default url pattern", async () => {
|
|
124
114
|
const r = await createParaglide({
|
|
125
|
-
|
|
115
|
+
blob: await newProject({
|
|
126
116
|
settings: {
|
|
127
117
|
baseLocale: "en",
|
|
128
118
|
locales: ["en", "de"],
|
|
@@ -4,17 +4,15 @@ import { newProject } from "@inlang/sdk";
|
|
|
4
4
|
import "@inlang/paraglide-js/urlpattern-polyfill";
|
|
5
5
|
test("generates localized URLs using default URL pattern", async () => {
|
|
6
6
|
const runtime = await createParaglide({
|
|
7
|
-
|
|
7
|
+
blob: await newProject({
|
|
8
8
|
settings: {
|
|
9
9
|
baseLocale: "en",
|
|
10
10
|
locales: ["en", "de", "fr"],
|
|
11
11
|
},
|
|
12
12
|
}),
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
urlPatterns: undefined,
|
|
17
|
-
},
|
|
13
|
+
strategy: ["url"],
|
|
14
|
+
// undefined creates the default pattern
|
|
15
|
+
urlPatterns: undefined,
|
|
18
16
|
});
|
|
19
17
|
const urls = runtime.generateStaticLocalizedUrls([
|
|
20
18
|
"http://example.com/about",
|
|
@@ -38,31 +36,29 @@ test("generates localized URLs using default URL pattern", async () => {
|
|
|
38
36
|
});
|
|
39
37
|
test("generates localized URLs using custom URL patterns", async () => {
|
|
40
38
|
const runtime = await createParaglide({
|
|
41
|
-
|
|
39
|
+
blob: await newProject({
|
|
42
40
|
settings: {
|
|
43
41
|
baseLocale: "en",
|
|
44
42
|
locales: ["en", "de"],
|
|
45
43
|
},
|
|
46
44
|
}),
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
],
|
|
65
|
-
},
|
|
45
|
+
strategy: ["url"],
|
|
46
|
+
urlPatterns: [
|
|
47
|
+
{
|
|
48
|
+
pattern: "/store/item/:id",
|
|
49
|
+
localized: [
|
|
50
|
+
["en", "/store/item/:id"],
|
|
51
|
+
["de", "/laden/artikel/:id"],
|
|
52
|
+
],
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
pattern: "/blog/:slug",
|
|
56
|
+
localized: [
|
|
57
|
+
["en", "/blog/:slug"],
|
|
58
|
+
["de", "/blog/:slug"], // Same pattern for both locales
|
|
59
|
+
],
|
|
60
|
+
},
|
|
61
|
+
],
|
|
66
62
|
});
|
|
67
63
|
const urls = runtime.generateStaticLocalizedUrls([
|
|
68
64
|
"https://example.com/store/item/123",
|
|
@@ -82,24 +78,22 @@ test("generates localized URLs using custom URL patterns", async () => {
|
|
|
82
78
|
});
|
|
83
79
|
test("handles paths that don't match any pattern by including them", async () => {
|
|
84
80
|
const runtime = await createParaglide({
|
|
85
|
-
|
|
81
|
+
blob: await newProject({
|
|
86
82
|
settings: {
|
|
87
83
|
baseLocale: "en",
|
|
88
84
|
locales: ["en", "de"],
|
|
89
85
|
},
|
|
90
86
|
}),
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
],
|
|
102
|
-
},
|
|
87
|
+
strategy: ["url"],
|
|
88
|
+
urlPatterns: [
|
|
89
|
+
{
|
|
90
|
+
pattern: "https://example.com/store/:path*",
|
|
91
|
+
localized: [
|
|
92
|
+
["en", "https://example.com/store/:path*"],
|
|
93
|
+
["de", "https://example.com/laden/:path*"],
|
|
94
|
+
],
|
|
95
|
+
},
|
|
96
|
+
],
|
|
103
97
|
});
|
|
104
98
|
const urls = runtime.generateStaticLocalizedUrls([
|
|
105
99
|
"https://example.com/store/item/123", // Should match pattern
|
|
@@ -118,24 +112,22 @@ test("handles paths that don't match any pattern by including them", async () =>
|
|
|
118
112
|
});
|
|
119
113
|
test("handles URL objects as input", async () => {
|
|
120
114
|
const runtime = await createParaglide({
|
|
121
|
-
|
|
115
|
+
blob: await newProject({
|
|
122
116
|
settings: {
|
|
123
117
|
baseLocale: "en",
|
|
124
118
|
locales: ["en", "de"],
|
|
125
119
|
},
|
|
126
120
|
}),
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
],
|
|
138
|
-
},
|
|
121
|
+
strategy: ["url"],
|
|
122
|
+
urlPatterns: [
|
|
123
|
+
{
|
|
124
|
+
pattern: "/store/:path*",
|
|
125
|
+
localized: [
|
|
126
|
+
["en", "/store/:path*"],
|
|
127
|
+
["de", "/laden/:path*"],
|
|
128
|
+
],
|
|
129
|
+
},
|
|
130
|
+
],
|
|
139
131
|
});
|
|
140
132
|
const urls = runtime.generateStaticLocalizedUrls([
|
|
141
133
|
new URL("https://example.com/store/item/123?color=blue#reviews"),
|
|
@@ -156,17 +148,15 @@ test("handles URL objects as input", async () => {
|
|
|
156
148
|
});
|
|
157
149
|
test("generates localized URLs from paths", async () => {
|
|
158
150
|
const runtime = await createParaglide({
|
|
159
|
-
|
|
151
|
+
blob: await newProject({
|
|
160
152
|
settings: {
|
|
161
153
|
baseLocale: "en",
|
|
162
154
|
locales: ["en", "de", "fr"],
|
|
163
155
|
},
|
|
164
156
|
}),
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
urlPatterns: undefined,
|
|
169
|
-
},
|
|
157
|
+
strategy: ["url"],
|
|
158
|
+
// undefined creates the default pattern
|
|
159
|
+
urlPatterns: undefined,
|
|
170
160
|
});
|
|
171
161
|
const urls = runtime.generateStaticLocalizedUrls([
|
|
172
162
|
"/about",
|
|
@@ -4,16 +4,14 @@ import { createParaglide } from "../create-paraglide.js";
|
|
|
4
4
|
test("matching by strategy works", async () => {
|
|
5
5
|
const baseLocale = "en";
|
|
6
6
|
const runtime = await createParaglide({
|
|
7
|
-
|
|
7
|
+
blob: await newProject({
|
|
8
8
|
settings: {
|
|
9
9
|
baseLocale,
|
|
10
10
|
locales: ["en", "de"],
|
|
11
11
|
},
|
|
12
12
|
}),
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
cookieName: "PARAGLIDE_LOCALE",
|
|
16
|
-
},
|
|
13
|
+
strategy: ["cookie", "baseLocale"],
|
|
14
|
+
cookieName: "PARAGLIDE_LOCALE",
|
|
17
15
|
});
|
|
18
16
|
// @ts-expect-error - global variable definition
|
|
19
17
|
globalThis.document = {};
|
|
@@ -23,15 +21,13 @@ test("matching by strategy works", async () => {
|
|
|
23
21
|
});
|
|
24
22
|
test("throws if variable is used without baseLocale as fallback strategy", async () => {
|
|
25
23
|
const runtime = await createParaglide({
|
|
26
|
-
|
|
24
|
+
blob: await newProject({
|
|
27
25
|
settings: {
|
|
28
26
|
baseLocale: "en",
|
|
29
27
|
locales: ["en", "de"],
|
|
30
28
|
},
|
|
31
29
|
}),
|
|
32
|
-
|
|
33
|
-
strategy: ["globalVariable"],
|
|
34
|
-
},
|
|
30
|
+
strategy: ["globalVariable"],
|
|
35
31
|
});
|
|
36
32
|
expect(() => runtime.getLocale()).toThrow();
|
|
37
33
|
runtime.setLocale("de");
|
|
@@ -39,25 +35,23 @@ test("throws if variable is used without baseLocale as fallback strategy", async
|
|
|
39
35
|
});
|
|
40
36
|
test("retrieves the locale for a url pattern", async () => {
|
|
41
37
|
const runtime = await createParaglide({
|
|
42
|
-
|
|
38
|
+
blob: await newProject({
|
|
43
39
|
settings: {
|
|
44
40
|
baseLocale: "en",
|
|
45
41
|
locales: ["en", "de"],
|
|
46
42
|
},
|
|
47
43
|
}),
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
],
|
|
60
|
-
},
|
|
44
|
+
strategy: ["url"],
|
|
45
|
+
isServer: "false",
|
|
46
|
+
urlPatterns: [
|
|
47
|
+
{
|
|
48
|
+
pattern: "https://example.:tld/:path*",
|
|
49
|
+
localized: [
|
|
50
|
+
["en", "https://example.com/:path*"],
|
|
51
|
+
["de", "https://example.de/:path*"],
|
|
52
|
+
],
|
|
53
|
+
},
|
|
54
|
+
],
|
|
61
55
|
});
|
|
62
56
|
globalThis.window = { location: { href: "https://example.com/page" } };
|
|
63
57
|
expect(runtime.getLocale()).toBe("en");
|
|
@@ -66,40 +60,36 @@ test("retrieves the locale for a url pattern", async () => {
|
|
|
66
60
|
});
|
|
67
61
|
test("url pattern strategy doesn't throw during SSR", async () => {
|
|
68
62
|
const runtime = await createParaglide({
|
|
69
|
-
|
|
63
|
+
blob: await newProject({
|
|
70
64
|
settings: {
|
|
71
65
|
baseLocale: "en",
|
|
72
66
|
locales: ["en", "de"],
|
|
73
67
|
},
|
|
74
68
|
}),
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
],
|
|
86
|
-
},
|
|
69
|
+
strategy: ["url", "baseLocale"],
|
|
70
|
+
urlPatterns: [
|
|
71
|
+
{
|
|
72
|
+
pattern: "https://example.:tld/:path*",
|
|
73
|
+
localized: [
|
|
74
|
+
["en", "https://example.com/:path*"],
|
|
75
|
+
["de", "https://example.de/:path*"],
|
|
76
|
+
],
|
|
77
|
+
},
|
|
78
|
+
],
|
|
87
79
|
});
|
|
88
80
|
expect(() => runtime.getLocale()).not.toThrow();
|
|
89
81
|
});
|
|
90
82
|
test("doesn't throw for an old cookie locale", async () => {
|
|
91
83
|
const baseLocale = "en";
|
|
92
84
|
const runtime = await createParaglide({
|
|
93
|
-
|
|
85
|
+
blob: await newProject({
|
|
94
86
|
settings: {
|
|
95
87
|
baseLocale: "en",
|
|
96
88
|
locales: ["en", "de"],
|
|
97
89
|
},
|
|
98
90
|
}),
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
cookieName: "PARAGLIDE_LOCALE",
|
|
102
|
-
},
|
|
91
|
+
strategy: ["cookie", "baseLocale"],
|
|
92
|
+
cookieName: "PARAGLIDE_LOCALE",
|
|
103
93
|
});
|
|
104
94
|
// @ts-expect-error - global variable definition
|
|
105
95
|
globalThis.document = {};
|
|
@@ -110,16 +100,14 @@ test("doesn't throw for an old cookie locale", async () => {
|
|
|
110
100
|
test("returns the preferred locale from navigator.languages", async () => {
|
|
111
101
|
const originalNavigator = globalThis.navigator;
|
|
112
102
|
const runtime = await createParaglide({
|
|
113
|
-
|
|
103
|
+
blob: await newProject({
|
|
114
104
|
settings: {
|
|
115
105
|
baseLocale: "en",
|
|
116
106
|
locales: ["en", "fr", "de"],
|
|
117
107
|
},
|
|
118
108
|
}),
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
strategy: ["preferredLanguage"],
|
|
122
|
-
},
|
|
109
|
+
isServer: "false",
|
|
110
|
+
strategy: ["preferredLanguage"],
|
|
123
111
|
});
|
|
124
112
|
// Mock navigator.languages
|
|
125
113
|
Object.defineProperty(globalThis, "navigator", {
|
|
@@ -137,17 +125,15 @@ test("returns the preferred locale from navigator.languages", async () => {
|
|
|
137
125
|
});
|
|
138
126
|
test("returns the locale from local storage", async () => {
|
|
139
127
|
const runtime = await createParaglide({
|
|
140
|
-
|
|
128
|
+
blob: await newProject({
|
|
141
129
|
settings: {
|
|
142
130
|
baseLocale: "en",
|
|
143
131
|
locales: ["en", "de"],
|
|
144
132
|
},
|
|
145
133
|
}),
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
isServer: "false",
|
|
150
|
-
},
|
|
134
|
+
strategy: ["localStorage"],
|
|
135
|
+
localStorageKey: "PARAGLIDE_LOCALE",
|
|
136
|
+
isServer: "false",
|
|
151
137
|
});
|
|
152
138
|
// @ts-expect-error - global variable definition
|
|
153
139
|
globalThis.localStorage = {
|
|
@@ -159,32 +145,31 @@ test("returns the locale from local storage", async () => {
|
|
|
159
145
|
test("initially sets the locale after resolving it for the first time", async () => {
|
|
160
146
|
// Create runtime with multiple strategies
|
|
161
147
|
const runtime = await createParaglide({
|
|
162
|
-
|
|
148
|
+
blob: await newProject({
|
|
163
149
|
settings: {
|
|
164
150
|
baseLocale: "en",
|
|
165
151
|
locales: ["en", "de", "fr"],
|
|
166
152
|
},
|
|
167
153
|
}),
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
],
|
|
182
|
-
},
|
|
154
|
+
strategy: ["url", "cookie"],
|
|
155
|
+
isServer: "false",
|
|
156
|
+
cookieName: "PARAGLIDE_LOCALE",
|
|
157
|
+
urlPatterns: [
|
|
158
|
+
{
|
|
159
|
+
pattern: "https://example.com/:path*",
|
|
160
|
+
localized: [
|
|
161
|
+
["en", "https://example.com/en/:path*"],
|
|
162
|
+
["de", "https://example.com/de/:path*"],
|
|
163
|
+
["fr", "https://example.com/fr/:path*"],
|
|
164
|
+
],
|
|
165
|
+
},
|
|
166
|
+
],
|
|
183
167
|
});
|
|
184
168
|
// Setup global objects for URL strategy
|
|
185
169
|
globalThis.window = {
|
|
186
170
|
// @ts-expect-error - global variable definition
|
|
187
171
|
location: {
|
|
172
|
+
hostname: "example.com",
|
|
188
173
|
href: "https://example.com/de/page",
|
|
189
174
|
},
|
|
190
175
|
};
|
|
@@ -195,6 +180,6 @@ test("initially sets the locale after resolving it for the first time", async ()
|
|
|
195
180
|
// First call to getLocale should resolve and set the locale
|
|
196
181
|
expect(runtime.getLocale()).toBe("de");
|
|
197
182
|
// Cookie should be set, proving that the locale was initially set
|
|
198
|
-
expect(globalThis.document.cookie).toBe("PARAGLIDE_LOCALE=de; path=/; max-age=34560000");
|
|
183
|
+
expect(globalThis.document.cookie).toBe("PARAGLIDE_LOCALE=de; path=/; max-age=34560000; domain=example.com");
|
|
199
184
|
expect(globalThis.window.location.href).toBe("https://example.com/de/page");
|
|
200
185
|
});
|
|
@@ -3,25 +3,23 @@ import { createParaglide } from "../create-paraglide.js";
|
|
|
3
3
|
import { newProject } from "@inlang/sdk";
|
|
4
4
|
test("returns a placeholder in server environment as placeholder to make dependent apis work", async () => {
|
|
5
5
|
const runtime = await createParaglide({
|
|
6
|
-
|
|
6
|
+
blob: await newProject({
|
|
7
7
|
settings: {
|
|
8
8
|
baseLocale: "en",
|
|
9
9
|
locales: ["en", "de"],
|
|
10
10
|
},
|
|
11
11
|
}),
|
|
12
|
-
compilerOptions: {},
|
|
13
12
|
});
|
|
14
13
|
expect(runtime.getUrlOrigin()).toBeDefined();
|
|
15
14
|
});
|
|
16
15
|
test("returns the window.location.origin if available", async () => {
|
|
17
16
|
const runtime = await createParaglide({
|
|
18
|
-
|
|
17
|
+
blob: await newProject({
|
|
19
18
|
settings: {
|
|
20
19
|
baseLocale: "en",
|
|
21
20
|
locales: ["en", "de"],
|
|
22
21
|
},
|
|
23
22
|
}),
|
|
24
|
-
compilerOptions: {},
|
|
25
23
|
});
|
|
26
24
|
globalThis.window = { location: { origin: "https://example.com" } };
|
|
27
25
|
expect(runtime.getUrlOrigin()).toBe("https://example.com");
|