@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,131 +0,0 @@
|
|
|
1
|
-
import fs from "node:fs";
|
|
2
|
-
/**
|
|
3
|
-
* Load a file from the current directory.
|
|
4
|
-
*
|
|
5
|
-
* @param {string} path
|
|
6
|
-
* @returns {string}
|
|
7
|
-
*/
|
|
8
|
-
const injectCode = (path) => fs.readFileSync(new URL(path, import.meta.url), "utf-8");
|
|
9
|
-
// to modify the runtime, outcomment the export const jsdocRuntime line
|
|
10
|
-
export const jsdocRuntime = `
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* The project's base locale.
|
|
14
|
-
*
|
|
15
|
-
* @example
|
|
16
|
-
* if (locale === baseLocale) {
|
|
17
|
-
* // do something
|
|
18
|
-
* }
|
|
19
|
-
*/
|
|
20
|
-
export const baseLocale = "<replace>";
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* The project's locales that have been specified in the settings.
|
|
24
|
-
*
|
|
25
|
-
* @example
|
|
26
|
-
* if (locales.includes(userSelectedLocale) === false) {
|
|
27
|
-
* throw new Error('Locale is not available');
|
|
28
|
-
* }
|
|
29
|
-
*/
|
|
30
|
-
export const locales = /** @type {const} */ (["<replace>"]);
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* This is a default implementation that is almost always
|
|
34
|
-
* overwritten by \`defineGetLocale()\` and \`defineSetLocale()\`.
|
|
35
|
-
*
|
|
36
|
-
* @type {Locale}
|
|
37
|
-
*/
|
|
38
|
-
let _locale = baseLocale;
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Define the \`getLocale()\` function.
|
|
42
|
-
*
|
|
43
|
-
* Use this function to define how the locale is resolved. For example,
|
|
44
|
-
* you can resolve the locale from the browser's preferred language,
|
|
45
|
-
* a cookie, env variable, or a user's preference.
|
|
46
|
-
*
|
|
47
|
-
* @example
|
|
48
|
-
* defineGetLocale(() => {
|
|
49
|
-
* // resolve the locale from a cookie. fallback to the base locale.
|
|
50
|
-
* return Cookies.get('locale') ?? baseLocale
|
|
51
|
-
* }
|
|
52
|
-
*
|
|
53
|
-
* @param {() => Locale} fn
|
|
54
|
-
* @type {(fn: () => Locale) => void}
|
|
55
|
-
*/
|
|
56
|
-
export const defineGetLocale = (fn) => {
|
|
57
|
-
getLocale = fn;
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Define the \`setLocale()\` function.
|
|
62
|
-
*
|
|
63
|
-
* Use this function to define how the locale is set. For example,
|
|
64
|
-
* modify a cookie, env variable, or a user's preference.
|
|
65
|
-
*
|
|
66
|
-
* @example
|
|
67
|
-
* defineSetLocale((newLocale) => {
|
|
68
|
-
* // set the locale in a cookie
|
|
69
|
-
* return Cookies.set('locale', newLocale)
|
|
70
|
-
* });
|
|
71
|
-
*
|
|
72
|
-
* @param {(newLocale: Locale) => void} fn
|
|
73
|
-
*/
|
|
74
|
-
export const defineSetLocale = (fn) => {
|
|
75
|
-
setLocale = fn;
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* Get the current locale.
|
|
80
|
-
*
|
|
81
|
-
* @example
|
|
82
|
-
* if (getLocale() === 'de') {
|
|
83
|
-
* console.log('Germany 🇩🇪');
|
|
84
|
-
* } else if (getLocale() === 'nl') {
|
|
85
|
-
* console.log('Netherlands 🇳🇱');
|
|
86
|
-
* }
|
|
87
|
-
*
|
|
88
|
-
* @type {() => Locale}
|
|
89
|
-
*/
|
|
90
|
-
export let getLocale =
|
|
91
|
-
/** default implementation likely overwritten by \`defineGetLocale()\` */ () =>
|
|
92
|
-
_locale;
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Set the locale.
|
|
96
|
-
*
|
|
97
|
-
* @example
|
|
98
|
-
* setLocale('en');
|
|
99
|
-
*
|
|
100
|
-
* @param {Locale} newLocale
|
|
101
|
-
* @type {(newLocale: Locale) => void}
|
|
102
|
-
*/
|
|
103
|
-
export let setLocale =
|
|
104
|
-
/** default implementation likely overwritten by \`defineSetLocale()\` */ (
|
|
105
|
-
newLocale
|
|
106
|
-
) => {
|
|
107
|
-
_locale = newLocale;
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
${injectCode("./is-locale.js")}
|
|
111
|
-
|
|
112
|
-
${injectCode("./assert-is-locale.js")}
|
|
113
|
-
|
|
114
|
-
${injectCode("./locale-in-path.js")}
|
|
115
|
-
|
|
116
|
-
${injectCode("./localize-path.js")}
|
|
117
|
-
|
|
118
|
-
${injectCode("./de-localize-path.js")}
|
|
119
|
-
|
|
120
|
-
// ------ TYPES ------
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* A locale that is available in the project.
|
|
124
|
-
*
|
|
125
|
-
* @example
|
|
126
|
-
* setLocale(request.locale as Locale)
|
|
127
|
-
*
|
|
128
|
-
* @typedef {(typeof locales)[number]} Locale
|
|
129
|
-
*/
|
|
130
|
-
|
|
131
|
-
`;
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Extracts the locale from a given path.
|
|
3
|
-
*
|
|
4
|
-
* @example
|
|
5
|
-
* const path = '/en/home';
|
|
6
|
-
* const locale = localeInPath(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 localeInPath(path: string): Locale | undefined;
|
|
13
|
-
//# sourceMappingURL=locale-in-path.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"locale-in-path.d.ts","sourceRoot":"","sources":["../../../src/compiler/runtime/locale-in-path.js"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,mCAHW,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 = localeInPath(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 localeInPath(path) {
|
|
13
|
-
const [, maybeLocale] = path.split("/");
|
|
14
|
-
if (isLocale(maybeLocale)) {
|
|
15
|
-
return maybeLocale;
|
|
16
|
-
}
|
|
17
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"locale-in-path.test.d.ts","sourceRoot":"","sources":["../../../src/compiler/runtime/locale-in-path.test.ts"],"names":[],"mappings":""}
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { test, expect, vi, describe, afterEach } from "vitest";
|
|
2
|
-
import { localeInPath } from "./locale-in-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 = localeInPath(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 = localeInPath(path);
|
|
20
|
-
expect(locale).toBe(undefined);
|
|
21
|
-
});
|
|
22
|
-
});
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Localizes the given path.
|
|
3
|
-
*
|
|
4
|
-
* This function is useful if you use localized (i18n) routing
|
|
5
|
-
* in your application.
|
|
6
|
-
*
|
|
7
|
-
* Defaults to `getLocale()` if no locale is provided.
|
|
8
|
-
*
|
|
9
|
-
* @tip
|
|
10
|
-
* Use `deLocalizedPath()` for the inverse operation.
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
* // getLocale() = 'en'
|
|
14
|
-
* localizedPath('/home'));
|
|
15
|
-
* // '/de/home'
|
|
16
|
-
*
|
|
17
|
-
* // enforcing a specific locale
|
|
18
|
-
* localizedPath('/home', { locale: 'fr' });
|
|
19
|
-
* // '/fr/home'
|
|
20
|
-
*
|
|
21
|
-
* @example
|
|
22
|
-
* <a href={localizedPath('/home')}>Home</a>
|
|
23
|
-
*
|
|
24
|
-
* @param {string} path
|
|
25
|
-
* @param {Object} [options] - Optional parameters.
|
|
26
|
-
* @param {Locale} [options.locale] - The locale to use for the path.
|
|
27
|
-
* @returns {string}
|
|
28
|
-
*/
|
|
29
|
-
export function localizedPath(path: string, options?: {
|
|
30
|
-
locale?: Locale;
|
|
31
|
-
}): string;
|
|
32
|
-
//# sourceMappingURL=localized-path.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"localized-path.d.ts","sourceRoot":"","sources":["../../../src/compiler/runtime/localized-path.js"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,oCALW,MAAM,YAEd;IAAyB,MAAM,GAAvB,MAAM;CACd,GAAU,MAAM,CAgBlB"}
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Localizes the given path.
|
|
3
|
-
*
|
|
4
|
-
* This function is useful if you use localized (i18n) routing
|
|
5
|
-
* in your application.
|
|
6
|
-
*
|
|
7
|
-
* Defaults to `getLocale()` if no locale is provided.
|
|
8
|
-
*
|
|
9
|
-
* @tip
|
|
10
|
-
* Use `deLocalizedPath()` for the inverse operation.
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
* // getLocale() = 'en'
|
|
14
|
-
* localizedPath('/home'));
|
|
15
|
-
* // '/de/home'
|
|
16
|
-
*
|
|
17
|
-
* // enforcing a specific locale
|
|
18
|
-
* localizedPath('/home', { locale: 'fr' });
|
|
19
|
-
* // '/fr/home'
|
|
20
|
-
*
|
|
21
|
-
* @example
|
|
22
|
-
* <a href={localizedPath('/home')}>Home</a>
|
|
23
|
-
*
|
|
24
|
-
* @param {string} path
|
|
25
|
-
* @param {Object} [options] - Optional parameters.
|
|
26
|
-
* @param {Locale} [options.locale] - The locale to use for the path.
|
|
27
|
-
* @returns {string}
|
|
28
|
-
*/
|
|
29
|
-
export function localizedPath(path, options) {
|
|
30
|
-
const locale = options?.locale ?? getLocale();
|
|
31
|
-
const hasLocale = getLocaleFromPath(path);
|
|
32
|
-
const pathWithoutLocale = hasLocale
|
|
33
|
-
? "/" + path.split("/").slice(2).join("/")
|
|
34
|
-
: path;
|
|
35
|
-
if (locale === baseLocale) {
|
|
36
|
-
return pathWithoutLocale;
|
|
37
|
-
}
|
|
38
|
-
else if (path === "/" || pathWithoutLocale === "/") {
|
|
39
|
-
return `/${locale}`;
|
|
40
|
-
}
|
|
41
|
-
else {
|
|
42
|
-
return `/${locale}${pathWithoutLocale}`;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"localized-path.test.d.ts","sourceRoot":"","sources":["../../../src/compiler/runtime/localized-path.test.ts"],"names":[],"mappings":""}
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { test, vi, beforeEach, expect, describe } from "vitest";
|
|
2
|
-
import { localizedPath } from "./localized-path.js";
|
|
3
|
-
import { mockRuntime } from "./mock-runtime.js";
|
|
4
|
-
// sequential to avoid global variable conflicts
|
|
5
|
-
describe.sequential("", () => {
|
|
6
|
-
beforeEach(() => {
|
|
7
|
-
vi.resetAllMocks();
|
|
8
|
-
});
|
|
9
|
-
mockRuntime({
|
|
10
|
-
baseLocale: "en",
|
|
11
|
-
locales: ["en", "de"],
|
|
12
|
-
});
|
|
13
|
-
test("localizes the path based on the getLocale definition", () => {
|
|
14
|
-
// @ts-expect-error - global variable definition
|
|
15
|
-
globalThis.getLocale = vi.fn().mockReturnValue("de");
|
|
16
|
-
const path = "/about";
|
|
17
|
-
const l10nPath = localizedPath(path);
|
|
18
|
-
expect(l10nPath).toBe("/de/about");
|
|
19
|
-
});
|
|
20
|
-
test("keeps trailing slashes if provided", () => {
|
|
21
|
-
const path = "/about/";
|
|
22
|
-
const l10nPath = localizedPath(path, { locale: "de" });
|
|
23
|
-
expect(l10nPath).toBe("/de/about/");
|
|
24
|
-
});
|
|
25
|
-
test("adds no trailing slash for the root path", () => {
|
|
26
|
-
const path = "/";
|
|
27
|
-
const l10nPath = localizedPath(path, { locale: "de" });
|
|
28
|
-
expect(l10nPath).toBe("/de");
|
|
29
|
-
});
|
|
30
|
-
test("removes the base locale from the path", () => {
|
|
31
|
-
// @ts-expect-error - global variable definition
|
|
32
|
-
globalThis.baseLocale = "en";
|
|
33
|
-
const path = "/de/about";
|
|
34
|
-
const l10nPath = localizedPath(path, { locale: "en" });
|
|
35
|
-
expect(l10nPath).toBe("/about");
|
|
36
|
-
});
|
|
37
|
-
test("does not add a slash suffix if it's the root path that is already localized", () => {
|
|
38
|
-
const path = "/de";
|
|
39
|
-
const l10nPath = localizedPath(path, { locale: "de" });
|
|
40
|
-
expect(l10nPath).toBe("/de");
|
|
41
|
-
});
|
|
42
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"mock-runtime.d.ts","sourceRoot":"","sources":["../../../src/compiler/runtime/mock-runtime.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAOzC,wBAAgB,WAAW,CAAC,EAAE,UAAiB,EAAE,OAAsB,EAAE;;;CAAA,WAqBxE"}
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { localeInPath } from "./locale-in-path.js";
|
|
2
|
-
import { localizePath } from "./localize-path.js";
|
|
3
|
-
import { isLocale } from "./is-locale.js";
|
|
4
|
-
import { deLocalizePath } from "./de-localize-path.js";
|
|
5
|
-
import { assertIsLocale } from "./assert-is-locale.js";
|
|
6
|
-
export function mockRuntime({ baseLocale = "en", locales = ["en", "de"] }) {
|
|
7
|
-
let locale = baseLocale;
|
|
8
|
-
const runtime = {
|
|
9
|
-
baseLocale,
|
|
10
|
-
locales,
|
|
11
|
-
isLocale,
|
|
12
|
-
assertIsLocale,
|
|
13
|
-
localeInPath,
|
|
14
|
-
localizePath: localizePath,
|
|
15
|
-
deLocalizePath: deLocalizePath,
|
|
16
|
-
defineGetLocale: (fn) => (runtime.getLocale = fn),
|
|
17
|
-
defineSetLocale: (fn) => (runtime.setLocale = fn),
|
|
18
|
-
setLocale: (newLocale) => (locale = newLocale),
|
|
19
|
-
getLocale: () => locale,
|
|
20
|
-
};
|
|
21
|
-
for (const [key, value] of Object.entries(runtime)) {
|
|
22
|
-
// @ts-expect-error - global variable definition
|
|
23
|
-
globalThis[key] = value;
|
|
24
|
-
}
|
|
25
|
-
return runtime;
|
|
26
|
-
}
|
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
export declare const tsRuntime = "\n// @eslint-disable\n\n/**\n * The project's base locale.\n *\n * @example\n * if (locale === baseLocale) {\n * // do something\n * }\n */\nexport const baseLocale = \"<replace>\";\n\n/**\n * The project's locales that have been specified in the settings.\n *\n * @example\n * if (locales.includes(userSelectedLocale) === false) {\n * throw new Error('Locale is not available');\n * }\n */\nexport const locales = [\"<replace>\"] as const;\n\n/**\n * This is a default implementation that is almost always\n * overwritten by `defineGetLocale()` and `defineSetLocale()`.\n *\n */\nlet _locale: Locale = baseLocale;\n\n/**\n * Define the `getLocale()` function.\n *\n * Use this function to define how the locale is resolved. For example,\n * you can resolve the locale from the browser's preferred language,\n * a cookie, env variable, or a user's preference.\n *\n * @example\n * defineGetLocale(() => {\n * // resolve the locale from a cookie. fallback to the base locale.\n * return Cookies.get('locale') ?? baseLocale\n * }\n *\n */\nexport const defineGetLocale: (fn: () => Locale) => void = (fn) => {\n\tgetLocale = fn;\n};\n\n/**\n * Define the `setLocale()` function.\n *\n * Use this function to define how the locale is set. For example,\n * modify a cookie, env variable, or a user's preference.\n *\n * @example\n * defineSetLocale((newLocale) => {\n * // set the locale in a cookie\n * return Cookies.set('locale', newLocale)\n * });\n *\n */\nexport const defineSetLocale: (fn: (newLocale: Locale) => void) => void = (\n\tfn\n) => {\n\tsetLocale = fn;\n};\n\n/**\n * Get the current locale.\n *\n * @example\n * if (getLocale() === 'de') {\n * console.log('Germany \uD83C\uDDE9\uD83C\uDDEA');\n * } else if (getLocale() === 'nl') {\n * console.log('Netherlands \uD83C\uDDF3\uD83C\uDDF1');\n * }\n *\n */\nexport let getLocale: () => Locale =\n\t/** default implementation likely overwritten by `defineGetLocale()` */ () =>\n\t\t_locale;\n\n/**\n * Set the locale.\n *\n * @example\n * setLocale('en');\n */\nexport let setLocale: (newLocale: Locale) => void = (newLocale) => {\n\t/** default implementation likely overwritten by `defineSetLocale()` */\n\t_locale = newLocale;\n};\n\n/**\n * Check if something is an available locale.\n *\n * @example\n * if (isLocale(params.locale)) {\n * setLocale(params.locale);\n * } else {\n * setLocale('en');\n * }\n */\nexport function isLocale(locale: any): locale is Locale {\n\treturn locales.includes(locale);\n}\n\n// ------ TYPES ------\n\n/**\n * A locale that is available in the project.\n *\n * @example\n * setLocale(request.locale as Locale)\n *\n */\nexport type Locale = (typeof locales)[number];\n";
|
|
2
|
-
//# sourceMappingURL=ts-runtime.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ts-runtime.d.ts","sourceRoot":"","sources":["../../../src/compiler/runtime/ts-runtime.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,SAAS,yrFAqHrB,CAAC"}
|
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
// to modify the runtime, outcomment the export const jsdocRuntime line
|
|
2
|
-
export const tsRuntime = `
|
|
3
|
-
// @eslint-disable
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* The project's base locale.
|
|
7
|
-
*
|
|
8
|
-
* @example
|
|
9
|
-
* if (locale === baseLocale) {
|
|
10
|
-
* // do something
|
|
11
|
-
* }
|
|
12
|
-
*/
|
|
13
|
-
export const baseLocale = "<replace>";
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* The project's locales that have been specified in the settings.
|
|
17
|
-
*
|
|
18
|
-
* @example
|
|
19
|
-
* if (locales.includes(userSelectedLocale) === false) {
|
|
20
|
-
* throw new Error('Locale is not available');
|
|
21
|
-
* }
|
|
22
|
-
*/
|
|
23
|
-
export const locales = ["<replace>"] as const;
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* This is a default implementation that is almost always
|
|
27
|
-
* overwritten by \`defineGetLocale()\` and \`defineSetLocale()\`.
|
|
28
|
-
*
|
|
29
|
-
*/
|
|
30
|
-
let _locale: Locale = baseLocale;
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Define the \`getLocale()\` function.
|
|
34
|
-
*
|
|
35
|
-
* Use this function to define how the locale is resolved. For example,
|
|
36
|
-
* you can resolve the locale from the browser's preferred language,
|
|
37
|
-
* a cookie, env variable, or a user's preference.
|
|
38
|
-
*
|
|
39
|
-
* @example
|
|
40
|
-
* defineGetLocale(() => {
|
|
41
|
-
* // resolve the locale from a cookie. fallback to the base locale.
|
|
42
|
-
* return Cookies.get('locale') ?? baseLocale
|
|
43
|
-
* }
|
|
44
|
-
*
|
|
45
|
-
*/
|
|
46
|
-
export const defineGetLocale: (fn: () => Locale) => void = (fn) => {
|
|
47
|
-
getLocale = fn;
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Define the \`setLocale()\` function.
|
|
52
|
-
*
|
|
53
|
-
* Use this function to define how the locale is set. For example,
|
|
54
|
-
* modify a cookie, env variable, or a user's preference.
|
|
55
|
-
*
|
|
56
|
-
* @example
|
|
57
|
-
* defineSetLocale((newLocale) => {
|
|
58
|
-
* // set the locale in a cookie
|
|
59
|
-
* return Cookies.set('locale', newLocale)
|
|
60
|
-
* });
|
|
61
|
-
*
|
|
62
|
-
*/
|
|
63
|
-
export const defineSetLocale: (fn: (newLocale: Locale) => void) => void = (
|
|
64
|
-
fn
|
|
65
|
-
) => {
|
|
66
|
-
setLocale = fn;
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Get the current locale.
|
|
71
|
-
*
|
|
72
|
-
* @example
|
|
73
|
-
* if (getLocale() === 'de') {
|
|
74
|
-
* console.log('Germany 🇩🇪');
|
|
75
|
-
* } else if (getLocale() === 'nl') {
|
|
76
|
-
* console.log('Netherlands 🇳🇱');
|
|
77
|
-
* }
|
|
78
|
-
*
|
|
79
|
-
*/
|
|
80
|
-
export let getLocale: () => Locale =
|
|
81
|
-
/** default implementation likely overwritten by \`defineGetLocale()\` */ () =>
|
|
82
|
-
_locale;
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Set the locale.
|
|
86
|
-
*
|
|
87
|
-
* @example
|
|
88
|
-
* setLocale('en');
|
|
89
|
-
*/
|
|
90
|
-
export let setLocale: (newLocale: Locale) => void = (newLocale) => {
|
|
91
|
-
/** default implementation likely overwritten by \`defineSetLocale()\` */
|
|
92
|
-
_locale = newLocale;
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* Check if something is an available locale.
|
|
97
|
-
*
|
|
98
|
-
* @example
|
|
99
|
-
* if (isLocale(params.locale)) {
|
|
100
|
-
* setLocale(params.locale);
|
|
101
|
-
* } else {
|
|
102
|
-
* setLocale('en');
|
|
103
|
-
* }
|
|
104
|
-
*/
|
|
105
|
-
export function isLocale(locale: any): locale is Locale {
|
|
106
|
-
return locales.includes(locale);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
// ------ TYPES ------
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* A locale that is available in the project.
|
|
113
|
-
*
|
|
114
|
-
* @example
|
|
115
|
-
* setLocale(request.locale as Locale)
|
|
116
|
-
*
|
|
117
|
-
*/
|
|
118
|
-
export type Locale = (typeof locales)[number];
|
|
119
|
-
`;
|