@inlang/paraglide-js 2.0.0-beta.14 → 2.0.0-beta.15
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/compile-message.js +3 -4
- package/dist/compiler/compile-project.d.ts +1 -1
- package/dist/compiler/compile-project.d.ts.map +1 -1
- package/dist/compiler/compile-project.js +13 -5
- package/dist/compiler/compile-project.test.js +9 -5
- package/dist/compiler/compile.d.ts +25 -0
- package/dist/compiler/compile.d.ts.map +1 -1
- package/dist/compiler/compile.js +3 -11
- package/dist/compiler/compile.test.js +11 -3
- package/dist/compiler/output-structure/locale-modules.d.ts +5 -1
- package/dist/compiler/output-structure/locale-modules.d.ts.map +1 -1
- package/dist/compiler/output-structure/locale-modules.js +8 -3
- package/dist/compiler/output-structure/locale-modules.test.js +3 -2
- package/dist/compiler/output-structure/message-modules.d.ts +5 -1
- package/dist/compiler/output-structure/message-modules.d.ts.map +1 -1
- package/dist/compiler/output-structure/message-modules.js +13 -5
- package/dist/compiler/output-structure/message-modules.test.js +2 -1
- package/dist/compiler/runtime/assert-is-locale.d.ts.map +1 -1
- package/dist/compiler/runtime/assert-is-locale.js +2 -0
- package/dist/compiler/runtime/assert-is-locale.test.js +19 -12
- package/dist/compiler/runtime/base-locale.d.ts +10 -0
- package/dist/compiler/runtime/base-locale.d.ts.map +1 -0
- package/dist/compiler/runtime/base-locale.js +9 -0
- package/dist/compiler/runtime/cookie-name.d.ts +3 -0
- package/dist/compiler/runtime/cookie-name.d.ts.map +1 -0
- package/dist/compiler/runtime/cookie-name.js +2 -0
- package/dist/compiler/runtime/create-runtime.d.ts +27 -2
- package/dist/compiler/runtime/create-runtime.d.ts.map +1 -1
- package/dist/compiler/runtime/create-runtime.js +116 -7
- package/dist/compiler/runtime/de-localize-path.d.ts.map +1 -1
- package/dist/compiler/runtime/de-localize-path.js +2 -1
- package/dist/compiler/runtime/de-localize-path.test.js +40 -41
- package/dist/compiler/runtime/extract-locale-from-cookie.d.ts +10 -0
- package/dist/compiler/runtime/extract-locale-from-cookie.d.ts.map +1 -0
- package/dist/compiler/runtime/extract-locale-from-cookie.js +16 -0
- package/dist/compiler/runtime/extract-locale-from-cookie.test.d.ts +2 -0
- package/dist/compiler/runtime/extract-locale-from-cookie.test.d.ts.map +1 -0
- package/dist/compiler/runtime/extract-locale-from-cookie.test.js +27 -0
- package/dist/compiler/runtime/extract-locale-from-pathname.d.ts +13 -0
- package/dist/compiler/runtime/extract-locale-from-pathname.d.ts.map +1 -0
- package/dist/compiler/runtime/extract-locale-from-pathname.js +19 -0
- package/dist/compiler/runtime/extract-locale-from-pathname.test.d.ts +2 -0
- package/dist/compiler/runtime/extract-locale-from-pathname.test.d.ts.map +1 -0
- package/dist/compiler/runtime/extract-locale-from-pathname.test.js +20 -0
- package/dist/compiler/runtime/extract-locale-from-request.d.ts +10 -0
- package/dist/compiler/runtime/extract-locale-from-request.d.ts.map +1 -0
- package/dist/compiler/runtime/extract-locale-from-request.js +42 -0
- package/dist/compiler/runtime/extract-locale-from-request.test.d.ts +2 -0
- package/dist/compiler/runtime/extract-locale-from-request.test.d.ts.map +1 -0
- package/dist/compiler/runtime/extract-locale-from-request.test.js +66 -0
- package/dist/compiler/runtime/get-locale.d.ts +14 -0
- package/dist/compiler/runtime/get-locale.d.ts.map +1 -0
- package/dist/compiler/runtime/get-locale.js +52 -0
- package/dist/compiler/runtime/get-locale.test.d.ts +2 -0
- package/dist/compiler/runtime/get-locale.test.d.ts.map +1 -0
- package/dist/compiler/runtime/get-locale.test.js +42 -0
- package/dist/compiler/runtime/is-locale.d.ts.map +1 -1
- package/dist/compiler/runtime/is-locale.js +2 -1
- package/dist/compiler/runtime/locales.d.ts +10 -0
- package/dist/compiler/runtime/locales.d.ts.map +1 -0
- package/dist/compiler/runtime/locales.js +9 -0
- package/dist/compiler/runtime/localize-path.d.ts.map +1 -1
- package/dist/compiler/runtime/localize-path.js +4 -1
- package/dist/compiler/runtime/localize-path.test.js +40 -34
- package/dist/compiler/runtime/set-locale.d.ts +10 -0
- package/dist/compiler/runtime/set-locale.d.ts.map +1 -0
- package/dist/compiler/runtime/set-locale.js +64 -0
- package/dist/compiler/runtime/set-locale.test.d.ts +2 -0
- package/dist/compiler/runtime/set-locale.test.d.ts.map +1 -0
- package/dist/compiler/runtime/set-locale.test.js +41 -0
- package/dist/compiler/runtime/strategy.d.ts +5 -0
- package/dist/compiler/runtime/strategy.d.ts.map +1 -0
- package/dist/compiler/runtime/strategy.js +4 -0
- package/dist/compiler/runtime/type.d.ts +8 -4
- package/dist/compiler/runtime/type.d.ts.map +1 -1
- package/dist/compiler/runtime/type.test.js +17 -8
- package/dist/services/env-variables/index.js +1 -1
- package/package.json +3 -3
- package/dist/compiler/runtime/de-localized-path.d.ts +0 -27
- package/dist/compiler/runtime/de-localized-path.d.ts.map +0 -1
- package/dist/compiler/runtime/de-localized-path.js +0 -32
- package/dist/compiler/runtime/de-localized-path.test.d.ts +0 -2
- package/dist/compiler/runtime/de-localized-path.test.d.ts.map +0 -1
- package/dist/compiler/runtime/de-localized-path.test.js +0 -48
- package/dist/compiler/runtime/get-locale-from-path.d.ts +0 -13
- package/dist/compiler/runtime/get-locale-from-path.d.ts.map +0 -1
- package/dist/compiler/runtime/get-locale-from-path.js +0 -17
- package/dist/compiler/runtime/get-locale-from-path.test.d.ts +0 -2
- package/dist/compiler/runtime/get-locale-from-path.test.d.ts.map +0 -1
- package/dist/compiler/runtime/get-locale-from-path.test.js +0 -22
- package/dist/compiler/runtime/jsdoc-runtime.d.ts +0 -2
- package/dist/compiler/runtime/jsdoc-runtime.d.ts.map +0 -1
- package/dist/compiler/runtime/jsdoc-runtime.js +0 -131
- package/dist/compiler/runtime/locale-in-path.d.ts +0 -13
- package/dist/compiler/runtime/locale-in-path.d.ts.map +0 -1
- package/dist/compiler/runtime/locale-in-path.js +0 -17
- package/dist/compiler/runtime/locale-in-path.test.d.ts +0 -2
- package/dist/compiler/runtime/locale-in-path.test.d.ts.map +0 -1
- package/dist/compiler/runtime/locale-in-path.test.js +0 -22
- package/dist/compiler/runtime/localized-path.d.ts +0 -32
- package/dist/compiler/runtime/localized-path.d.ts.map +0 -1
- package/dist/compiler/runtime/localized-path.js +0 -44
- package/dist/compiler/runtime/localized-path.test.d.ts +0 -2
- package/dist/compiler/runtime/localized-path.test.d.ts.map +0 -1
- package/dist/compiler/runtime/localized-path.test.js +0 -42
- package/dist/compiler/runtime/mock-runtime.d.ts +0 -6
- package/dist/compiler/runtime/mock-runtime.d.ts.map +0 -1
- package/dist/compiler/runtime/mock-runtime.js +0 -26
- package/dist/compiler/runtime/ts-runtime.d.ts +0 -2
- package/dist/compiler/runtime/ts-runtime.d.ts.map +0 -1
- package/dist/compiler/runtime/ts-runtime.js +0 -119
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { extractLocaleFromPathname } from "./extract-locale-from-pathname.js";
|
|
1
2
|
/**
|
|
2
3
|
* De-localizes the given localized path.
|
|
3
4
|
*
|
|
@@ -24,7 +25,7 @@
|
|
|
24
25
|
* @returns {string} The de-localized path without the locale prefix.
|
|
25
26
|
*/
|
|
26
27
|
export function deLocalizePath(path) {
|
|
27
|
-
const hasLocale =
|
|
28
|
+
const hasLocale = extractLocaleFromPathname(path);
|
|
28
29
|
if (!hasLocale) {
|
|
29
30
|
return path; // Path is already de-localized
|
|
30
31
|
}
|
|
@@ -1,48 +1,47 @@
|
|
|
1
|
-
import { test,
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
describe.sequential("delocalizedPath", () => {
|
|
6
|
-
beforeEach(() => {
|
|
7
|
-
vi.resetAllMocks();
|
|
8
|
-
});
|
|
9
|
-
mockRuntime({
|
|
1
|
+
import { test, expect } from "vitest";
|
|
2
|
+
import { createRuntimeForTesting } from "./create-runtime.js";
|
|
3
|
+
test("removes the locale from a localized path", async () => {
|
|
4
|
+
const runtime = await createRuntimeForTesting({
|
|
10
5
|
baseLocale: "en",
|
|
11
|
-
locales: ["en", "de"
|
|
12
|
-
});
|
|
13
|
-
test("removes the locale from a localized path", () => {
|
|
14
|
-
// @ts-expect-error - global variable definition
|
|
15
|
-
globalThis.localeInPath = vi.fn().mockReturnValue("de");
|
|
16
|
-
const path = "/de/home";
|
|
17
|
-
const result = deLocalizePath(path);
|
|
18
|
-
expect(result).toBe("/home");
|
|
6
|
+
locales: ["en", "de"],
|
|
19
7
|
});
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
8
|
+
const path = "/de/home";
|
|
9
|
+
const result = runtime.deLocalizePath(path);
|
|
10
|
+
expect(result).toBe("/home");
|
|
11
|
+
});
|
|
12
|
+
test("returns the same path if there is no locale", async () => {
|
|
13
|
+
const runtime = await createRuntimeForTesting({
|
|
14
|
+
baseLocale: "en",
|
|
15
|
+
locales: ["en", "de"],
|
|
26
16
|
});
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
17
|
+
const path = "/home";
|
|
18
|
+
const result = runtime.deLocalizePath(path);
|
|
19
|
+
expect(result).toBe("/home");
|
|
20
|
+
});
|
|
21
|
+
test("handles paths with different locales", async () => {
|
|
22
|
+
const runtime = await createRuntimeForTesting({
|
|
23
|
+
baseLocale: "en",
|
|
24
|
+
locales: ["en", "de", "fr"],
|
|
33
25
|
});
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
26
|
+
const path = "/fr/contact";
|
|
27
|
+
const result = runtime.deLocalizePath(path);
|
|
28
|
+
expect(result).toBe("/contact");
|
|
29
|
+
});
|
|
30
|
+
test("handles paths with no segments after locale", async () => {
|
|
31
|
+
const runtime = await createRuntimeForTesting({
|
|
32
|
+
baseLocale: "en",
|
|
33
|
+
locales: ["en", "de"],
|
|
40
34
|
});
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
35
|
+
const path = "/en/";
|
|
36
|
+
const result = runtime.deLocalizePath(path);
|
|
37
|
+
expect(result).toBe("/");
|
|
38
|
+
});
|
|
39
|
+
test("handles paths that are already the root", async () => {
|
|
40
|
+
const runtime = await createRuntimeForTesting({
|
|
41
|
+
baseLocale: "en",
|
|
42
|
+
locales: ["en", "de"],
|
|
47
43
|
});
|
|
44
|
+
const path = "/";
|
|
45
|
+
const result = runtime.deLocalizePath(path);
|
|
46
|
+
expect(result).toBe("/");
|
|
48
47
|
});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Extracts a cookie from the document.
|
|
3
|
+
*
|
|
4
|
+
* Will return undefined if the docuement is not available or if the cookie is not set.
|
|
5
|
+
* The `document` object is not available in server-side rendering, so this function should not be called in that context.
|
|
6
|
+
*
|
|
7
|
+
* @returns {string | undefined}
|
|
8
|
+
*/
|
|
9
|
+
export function extractLocaleFromCookie(): string | undefined;
|
|
10
|
+
//# sourceMappingURL=extract-locale-from-cookie.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extract-locale-from-cookie.d.ts","sourceRoot":"","sources":["../../../src/compiler/runtime/extract-locale-from-cookie.js"],"names":[],"mappings":"AAEA;;;;;;;GAOG;AACH,2CAFa,MAAM,GAAG,SAAS,CAQ9B"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { cookieName } from "./cookie-name.js";
|
|
2
|
+
/**
|
|
3
|
+
* Extracts a cookie from the document.
|
|
4
|
+
*
|
|
5
|
+
* Will return undefined if the docuement is not available or if the cookie is not set.
|
|
6
|
+
* The `document` object is not available in server-side rendering, so this function should not be called in that context.
|
|
7
|
+
*
|
|
8
|
+
* @returns {string | undefined}
|
|
9
|
+
*/
|
|
10
|
+
export function extractLocaleFromCookie() {
|
|
11
|
+
if (typeof document === "undefined" || !document.cookie) {
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
const match = document.cookie.match(new RegExp(`(^| )${cookieName}=([^;]+)`));
|
|
15
|
+
return match?.[2];
|
|
16
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extract-locale-from-cookie.test.d.ts","sourceRoot":"","sources":["../../../src/compiler/runtime/extract-locale-from-cookie.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { test, expect } from "vitest";
|
|
2
|
+
import { createRuntimeForTesting } from "./create-runtime.js";
|
|
3
|
+
test("returns undefined if document is not available", async () => {
|
|
4
|
+
// @ts-expect-error - global variable definition
|
|
5
|
+
globalThis.document = undefined;
|
|
6
|
+
const runtime = await createRuntimeForTesting({
|
|
7
|
+
baseLocale: "en",
|
|
8
|
+
locales: ["en", "de"],
|
|
9
|
+
});
|
|
10
|
+
expect(runtime.extractLocaleFromCookie()).toBeUndefined();
|
|
11
|
+
});
|
|
12
|
+
test("matches the locale of a cookie", async () => {
|
|
13
|
+
const runtime = await createRuntimeForTesting({
|
|
14
|
+
baseLocale: "en",
|
|
15
|
+
locales: ["en", "de"],
|
|
16
|
+
compilerOptions: {
|
|
17
|
+
strategy: ["cookie"],
|
|
18
|
+
cookieName: "PARAGLIDE_LOCALE",
|
|
19
|
+
},
|
|
20
|
+
});
|
|
21
|
+
// @ts-expect-error - global variable definition
|
|
22
|
+
globalThis.document = {};
|
|
23
|
+
globalThis.document.cookie =
|
|
24
|
+
"OTHER_COOKIE=fr; PARAGLIDE_LOCALE=de; ANOTHER_COOKIE=en; EXPIRES_COOKIE=es; Max-Age=3600";
|
|
25
|
+
const locale = runtime.extractLocaleFromCookie();
|
|
26
|
+
expect(locale).toBe("de");
|
|
27
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Extracts the locale from a given pathname.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* const pathname = '/en/home';
|
|
6
|
+
* const locale = extractLocaleFromPathname(pathname);
|
|
7
|
+
* console.log(locale); // 'en'
|
|
8
|
+
*
|
|
9
|
+
* @param {string} pathname - The pathname from which to extract the locale.
|
|
10
|
+
* @returns {Locale|undefined} The extracted locale, or undefined if no locale is found.
|
|
11
|
+
*/
|
|
12
|
+
export function extractLocaleFromPathname(pathname: string): Locale | undefined;
|
|
13
|
+
//# sourceMappingURL=extract-locale-from-pathname.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extract-locale-from-pathname.d.ts","sourceRoot":"","sources":["../../../src/compiler/runtime/extract-locale-from-pathname.js"],"names":[],"mappings":"AAEA;;;;;;;;;;GAUG;AACH,oDAHW,MAAM,GACJ,MAAM,GAAC,SAAS,CAS5B"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { isLocale } from "./is-locale.js";
|
|
2
|
+
/**
|
|
3
|
+
* Extracts the locale from a given pathname.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* const pathname = '/en/home';
|
|
7
|
+
* const locale = extractLocaleFromPathname(pathname);
|
|
8
|
+
* console.log(locale); // 'en'
|
|
9
|
+
*
|
|
10
|
+
* @param {string} pathname - The pathname from which to extract the locale.
|
|
11
|
+
* @returns {Locale|undefined} The extracted locale, or undefined if no locale is found.
|
|
12
|
+
*/
|
|
13
|
+
export function extractLocaleFromPathname(pathname) {
|
|
14
|
+
const [, maybeLocale] = pathname.split("/");
|
|
15
|
+
if (isLocale(maybeLocale)) {
|
|
16
|
+
return maybeLocale;
|
|
17
|
+
}
|
|
18
|
+
return undefined;
|
|
19
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extract-locale-from-pathname.test.d.ts","sourceRoot":"","sources":["../../../src/compiler/runtime/extract-locale-from-pathname.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { test, expect } from "vitest";
|
|
2
|
+
import { createRuntimeForTesting } from "./create-runtime.js";
|
|
3
|
+
test("returns the locale from the path", async () => {
|
|
4
|
+
const runtime = await createRuntimeForTesting({
|
|
5
|
+
baseLocale: "en",
|
|
6
|
+
locales: ["en", "en-US"],
|
|
7
|
+
});
|
|
8
|
+
const path = "/en-US/about";
|
|
9
|
+
const locale = runtime.extractLocaleFromPathname(path);
|
|
10
|
+
expect(locale).toBe("en-US");
|
|
11
|
+
});
|
|
12
|
+
test("returns undefined if isLocale is false", async () => {
|
|
13
|
+
const runtime = await createRuntimeForTesting({
|
|
14
|
+
baseLocale: "en",
|
|
15
|
+
locales: ["en"],
|
|
16
|
+
});
|
|
17
|
+
const path = "/en-US/about";
|
|
18
|
+
const locale = runtime.extractLocaleFromPathname(path);
|
|
19
|
+
expect(locale).toBe(undefined);
|
|
20
|
+
});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Detect a locale from a request.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* const locale = extractLocaleFromRequest(request);
|
|
6
|
+
*
|
|
7
|
+
* @type {(request: Request) => Locale}
|
|
8
|
+
*/
|
|
9
|
+
export const extractLocaleFromRequest: (request: Request) => Locale;
|
|
10
|
+
//# sourceMappingURL=extract-locale-from-request.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extract-locale-from-request.d.ts","sourceRoot":"","sources":["../../../src/compiler/runtime/extract-locale-from-request.js"],"names":[],"mappings":"AAMA;;;;;;;GAOG;AACH,uCAFU,CAAC,OAAO,EAAE,OAAO,KAAK,MAAM,CA6BpC"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { assertIsLocale } from "./assert-is-locale.js";
|
|
2
|
+
import { baseLocale } from "./base-locale.js";
|
|
3
|
+
import { cookieName } from "./cookie-name.js";
|
|
4
|
+
import { extractLocaleFromPathname } from "./extract-locale-from-pathname.js";
|
|
5
|
+
import { strategy } from "./strategy.js";
|
|
6
|
+
/**
|
|
7
|
+
* Detect a locale from a request.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* const locale = extractLocaleFromRequest(request);
|
|
11
|
+
*
|
|
12
|
+
* @type {(request: Request) => Locale}
|
|
13
|
+
*/
|
|
14
|
+
export const extractLocaleFromRequest = (request) => {
|
|
15
|
+
/** @type {string|undefined} */
|
|
16
|
+
let locale;
|
|
17
|
+
for (const strat of strategy) {
|
|
18
|
+
if (strat === "cookie") {
|
|
19
|
+
locale = request.headers
|
|
20
|
+
.get("cookie")
|
|
21
|
+
?.split("; ")
|
|
22
|
+
.find((c) => c.startsWith(cookieName + "="))
|
|
23
|
+
?.split("=")[1];
|
|
24
|
+
}
|
|
25
|
+
else if (strat === "pathname") {
|
|
26
|
+
locale = extractLocaleFromPathname(new URL(request.url).pathname);
|
|
27
|
+
}
|
|
28
|
+
else if (strat === "variable") {
|
|
29
|
+
locale = _locale;
|
|
30
|
+
}
|
|
31
|
+
else if (strat === "baseLocale") {
|
|
32
|
+
return baseLocale;
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
throw new Error(`Unsupported strategy: ${strat}`);
|
|
36
|
+
}
|
|
37
|
+
if (locale !== undefined) {
|
|
38
|
+
return assertIsLocale(locale);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
throw new Error("No locale found. There is an error in your strategy. Try adding 'baseLocale' as the very last strategy.");
|
|
42
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extract-locale-from-request.test.d.ts","sourceRoot":"","sources":["../../../src/compiler/runtime/extract-locale-from-request.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { test, expect } from "vitest";
|
|
2
|
+
import { createRuntimeForTesting } from "./create-runtime.js";
|
|
3
|
+
test("returns the locale from the cookie", async () => {
|
|
4
|
+
const runtime = await createRuntimeForTesting({
|
|
5
|
+
baseLocale: "en",
|
|
6
|
+
locales: ["en", "fr"],
|
|
7
|
+
compilerOptions: {
|
|
8
|
+
strategy: ["cookie"],
|
|
9
|
+
cookieName: "PARAGLIDE_LOCALE",
|
|
10
|
+
},
|
|
11
|
+
});
|
|
12
|
+
const request = new Request("http://example.com", {
|
|
13
|
+
headers: {
|
|
14
|
+
cookie: `PARAGLIDE_LOCALE=fr`,
|
|
15
|
+
},
|
|
16
|
+
});
|
|
17
|
+
const locale = runtime.extractLocaleFromRequest(request);
|
|
18
|
+
expect(locale).toBe("fr");
|
|
19
|
+
});
|
|
20
|
+
test("returns the locale from the pathname", async () => {
|
|
21
|
+
const runtime = await createRuntimeForTesting({
|
|
22
|
+
baseLocale: "en",
|
|
23
|
+
locales: ["en"],
|
|
24
|
+
compilerOptions: {
|
|
25
|
+
strategy: ["pathname"],
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
const request = new Request("http://example.com/en/home");
|
|
29
|
+
const locale = runtime.extractLocaleFromRequest(request);
|
|
30
|
+
expect(locale).toBe("en");
|
|
31
|
+
});
|
|
32
|
+
test("returns the baseLocale if no other strategy matches", async () => {
|
|
33
|
+
const runtime = await createRuntimeForTesting({
|
|
34
|
+
baseLocale: "en",
|
|
35
|
+
locales: ["en"],
|
|
36
|
+
compilerOptions: {
|
|
37
|
+
strategy: ["baseLocale"],
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
const request = new Request("http://example.com");
|
|
41
|
+
const locale = runtime.extractLocaleFromRequest(request);
|
|
42
|
+
expect(locale).toBe("en");
|
|
43
|
+
});
|
|
44
|
+
test("throws an error for unsupported strategy", async () => {
|
|
45
|
+
const runtime = await createRuntimeForTesting({
|
|
46
|
+
baseLocale: "en",
|
|
47
|
+
locales: ["en"],
|
|
48
|
+
compilerOptions: {
|
|
49
|
+
// @ts-expect-error - unsupported strategy
|
|
50
|
+
strategy: ["unsupported"],
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
const request = new Request("http://example.com");
|
|
54
|
+
expect(() => runtime.extractLocaleFromRequest(request)).toThrow("Unsupported strategy: unsupported");
|
|
55
|
+
});
|
|
56
|
+
test("throws an error if no locale is found", async () => {
|
|
57
|
+
const runtime = await createRuntimeForTesting({
|
|
58
|
+
baseLocale: "en",
|
|
59
|
+
locales: ["en"],
|
|
60
|
+
compilerOptions: {
|
|
61
|
+
strategy: ["cookie"],
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
const request = new Request("http://example.com");
|
|
65
|
+
expect(() => runtime.extractLocaleFromRequest(request)).toThrow("No locale found. There is an error in your strategy. Try adding 'baseLocale' as the very last strategy.");
|
|
66
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Get the current locale.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* if (getLocale() === 'de') {
|
|
6
|
+
* console.log('Germany 🇩🇪');
|
|
7
|
+
* } else if (getLocale() === 'nl') {
|
|
8
|
+
* console.log('Netherlands 🇳🇱');
|
|
9
|
+
* }
|
|
10
|
+
*
|
|
11
|
+
* @type {() => Locale}
|
|
12
|
+
*/
|
|
13
|
+
export let getLocale: () => Locale;
|
|
14
|
+
//# sourceMappingURL=get-locale.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"get-locale.d.ts","sourceRoot":"","sources":["../../../src/compiler/runtime/get-locale.js"],"names":[],"mappings":"AAiBA;;;;;;;;;;;GAWG;AACH,sBAFU,MAAM,MAAM,CA8BpB"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { assertIsLocale } from "./assert-is-locale.js";
|
|
2
|
+
import { baseLocale } from "./base-locale.js";
|
|
3
|
+
import { extractLocaleFromCookie } from "./extract-locale-from-cookie.js";
|
|
4
|
+
import { extractLocaleFromPathname } from "./extract-locale-from-pathname.js";
|
|
5
|
+
import { strategy } from "./strategy.js";
|
|
6
|
+
/**
|
|
7
|
+
* This is a fallback to get started with a custom
|
|
8
|
+
* strategy and avoid type errors.
|
|
9
|
+
*
|
|
10
|
+
* The implementation is overwritten
|
|
11
|
+
* by \`defineGetLocale()\` and \`defineSetLocale()\`.
|
|
12
|
+
*
|
|
13
|
+
* @type {Locale|undefined}
|
|
14
|
+
*/
|
|
15
|
+
let _locale;
|
|
16
|
+
/**
|
|
17
|
+
* Get the current locale.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* if (getLocale() === 'de') {
|
|
21
|
+
* console.log('Germany 🇩🇪');
|
|
22
|
+
* } else if (getLocale() === 'nl') {
|
|
23
|
+
* console.log('Netherlands 🇳🇱');
|
|
24
|
+
* }
|
|
25
|
+
*
|
|
26
|
+
* @type {() => Locale}
|
|
27
|
+
*/
|
|
28
|
+
export let getLocale = () => {
|
|
29
|
+
/** @type {string | undefined} */
|
|
30
|
+
let locale;
|
|
31
|
+
for (const strat of strategy) {
|
|
32
|
+
if (strat === "cookie") {
|
|
33
|
+
locale = extractLocaleFromCookie();
|
|
34
|
+
}
|
|
35
|
+
if (strat === "baseLocale") {
|
|
36
|
+
locale = baseLocale;
|
|
37
|
+
}
|
|
38
|
+
if (strat === "pathname" &&
|
|
39
|
+
typeof window !== "undefined" &&
|
|
40
|
+
window.location?.pathname) {
|
|
41
|
+
locale = extractLocaleFromPathname(window.location.pathname);
|
|
42
|
+
}
|
|
43
|
+
if (strat === "variable" && _locale !== undefined) {
|
|
44
|
+
locale = _locale;
|
|
45
|
+
}
|
|
46
|
+
// check if match, else continue loop
|
|
47
|
+
if (locale !== undefined) {
|
|
48
|
+
return assertIsLocale(locale);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
throw new Error("No locale found. There is an error in your strategy.");
|
|
52
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"get-locale.test.d.ts","sourceRoot":"","sources":["../../../src/compiler/runtime/get-locale.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { test, expect } from "vitest";
|
|
2
|
+
import { createRuntimeForTesting } from "./create-runtime.js";
|
|
3
|
+
test("matching by strategy works", async () => {
|
|
4
|
+
const baseLocale = "en";
|
|
5
|
+
const runtime = await createRuntimeForTesting({
|
|
6
|
+
baseLocale,
|
|
7
|
+
locales: ["en", "de"],
|
|
8
|
+
compilerOptions: {
|
|
9
|
+
strategy: ["cookie", "baseLocale"],
|
|
10
|
+
cookieName: "PARAGLIDE_LOCALE",
|
|
11
|
+
},
|
|
12
|
+
});
|
|
13
|
+
// @ts-expect-error - global variable definition
|
|
14
|
+
globalThis.document = {};
|
|
15
|
+
globalThis.document.cookie = "OTHER_COOKIE=blaba;";
|
|
16
|
+
const locale = runtime.getLocale();
|
|
17
|
+
expect(locale).toBe(baseLocale);
|
|
18
|
+
});
|
|
19
|
+
test("doesn't throw when using pathname strategy on the server", async () => {
|
|
20
|
+
const baseLocale = "en";
|
|
21
|
+
const runtime = await createRuntimeForTesting({
|
|
22
|
+
baseLocale,
|
|
23
|
+
locales: ["en", "de"],
|
|
24
|
+
compilerOptions: {
|
|
25
|
+
strategy: ["pathname", "variable", "baseLocale"],
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
expect(() => runtime.getLocale()).not.toThrow();
|
|
29
|
+
expect(runtime.getLocale()).toBe(baseLocale);
|
|
30
|
+
});
|
|
31
|
+
test("throws if variable is used without baseLocale as fallback strategy", async () => {
|
|
32
|
+
const runtime = await createRuntimeForTesting({
|
|
33
|
+
baseLocale: "en",
|
|
34
|
+
locales: ["en", "de"],
|
|
35
|
+
compilerOptions: {
|
|
36
|
+
strategy: ["variable"],
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
expect(() => runtime.getLocale()).toThrow();
|
|
40
|
+
runtime.setLocale("de");
|
|
41
|
+
expect(runtime.getLocale()).toBe("de");
|
|
42
|
+
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"is-locale.d.ts","sourceRoot":"","sources":["../../../src/compiler/runtime/is-locale.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"is-locale.d.ts","sourceRoot":"","sources":["../../../src/compiler/runtime/is-locale.js"],"names":[],"mappings":"AAEA;;;;;;;;;;;;GAYG;AACH,iCAHW,GAAG,GACD,MAAM,IAAI,MAAM,CAI5B"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { locales } from "./locales.js";
|
|
1
2
|
/**
|
|
2
3
|
* Check if something is an available locale.
|
|
3
4
|
*
|
|
@@ -12,5 +13,5 @@
|
|
|
12
13
|
* @returns {locale is Locale}
|
|
13
14
|
*/
|
|
14
15
|
export function isLocale(locale) {
|
|
15
|
-
return locales.includes(locale);
|
|
16
|
+
return !locale ? false : locales.includes(locale);
|
|
16
17
|
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The project's locales that have been specified in the settings.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* if (locales.includes(userSelectedLocale) === false) {
|
|
6
|
+
* throw new Error('Locale is not available');
|
|
7
|
+
* }
|
|
8
|
+
*/
|
|
9
|
+
export const locales: readonly ["<base-locale>"];
|
|
10
|
+
//# sourceMappingURL=locales.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"locales.d.ts","sourceRoot":"","sources":["../../../src/compiler/runtime/locales.js"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,iDAAgE"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The project's locales that have been specified in the settings.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* if (locales.includes(userSelectedLocale) === false) {
|
|
6
|
+
* throw new Error('Locale is not available');
|
|
7
|
+
* }
|
|
8
|
+
*/
|
|
9
|
+
export const locales = /** @type {const} */ (["<base-locale>"]);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"localize-path.d.ts","sourceRoot":"","sources":["../../../src/compiler/runtime/localize-path.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"localize-path.d.ts","sourceRoot":"","sources":["../../../src/compiler/runtime/localize-path.js"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,mCALW,MAAM,YAEd;IAAyB,MAAM,GAAvB,MAAM;CACd,GAAU,MAAM,CAgBlB"}
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { baseLocale } from "./base-locale.js";
|
|
2
|
+
import { getLocale } from "./get-locale.js";
|
|
3
|
+
import { extractLocaleFromPathname } from "./extract-locale-from-pathname.js";
|
|
1
4
|
/**
|
|
2
5
|
* Localizes the given path.
|
|
3
6
|
*
|
|
@@ -28,7 +31,7 @@
|
|
|
28
31
|
*/
|
|
29
32
|
export function localizePath(path, options) {
|
|
30
33
|
const locale = options?.locale ?? getLocale();
|
|
31
|
-
const hasLocale =
|
|
34
|
+
const hasLocale = extractLocaleFromPathname(path);
|
|
32
35
|
const pathWithoutLocale = hasLocale
|
|
33
36
|
? "/" + path.split("/").slice(2).join("/")
|
|
34
37
|
: path;
|