@inlang/paraglide-js 2.0.11 → 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.
@@ -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
- project: await newProject({
6
+ blob: await newProject({
7
7
  settings: {
8
8
  baseLocale: "en",
9
9
  locales: ["en", "de"],
10
10
  },
11
11
  }),
12
- compilerOptions: {
13
- urlPatterns: [
14
- {
15
- pattern: "https://example.com/bookstore/item/:id",
16
- localized: [
17
- ["de", "https://example.com/buchladen/artikel/:id"],
18
- ["en", "https://example.com/bookstore/item/:id"],
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
- project: await newProject({
28
+ blob: await newProject({
31
29
  settings: {
32
30
  baseLocale: "en",
33
31
  locales: ["en", "de"],
34
32
  },
35
33
  }),
36
- compilerOptions: {
37
- urlPatterns: [
38
- {
39
- pattern: "/bookstore/item/:id",
40
- localized: [
41
- ["de", "/buchladen/artikel/:id"],
42
- ["en", "/bookstore/item/:id"],
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
- project: await newProject({
50
+ blob: await newProject({
55
51
  settings: {
56
52
  baseLocale: "en",
57
53
  locales: ["en", "de"],
58
54
  },
59
55
  }),
60
- compilerOptions: {
61
- urlPatterns: [
62
- {
63
- pattern: "https://{:subdomain.}?:domain.:tld/:path*",
64
- localized: [
65
- ["de", "https://de.:domain.:tld/startseite/ueber-uns"],
66
- ["en", "https://:domain.:tld/home/about-us"],
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
- project: await newProject({
71
+ blob: await newProject({
78
72
  settings: {
79
73
  baseLocale: "en",
80
74
  locales: ["en", "de"],
81
75
  },
82
76
  }),
83
- compilerOptions: {
84
- urlPatterns: [
85
- {
86
- pattern: "https://example.com/:locale?",
87
- localized: [
88
- ["de", "https://example.com/de"],
89
- ["en", "https://example.com/en"],
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
- project: await newProject({
92
+ blob: await newProject({
101
93
  settings: {
102
94
  baseLocale: "en",
103
95
  locales: ["en", "de"],
104
96
  },
105
97
  }),
106
- compilerOptions: {
107
- urlPatterns: [
108
- {
109
- pattern: "https://example.com/:item(phone)",
110
- localized: [
111
- ["en", "https://example.com/:item(phone)"],
112
- ["de", "https://example.com/de/:item(phone)"],
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
- project: await newProject({
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
- project: await newProject({
7
+ blob: await newProject({
8
8
  settings: {
9
9
  baseLocale: "en",
10
10
  locales: ["en", "de", "fr"],
11
11
  },
12
12
  }),
13
- compilerOptions: {
14
- strategy: ["url"],
15
- // undefined creates the default pattern
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
- project: await newProject({
39
+ blob: await newProject({
42
40
  settings: {
43
41
  baseLocale: "en",
44
42
  locales: ["en", "de"],
45
43
  },
46
44
  }),
47
- compilerOptions: {
48
- strategy: ["url"],
49
- urlPatterns: [
50
- {
51
- pattern: "/store/item/:id",
52
- localized: [
53
- ["en", "/store/item/:id"],
54
- ["de", "/laden/artikel/:id"],
55
- ],
56
- },
57
- {
58
- pattern: "/blog/:slug",
59
- localized: [
60
- ["en", "/blog/:slug"],
61
- ["de", "/blog/:slug"], // Same pattern for both locales
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
- project: await newProject({
81
+ blob: await newProject({
86
82
  settings: {
87
83
  baseLocale: "en",
88
84
  locales: ["en", "de"],
89
85
  },
90
86
  }),
91
- compilerOptions: {
92
- strategy: ["url"],
93
- urlPatterns: [
94
- {
95
- pattern: "https://example.com/store/:path*",
96
- localized: [
97
- ["en", "https://example.com/store/:path*"],
98
- ["de", "https://example.com/laden/:path*"],
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
- project: await newProject({
115
+ blob: await newProject({
122
116
  settings: {
123
117
  baseLocale: "en",
124
118
  locales: ["en", "de"],
125
119
  },
126
120
  }),
127
- compilerOptions: {
128
- strategy: ["url"],
129
- urlPatterns: [
130
- {
131
- pattern: "/store/:path*",
132
- localized: [
133
- ["en", "/store/:path*"],
134
- ["de", "/laden/:path*"],
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
- project: await newProject({
151
+ blob: await newProject({
160
152
  settings: {
161
153
  baseLocale: "en",
162
154
  locales: ["en", "de", "fr"],
163
155
  },
164
156
  }),
165
- compilerOptions: {
166
- strategy: ["url"],
167
- // undefined creates the default pattern
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
- project: await newProject({
7
+ blob: await newProject({
8
8
  settings: {
9
9
  baseLocale,
10
10
  locales: ["en", "de"],
11
11
  },
12
12
  }),
13
- compilerOptions: {
14
- strategy: ["cookie", "baseLocale"],
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
- project: await newProject({
24
+ blob: await newProject({
27
25
  settings: {
28
26
  baseLocale: "en",
29
27
  locales: ["en", "de"],
30
28
  },
31
29
  }),
32
- compilerOptions: {
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
- project: await newProject({
38
+ blob: await newProject({
43
39
  settings: {
44
40
  baseLocale: "en",
45
41
  locales: ["en", "de"],
46
42
  },
47
43
  }),
48
- compilerOptions: {
49
- strategy: ["url"],
50
- isServer: "false",
51
- urlPatterns: [
52
- {
53
- pattern: "https://example.:tld/:path*",
54
- localized: [
55
- ["en", "https://example.com/:path*"],
56
- ["de", "https://example.de/:path*"],
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
- project: await newProject({
63
+ blob: await newProject({
70
64
  settings: {
71
65
  baseLocale: "en",
72
66
  locales: ["en", "de"],
73
67
  },
74
68
  }),
75
- compilerOptions: {
76
- strategy: ["url", "baseLocale"],
77
- urlPatterns: [
78
- {
79
- pattern: "https://example.:tld/:path*",
80
- localized: [
81
- ["en", "https://example.com/:path*"],
82
- ["de", "https://example.de/:path*"],
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
- project: await newProject({
85
+ blob: await newProject({
94
86
  settings: {
95
87
  baseLocale: "en",
96
88
  locales: ["en", "de"],
97
89
  },
98
90
  }),
99
- compilerOptions: {
100
- strategy: ["cookie", "baseLocale"],
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
- project: await newProject({
103
+ blob: await newProject({
114
104
  settings: {
115
105
  baseLocale: "en",
116
106
  locales: ["en", "fr", "de"],
117
107
  },
118
108
  }),
119
- compilerOptions: {
120
- isServer: "false",
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
- project: await newProject({
128
+ blob: await newProject({
141
129
  settings: {
142
130
  baseLocale: "en",
143
131
  locales: ["en", "de"],
144
132
  },
145
133
  }),
146
- compilerOptions: {
147
- strategy: ["localStorage"],
148
- localStorageKey: "PARAGLIDE_LOCALE",
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,27 +145,25 @@ 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
- project: await newProject({
148
+ blob: await newProject({
163
149
  settings: {
164
150
  baseLocale: "en",
165
151
  locales: ["en", "de", "fr"],
166
152
  },
167
153
  }),
168
- compilerOptions: {
169
- strategy: ["url", "cookie"],
170
- isServer: "false",
171
- cookieName: "PARAGLIDE_LOCALE",
172
- urlPatterns: [
173
- {
174
- pattern: "https://example.com/:path*",
175
- localized: [
176
- ["en", "https://example.com/en/:path*"],
177
- ["de", "https://example.com/de/:path*"],
178
- ["fr", "https://example.com/fr/:path*"],
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 = {
@@ -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
- project: await newProject({
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
- project: await newProject({
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");