@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,42 +1,48 @@
|
|
|
1
|
-
import { test,
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
describe.sequential("", () => {
|
|
6
|
-
beforeEach(() => {
|
|
7
|
-
vi.resetAllMocks();
|
|
8
|
-
});
|
|
9
|
-
mockRuntime({
|
|
1
|
+
import { test, expect } from "vitest";
|
|
2
|
+
import { createRuntimeForTesting } from "./create-runtime.js";
|
|
3
|
+
test("localizes the path based on the return value of getLocale()", async () => {
|
|
4
|
+
const runtime = await createRuntimeForTesting({
|
|
10
5
|
baseLocale: "en",
|
|
11
6
|
locales: ["en", "de"],
|
|
12
7
|
});
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
const l10nPath = localizePath(path, { locale: "de" });
|
|
23
|
-
expect(l10nPath).toBe("/de/about/");
|
|
8
|
+
runtime.setLocale("de");
|
|
9
|
+
const path = "/about";
|
|
10
|
+
const l10nPath = runtime.localizePath(path);
|
|
11
|
+
expect(l10nPath).toBe("/de/about");
|
|
12
|
+
});
|
|
13
|
+
test("keeps trailing slashes if provided", async () => {
|
|
14
|
+
const runtime = await createRuntimeForTesting({
|
|
15
|
+
baseLocale: "en",
|
|
16
|
+
locales: ["en", "de"],
|
|
24
17
|
});
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
18
|
+
const path = "/about/";
|
|
19
|
+
const l10nPath = runtime.localizePath(path, { locale: "de" });
|
|
20
|
+
expect(l10nPath).toBe("/de/about/");
|
|
21
|
+
});
|
|
22
|
+
test("adds no trailing slash for the root path", async () => {
|
|
23
|
+
const runtime = await createRuntimeForTesting({
|
|
24
|
+
baseLocale: "en",
|
|
25
|
+
locales: ["en", "de"],
|
|
29
26
|
});
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
27
|
+
const path = "/";
|
|
28
|
+
const l10nPath = runtime.localizePath(path, { locale: "de" });
|
|
29
|
+
expect(l10nPath).toBe("/de");
|
|
30
|
+
});
|
|
31
|
+
test("removes the base locale from the path", async () => {
|
|
32
|
+
const runtime = await createRuntimeForTesting({
|
|
33
|
+
baseLocale: "en",
|
|
34
|
+
locales: ["en", "de"],
|
|
36
35
|
});
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
36
|
+
const path = "/de/about";
|
|
37
|
+
const l10nPath = runtime.localizePath(path, { locale: "en" });
|
|
38
|
+
expect(l10nPath).toBe("/about");
|
|
39
|
+
});
|
|
40
|
+
test("does not add a slash suffix if it's the root path that is already localized", async () => {
|
|
41
|
+
const runtime = await createRuntimeForTesting({
|
|
42
|
+
baseLocale: "en",
|
|
43
|
+
locales: ["en", "de"],
|
|
41
44
|
});
|
|
45
|
+
const path = "/de";
|
|
46
|
+
const l10nPath = runtime.localizePath(path, { locale: "de" });
|
|
47
|
+
expect(l10nPath).toBe("/de");
|
|
42
48
|
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"set-locale.d.ts","sourceRoot":"","sources":["../../../src/compiler/runtime/set-locale.js"],"names":[],"mappings":"AAIA;;;;;;;GAOG;AACH,sBAFU,CAAC,SAAS,EAAE,MAAM,KAAK,IAAI,CAoDnC"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { cookieName } from "./cookie-name.js";
|
|
2
|
+
import { localizePath } from "./localize-path.js";
|
|
3
|
+
import { strategy } from "./strategy.js";
|
|
4
|
+
/**
|
|
5
|
+
* Set the locale.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* setLocale('en');
|
|
9
|
+
*
|
|
10
|
+
* @type {(newLocale: Locale) => void}
|
|
11
|
+
*/
|
|
12
|
+
export let setLocale = (newLocale) => {
|
|
13
|
+
let localeHasBeenSet = false;
|
|
14
|
+
for (const strat of strategy) {
|
|
15
|
+
if (strat === "variable") {
|
|
16
|
+
// a default for a custom strategy to get started quickly
|
|
17
|
+
// is likely overwritten by `defineSetLocale()`
|
|
18
|
+
_locale = newLocale;
|
|
19
|
+
localeHasBeenSet = true;
|
|
20
|
+
}
|
|
21
|
+
else if (strat === "cookie") {
|
|
22
|
+
if (typeof document === "undefined" || !document.cookie) {
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
// set the cookie
|
|
26
|
+
document.cookie = `${cookieName}=${newLocale}`;
|
|
27
|
+
localeHasBeenSet = true;
|
|
28
|
+
}
|
|
29
|
+
else if (strat === "pathname") {
|
|
30
|
+
if (typeof window === "undefined" || !window.location) {
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
// route to the new locale
|
|
34
|
+
//
|
|
35
|
+
// this triggers a page reload but a user rarely
|
|
36
|
+
// switches locales, so this should be fine.
|
|
37
|
+
//
|
|
38
|
+
// if the behavior is not desired, the implementation
|
|
39
|
+
// can be overwritten by `defineSetLocale()` to avoid
|
|
40
|
+
// a full page reload.
|
|
41
|
+
window.location.pathname = localizePath(window.location.pathname, {
|
|
42
|
+
locale: newLocale,
|
|
43
|
+
});
|
|
44
|
+
// not needed, as the page reloads
|
|
45
|
+
// localeHasBeenSet = true;
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
else if (strat === "baseLocale") {
|
|
49
|
+
// nothing to be set here. baseLocale is only a fallback
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
throw new Error("Unknown strategy");
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
if (localeHasBeenSet === false) {
|
|
57
|
+
throw new Error("No strategy was able to set the locale. This can happen if you use browser-based strategies like `cookie` in a server-side rendering environment. Overwrite setLocale() on the server to avoid this error.");
|
|
58
|
+
}
|
|
59
|
+
else if (typeof window !== "undefined" && window.location) {
|
|
60
|
+
// reload the page to reflect the new locale
|
|
61
|
+
window.location.reload();
|
|
62
|
+
}
|
|
63
|
+
return;
|
|
64
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"set-locale.test.d.ts","sourceRoot":"","sources":["../../../src/compiler/runtime/set-locale.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { test, expect, vi } from "vitest";
|
|
2
|
+
import { createRuntimeForTesting } from "./create-runtime.js";
|
|
3
|
+
test("sets the cookie to a different locale", async () => {
|
|
4
|
+
// @ts-expect-error - global variable definition
|
|
5
|
+
globalThis.document = {};
|
|
6
|
+
// @ts-expect-error - global variable definition
|
|
7
|
+
globalThis.window = {};
|
|
8
|
+
// @ts-expect-error - global variable definition
|
|
9
|
+
globalThis.window.location = {};
|
|
10
|
+
globalThis.window.location.reload = vi.fn();
|
|
11
|
+
const runtime = await createRuntimeForTesting({
|
|
12
|
+
baseLocale: "en",
|
|
13
|
+
locales: ["en", "de"],
|
|
14
|
+
compilerOptions: {
|
|
15
|
+
strategy: ["cookie"],
|
|
16
|
+
cookieName: "PARAGLIDE_LOCALE",
|
|
17
|
+
},
|
|
18
|
+
});
|
|
19
|
+
globalThis.document.cookie = "PARAGLIDE_LOCALE=en";
|
|
20
|
+
runtime.setLocale("de");
|
|
21
|
+
// set the locale
|
|
22
|
+
expect(globalThis.document.cookie).toBe("PARAGLIDE_LOCALE=de");
|
|
23
|
+
// reloads the site if window is available
|
|
24
|
+
expect(globalThis.window.location.reload).toBeCalled();
|
|
25
|
+
});
|
|
26
|
+
test("doesn't throw for server unavailable APIs", async () => {
|
|
27
|
+
// @ts-expect-error - reset document in case it's defined
|
|
28
|
+
globalThis.document = undefined;
|
|
29
|
+
// @ts-expect-error - reset window in case it's defined
|
|
30
|
+
globalThis.window = undefined;
|
|
31
|
+
const runtime = await createRuntimeForTesting({
|
|
32
|
+
baseLocale: "en",
|
|
33
|
+
locales: ["en", "de"],
|
|
34
|
+
compilerOptions: {
|
|
35
|
+
// using browser based strategies first, then variable which is available on the server
|
|
36
|
+
strategy: ["pathname", "cookie", "variable", "baseLocale"],
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
expect(() => runtime.setLocale("de")).not.toThrow();
|
|
40
|
+
expect(runtime.getLocale()).toBe("de");
|
|
41
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"strategy.d.ts","sourceRoot":"","sources":["../../../src/compiler/runtime/strategy.js"],"names":[],"mappings":"AAAA;;GAEG;AACH,uBAFU,KAAK,CAAC,QAAQ,GAAG,YAAY,GAAG,UAAU,GAAG,UAAU,CAAC,CAE7B"}
|
|
@@ -4,15 +4,19 @@
|
|
|
4
4
|
export type Runtime = {
|
|
5
5
|
baseLocale: Locale;
|
|
6
6
|
locales: Readonly<Locale[]>;
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
strategy: typeof import("./strategy.js").strategy;
|
|
8
|
+
cookieName: typeof import("./cookie-name.js").cookieName;
|
|
9
|
+
getLocale: typeof import("./get-locale.js").getLocale;
|
|
10
|
+
setLocale: typeof import("./set-locale.js").setLocale;
|
|
9
11
|
defineGetLocale: (fn: () => Locale) => void;
|
|
10
12
|
defineSetLocale: (fn: (newLocale: Locale) => void) => void;
|
|
11
13
|
assertIsLocale: typeof import("./assert-is-locale.js").assertIsLocale;
|
|
12
|
-
isLocale: (locale
|
|
14
|
+
isLocale: typeof import("./is-locale.js").isLocale;
|
|
13
15
|
deLocalizePath: typeof import("./de-localize-path.js").deLocalizePath;
|
|
14
16
|
localizePath: typeof import("./localize-path.js").localizePath;
|
|
15
|
-
|
|
17
|
+
extractLocaleFromPathname: typeof import("./extract-locale-from-pathname.js").extractLocaleFromPathname;
|
|
18
|
+
extractLocaleFromRequest: typeof import("./extract-locale-from-request.js").extractLocaleFromRequest;
|
|
19
|
+
extractLocaleFromCookie: typeof import("./extract-locale-from-cookie.js").extractLocaleFromCookie;
|
|
16
20
|
};
|
|
17
21
|
/**
|
|
18
22
|
* Locale is any here because the locale is unknown before compilation.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../../src/compiler/runtime/type.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,MAAM,OAAO,GAAG;IACrB,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC;IAC5B,
|
|
1
|
+
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../../src/compiler/runtime/type.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,MAAM,OAAO,GAAG;IACrB,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC;IAC5B,QAAQ,EAAE,cAAc,eAAe,EAAE,QAAQ,CAAC;IAClD,UAAU,EAAE,cAAc,kBAAkB,EAAE,UAAU,CAAC;IACzD,SAAS,EAAE,cAAc,iBAAiB,EAAE,SAAS,CAAC;IACtD,SAAS,EAAE,cAAc,iBAAiB,EAAE,SAAS,CAAC;IACtD,eAAe,EAAE,CAAC,EAAE,EAAE,MAAM,MAAM,KAAK,IAAI,CAAC;IAC5C,eAAe,EAAE,CAAC,EAAE,EAAE,CAAC,SAAS,EAAE,MAAM,KAAK,IAAI,KAAK,IAAI,CAAC;IAC3D,cAAc,EAAE,cAAc,uBAAuB,EAAE,cAAc,CAAC;IACtE,QAAQ,EAAE,cAAc,gBAAgB,EAAE,QAAQ,CAAC;IACnD,cAAc,EAAE,cAAc,uBAAuB,EAAE,cAAc,CAAC;IACtE,YAAY,EAAE,cAAc,oBAAoB,EAAE,YAAY,CAAC;IAC/D,yBAAyB,EAAE,cAAc,mCAAmC,EAAE,yBAAyB,CAAC;IACxG,wBAAwB,EAAE,cAAc,kCAAkC,EAAE,wBAAwB,CAAC;IACrG,uBAAuB,EAAE,cAAc,iCAAiC,EAAE,uBAAuB,CAAC;CAClG,CAAC;AAEF;;GAEG;AACH,KAAK,MAAM,GAAG,GAAG,CAAC"}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { expect, test } from "vitest";
|
|
2
2
|
import { createProject as typescriptProject, ts } from "@ts-morph/bootstrap";
|
|
3
|
-
import {
|
|
3
|
+
import { createRuntimeFile } from "./create-runtime.js";
|
|
4
4
|
import fs from "node:fs";
|
|
5
5
|
import { dirname, resolve } from "node:path";
|
|
6
6
|
import { fileURLToPath } from "url";
|
|
7
|
+
import { defaultCompilerOptions } from "../compile.js";
|
|
7
8
|
const __filename = fileURLToPath(import.meta.url);
|
|
8
9
|
const __dirname = dirname(__filename);
|
|
9
10
|
test("runtime type", async () => {
|
|
@@ -18,18 +19,23 @@ test("runtime type", async () => {
|
|
|
18
19
|
strict: true,
|
|
19
20
|
},
|
|
20
21
|
});
|
|
21
|
-
const jsdocRuntime =
|
|
22
|
+
const jsdocRuntime = createRuntimeFile({
|
|
23
|
+
baseLocale: "en",
|
|
24
|
+
locales: ["en"],
|
|
25
|
+
compilerOptions: defaultCompilerOptions,
|
|
26
|
+
});
|
|
22
27
|
const file = (path) => {
|
|
23
28
|
return [path, fs.readFileSync(resolve(__dirname, path), "utf-8")];
|
|
24
29
|
};
|
|
25
30
|
project.createSourceFile("./runtime.js", jsdocRuntime);
|
|
26
31
|
project.createSourceFile(...file("./type.ts"));
|
|
27
32
|
project.createSourceFile(...file("./ambient.d.ts"));
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
+
// add the imports for the types in the runtime type
|
|
34
|
+
for (const name of fs.readdirSync(__dirname)) {
|
|
35
|
+
if (name.endsWith(".js")) {
|
|
36
|
+
project.createSourceFile(...file(name));
|
|
37
|
+
}
|
|
38
|
+
}
|
|
33
39
|
project.createSourceFile("./test.ts", `
|
|
34
40
|
import * as runtime from "./runtime.js"
|
|
35
41
|
import type { Runtime } from "./type.js"
|
|
@@ -37,7 +43,10 @@ test("runtime type", async () => {
|
|
|
37
43
|
runtime satisfies Runtime
|
|
38
44
|
`);
|
|
39
45
|
const program = project.createProgram();
|
|
40
|
-
const diagnostics = ts
|
|
46
|
+
const diagnostics = ts
|
|
47
|
+
.getPreEmitDiagnostics(program)
|
|
48
|
+
// ignore 'export' modifier cannot be applied to ambient modules and module augmentations since they are always visible. /ambient.d.ts
|
|
49
|
+
.filter((d) => d.code !== 2668);
|
|
41
50
|
for (const diagnostic of diagnostics) {
|
|
42
51
|
console.error(diagnostic.messageText, diagnostic.file?.fileName);
|
|
43
52
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inlang/paraglide-js",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "2.0.0-beta.
|
|
4
|
+
"version": "2.0.0-beta.15",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"publishConfig": {
|
|
7
7
|
"access": "public",
|
|
@@ -25,8 +25,8 @@
|
|
|
25
25
|
"consola": "3.4.0",
|
|
26
26
|
"json5": "2.2.3",
|
|
27
27
|
"unplugin": "^2.1.2",
|
|
28
|
-
"@inlang/
|
|
29
|
-
"@inlang/
|
|
28
|
+
"@inlang/sdk": "2.1.2",
|
|
29
|
+
"@inlang/recommend-sherlock": "0.2.1"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"@eslint/js": "^9.18.0",
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* De-localizes the given localized path.
|
|
3
|
-
*
|
|
4
|
-
* This function removes the locale from the given path, returning the
|
|
5
|
-
* base path without the locale prefix.
|
|
6
|
-
*
|
|
7
|
-
* Useful when you need to work with non-localized paths from a localized routing system.
|
|
8
|
-
*
|
|
9
|
-
* @tip
|
|
10
|
-
* Use `localizedPath()` for the inverse operation.
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
* // Assuming baseLocale = 'en'
|
|
14
|
-
* deLocalizedPath('/fr/home');
|
|
15
|
-
* // '/home'
|
|
16
|
-
*
|
|
17
|
-
* deLocalizedPath('/en/home');
|
|
18
|
-
* // '/home'
|
|
19
|
-
*
|
|
20
|
-
* deLocalizedPath('/home');
|
|
21
|
-
* // '/home' (no change)
|
|
22
|
-
*
|
|
23
|
-
* @param {string} path - The localized path to de-localize.
|
|
24
|
-
* @returns {string} The de-localized path without the locale prefix.
|
|
25
|
-
*/
|
|
26
|
-
export function deLocalizedPath(path: string): string;
|
|
27
|
-
//# sourceMappingURL=de-localized-path.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"de-localized-path.d.ts","sourceRoot":"","sources":["../../../src/compiler/runtime/de-localized-path.js"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,sCAHW,MAAM,GACJ,MAAM,CAQlB"}
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* De-localizes the given localized path.
|
|
3
|
-
*
|
|
4
|
-
* This function removes the locale from the given path, returning the
|
|
5
|
-
* base path without the locale prefix.
|
|
6
|
-
*
|
|
7
|
-
* Useful when you need to work with non-localized paths from a localized routing system.
|
|
8
|
-
*
|
|
9
|
-
* @tip
|
|
10
|
-
* Use `localizedPath()` for the inverse operation.
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
* // Assuming baseLocale = 'en'
|
|
14
|
-
* deLocalizedPath('/fr/home');
|
|
15
|
-
* // '/home'
|
|
16
|
-
*
|
|
17
|
-
* deLocalizedPath('/en/home');
|
|
18
|
-
* // '/home'
|
|
19
|
-
*
|
|
20
|
-
* deLocalizedPath('/home');
|
|
21
|
-
* // '/home' (no change)
|
|
22
|
-
*
|
|
23
|
-
* @param {string} path - The localized path to de-localize.
|
|
24
|
-
* @returns {string} The de-localized path without the locale prefix.
|
|
25
|
-
*/
|
|
26
|
-
export function deLocalizedPath(path) {
|
|
27
|
-
const hasLocale = getLocaleFromPath(path);
|
|
28
|
-
if (!hasLocale) {
|
|
29
|
-
return path; // Path is already de-localized
|
|
30
|
-
}
|
|
31
|
-
return "/" + path.split("/").slice(2).join("/");
|
|
32
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"de-localized-path.test.d.ts","sourceRoot":"","sources":["../../../src/compiler/runtime/de-localized-path.test.ts"],"names":[],"mappings":""}
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import { test, vi, beforeEach, expect, describe } from "vitest";
|
|
2
|
-
import { deLocalizedPath } from "./de-localized-path.js";
|
|
3
|
-
import { mockRuntime } from "./mock-runtime.js";
|
|
4
|
-
// sequential to avoid global variable conflicts
|
|
5
|
-
describe.sequential("delocalizedPath", () => {
|
|
6
|
-
beforeEach(() => {
|
|
7
|
-
vi.resetAllMocks();
|
|
8
|
-
});
|
|
9
|
-
mockRuntime({
|
|
10
|
-
baseLocale: "en",
|
|
11
|
-
locales: ["en", "de", "fr"],
|
|
12
|
-
});
|
|
13
|
-
test("removes the locale from a localized path", () => {
|
|
14
|
-
// @ts-expect-error - global variable definition
|
|
15
|
-
globalThis.getLocaleFromPath = vi.fn().mockReturnValue("de");
|
|
16
|
-
const path = "/de/home";
|
|
17
|
-
const result = deLocalizedPath(path);
|
|
18
|
-
expect(result).toBe("/home");
|
|
19
|
-
});
|
|
20
|
-
test("returns the same path if there is no locale", () => {
|
|
21
|
-
// @ts-expect-error - global variable definition
|
|
22
|
-
globalThis.getLocaleFromPath = vi.fn().mockReturnValue(undefined);
|
|
23
|
-
const path = "/home";
|
|
24
|
-
const result = deLocalizedPath(path);
|
|
25
|
-
expect(result).toBe("/home");
|
|
26
|
-
});
|
|
27
|
-
test("handles paths with different locales", () => {
|
|
28
|
-
// @ts-expect-error - global variable definition
|
|
29
|
-
globalThis.getLocaleFromPath = vi.fn().mockReturnValue("fr");
|
|
30
|
-
const path = "/fr/contact";
|
|
31
|
-
const result = deLocalizedPath(path);
|
|
32
|
-
expect(result).toBe("/contact");
|
|
33
|
-
});
|
|
34
|
-
test("handles paths with no segments after locale", () => {
|
|
35
|
-
// @ts-expect-error - global variable definition
|
|
36
|
-
globalThis.getLocaleFromPath = vi.fn().mockReturnValue("en");
|
|
37
|
-
const path = "/en/";
|
|
38
|
-
const result = deLocalizedPath(path);
|
|
39
|
-
expect(result).toBe("/");
|
|
40
|
-
});
|
|
41
|
-
test("handles paths that are already the root", () => {
|
|
42
|
-
// @ts-expect-error - global variable definition
|
|
43
|
-
globalThis.getLocaleFromPath = vi.fn().mockReturnValue(undefined);
|
|
44
|
-
const path = "/";
|
|
45
|
-
const result = deLocalizedPath(path);
|
|
46
|
-
expect(result).toBe("/");
|
|
47
|
-
});
|
|
48
|
-
});
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Extracts the locale from a given path.
|
|
3
|
-
*
|
|
4
|
-
* @example
|
|
5
|
-
* const path = '/en/home';
|
|
6
|
-
* const locale = getLocaleFromPath(path);
|
|
7
|
-
* console.log(locale); // 'en'
|
|
8
|
-
*
|
|
9
|
-
* @param {string} path - The path from which to extract the locale.
|
|
10
|
-
* @returns {Locale|undefined} The extracted locale, or undefined if no locale is found.
|
|
11
|
-
*/
|
|
12
|
-
export function getLocaleFromPath(path: string): Locale | undefined;
|
|
13
|
-
//# sourceMappingURL=get-locale-from-path.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"get-locale-from-path.d.ts","sourceRoot":"","sources":["../../../src/compiler/runtime/get-locale-from-path.js"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,wCAHW,MAAM,GACJ,MAAM,GAAC,SAAS,CAO5B"}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Extracts the locale from a given path.
|
|
3
|
-
*
|
|
4
|
-
* @example
|
|
5
|
-
* const path = '/en/home';
|
|
6
|
-
* const locale = getLocaleFromPath(path);
|
|
7
|
-
* console.log(locale); // 'en'
|
|
8
|
-
*
|
|
9
|
-
* @param {string} path - The path from which to extract the locale.
|
|
10
|
-
* @returns {Locale|undefined} The extracted locale, or undefined if no locale is found.
|
|
11
|
-
*/
|
|
12
|
-
export function getLocaleFromPath(path) {
|
|
13
|
-
const [, maybeLocale] = path.split("/");
|
|
14
|
-
if (isLocale(maybeLocale)) {
|
|
15
|
-
return maybeLocale;
|
|
16
|
-
}
|
|
17
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"get-locale-from-path.test.d.ts","sourceRoot":"","sources":["../../../src/compiler/runtime/get-locale-from-path.test.ts"],"names":[],"mappings":""}
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { test, expect, vi, describe, afterEach } from "vitest";
|
|
2
|
-
import { getLocaleFromPath } from "./get-locale-from-path.js";
|
|
3
|
-
// sequential to avoid global variable conflicts
|
|
4
|
-
describe.sequential("", () => {
|
|
5
|
-
afterEach(() => {
|
|
6
|
-
vi.resetAllMocks();
|
|
7
|
-
});
|
|
8
|
-
test("returns the locale from the path", () => {
|
|
9
|
-
// @ts-expect-error - global variable definition
|
|
10
|
-
globalThis.isLocale = vi.fn().mockReturnValue(true);
|
|
11
|
-
const path = "/en-US/about";
|
|
12
|
-
const locale = getLocaleFromPath(path);
|
|
13
|
-
expect(locale).toBe("en-US");
|
|
14
|
-
});
|
|
15
|
-
test("returns undefined if isLocale is false", () => {
|
|
16
|
-
// @ts-expect-error - global variable definition
|
|
17
|
-
globalThis.isLocale = vi.fn().mockReturnValue(false);
|
|
18
|
-
const path = "/en-US/about";
|
|
19
|
-
const locale = getLocaleFromPath(path);
|
|
20
|
-
expect(locale).toBe(undefined);
|
|
21
|
-
});
|
|
22
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"jsdoc-runtime.d.ts","sourceRoot":"","sources":["../../../src/compiler/runtime/jsdoc-runtime.js"],"names":[],"mappings":"AAaA,kCAyHE"}
|