@inlang/paraglide-js 1.0.0-prerelease.2 → 1.0.0-prerelease.3

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.
Files changed (42) hide show
  1. package/dist/cli/commands/compile.d.ts.map +1 -1
  2. package/dist/cli/commands/compile.js +1 -1
  3. package/dist/cli/commands/init.d.ts +3 -0
  4. package/dist/cli/commands/init.d.ts.map +1 -0
  5. package/dist/cli/commands/init.js +10 -0
  6. package/dist/cli/commands/inittest.d.ts +2 -0
  7. package/dist/cli/commands/inittest.d.ts.map +1 -0
  8. package/dist/cli/commands/inittest.js +29 -0
  9. package/dist/cli/main.d.ts +0 -8
  10. package/dist/cli/main.d.ts.map +1 -1
  11. package/dist/cli/main.js +8 -20
  12. package/dist/cli/main.test.d.ts +2 -0
  13. package/dist/cli/main.test.d.ts.map +1 -0
  14. package/dist/cli/main.test.js +43 -0
  15. package/dist/cli/state.d.ts +10 -0
  16. package/dist/cli/state.d.ts.map +1 -0
  17. package/dist/cli/state.js +23 -0
  18. package/package.json +1 -1
  19. package/dist/compiled-output/example-javascript/messages.d.ts +0 -7
  20. package/dist/compiled-output/example-javascript/messages.js +0 -42
  21. package/dist/compiled-output/example-javascript/runtime.d.ts +0 -38
  22. package/dist/compiled-output/example-javascript/runtime.js +0 -112
  23. package/dist/compiled-output/example-typescript/messages.d.ts +0 -7
  24. package/dist/compiled-output/example-typescript/messages.js +0 -42
  25. package/dist/compiled-output/example-typescript/runtime.d.ts +0 -38
  26. package/dist/compiled-output/example-typescript/runtime.js +0 -112
  27. package/dist/compiled-output/nextjs-example/messages.d.ts +0 -7
  28. package/dist/compiled-output/nextjs-example/messages.js +0 -42
  29. package/dist/compiled-output/nextjs-example/runtime.d.ts +0 -38
  30. package/dist/compiled-output/nextjs-example/runtime.js +0 -112
  31. package/dist/compiled-output/react-example/messages.d.ts +0 -7
  32. package/dist/compiled-output/react-example/messages.js +0 -42
  33. package/dist/compiled-output/react-example/runtime.d.ts +0 -38
  34. package/dist/compiled-output/react-example/runtime.js +0 -112
  35. package/dist/compiled-output/svelte-example/messages.d.ts +0 -7
  36. package/dist/compiled-output/svelte-example/messages.js +0 -42
  37. package/dist/compiled-output/svelte-example/runtime.d.ts +0 -38
  38. package/dist/compiled-output/svelte-example/runtime.js +0 -112
  39. package/dist/compiled-output/sveltekit-example/messages.d.ts +0 -7
  40. package/dist/compiled-output/sveltekit-example/messages.js +0 -42
  41. package/dist/compiled-output/sveltekit-example/runtime.d.ts +0 -38
  42. package/dist/compiled-output/sveltekit-example/runtime.js +0 -112
@@ -1 +1 @@
1
- {"version":3,"file":"compile.d.ts","sourceRoot":"","sources":["../../../src/cli/commands/compile.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AAInC,eAAO,MAAM,cAAc,SAsBxB,CAAA"}
1
+ {"version":3,"file":"compile.d.ts","sourceRoot":"","sources":["../../../src/cli/commands/compile.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AAKnC,eAAO,MAAM,cAAc,SAsBxB,CAAA"}
@@ -4,7 +4,7 @@ import { compile } from "../../compiler/compile.js";
4
4
  import fs from "node:fs/promises";
5
5
  import { resolve } from "node:path";
6
6
  import { Command } from "commander";
7
- import { paraglideDirectory } from "../main.js";
7
+ import { paraglideDirectory } from "../state.js";
8
8
  import dedent from "dedent";
9
9
  export const compileCommand = new Command()
10
10
  .name("compile")
@@ -0,0 +1,3 @@
1
+ import { Command } from "commander";
2
+ export declare const initCommand: Command;
3
+ //# sourceMappingURL=init.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"init.d.ts","sourceRoot":"","sources":["../../../src/cli/commands/init.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AAEnC,eAAO,MAAM,WAAW,SAMrB,CAAA"}
@@ -0,0 +1,10 @@
1
+ import consola from "consola";
2
+ import fs from "node:fs/promises";
3
+ import { Command } from "commander";
4
+ export const initCommand = new Command()
5
+ .name("init")
6
+ .summary("Initializes inlang Paraglide-JS.")
7
+ .action(async () => {
8
+ const file = await fs.readFile("./package.json", "utf-8");
9
+ consola.log(file);
10
+ });
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=inittest.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"inittest.d.ts","sourceRoot":"","sources":["../../../src/cli/commands/inittest.ts"],"names":[],"mappings":""}
@@ -0,0 +1,29 @@
1
+ import { test, expect, vi } from "vitest";
2
+ import { initCommand } from "./init.js";
3
+ import fs from "node:fs/promises";
4
+ test("mock", async () => {
5
+ const spy = vi
6
+ .spyOn(fs, "readFile")
7
+ .mockImplementation(async () => JSON.stringify({ name: "test" }));
8
+ await initCommand.parseAsync(["init"]);
9
+ expect(fs.readFile).toHaveBeenCalledWith("./package.json", "utf-8");
10
+ });
11
+ test("the paraglide plugin for vscode should be installed", () => {
12
+ throw new Error("Not implemented");
13
+ });
14
+ test("it should create a project if it doesn't exist", () => {
15
+ test("the inlang message format should be the default selection", () => {
16
+ throw new Error("Not implemented");
17
+ });
18
+ });
19
+ test("the vscode extension should be added to the workspace recommendations", () => {
20
+ test("automatically if the .vscode folder exists", () => {
21
+ throw new Error("Not implemented");
22
+ });
23
+ test("else, the user should be prompted if the vscode extension should be added", () => {
24
+ throw new Error("Not implemented");
25
+ });
26
+ });
27
+ test("the user should be prompted for the framework and forwarded to the corresponding guide", () => {
28
+ throw new Error("Not implemented");
29
+ });
@@ -1,11 +1,3 @@
1
1
  import { Command } from "commander";
2
- /**
3
- * The absolute path to the paraglide directory.
4
- *
5
- * slices a path
6
- * from '/Users/samuel/example/repository/node_modules/paraglide-js/dist/cli/main.js'
7
- * to '/Users/samuel/example/repository/node_modules/paraglide-js'
8
- */
9
- export declare const paraglideDirectory: string;
10
2
  export declare const cli: Command;
11
3
  //# sourceMappingURL=main.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../../src/cli/main.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AAUnC;;;;;;GAMG;AACH,eAAO,MAAM,kBAAkB,EAAE,MAA4D,CAAA;AAE7F,eAAO,MAAM,GAAG,SAqBb,CAAA"}
1
+ {"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../../src/cli/main.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AAInC,eAAO,MAAM,GAAG,SAuBb,CAAA"}
package/dist/cli/main.js CHANGED
@@ -1,40 +1,28 @@
1
1
  import consola from "consola";
2
2
  import dedent from "dedent";
3
- import { fileURLToPath } from "node:url";
4
3
  import { Command } from "commander";
5
4
  import { compileCommand } from "./commands/compile.js";
6
- /**
7
- * The path this file is executed from.
8
- *
9
- * Usually something like '/Users/samuel/example/repository/node_modules/paraglide-js/dist/cli/main.js'
10
- */
11
- const metaUrlPath = fileURLToPath(import.meta.url);
12
- /**
13
- * The absolute path to the paraglide directory.
14
- *
15
- * slices a path
16
- * from '/Users/samuel/example/repository/node_modules/paraglide-js/dist/cli/main.js'
17
- * to '/Users/samuel/example/repository/node_modules/paraglide-js'
18
- */
19
- export const paraglideDirectory = metaUrlPath.slice(0, metaUrlPath.indexOf("/dist/"));
5
+ import { paraglideDirectory } from "./state.js";
20
6
  export const cli = new Command()
21
7
  .name("paraglide-js")
22
8
  .addCommand(compileCommand)
23
- .hook("preAction", () => {
9
+ .action(() => {
24
10
  // ------------------- VALIDATE IF RUNNING FROM CORRECT FOLDER -------------------
25
11
  // the CLI expects to be running from the dist folder of the specific paraglide package
26
12
  // to compile the output in the correct directory
27
- if (metaUrlPath.includes(`paraglide-js/dist/`) === false) {
13
+ if (paraglideDirectory.endsWith(`paraglide-js`) === false) {
28
14
  consola.error(dedent `
29
- The CLI is not running from the dist folder.
15
+ The CLI is not running from a paraglide-js directory.
30
16
 
31
17
  This is likely an internal bug. Please file an issue at https://github.com/inlang/monorepo/issues.
32
18
 
33
19
  Debug information:
34
20
 
35
- - metaUrlPath: ${metaUrlPath}
21
+ - paraglideDirectory: ${paraglideDirectory}
36
22
  `);
37
- process.exit(1);
23
+ return process.exit(1);
38
24
  }
25
+ // show the help because no command is specified
26
+ return cli.help();
39
27
  });
40
28
  cli.showHelpAfterError(true);
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=main.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"main.test.d.ts","sourceRoot":"","sources":["../../src/cli/main.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,43 @@
1
+ import { describe, test, expect, vi } from "vitest";
2
+ import { _setStateForTest } from "./state.js";
3
+ import { cli } from "./main.js";
4
+ import consola from "consola";
5
+ // surpress output for clean test output
6
+ cli.configureOutput({ writeErr: () => { }, writeOut: () => { } });
7
+ describe("it should exit if the CLI is not running from the dist folder", () => {
8
+ const shouldSucceed = [
9
+ "/Users/example/repository/node_modules/paraglide-js",
10
+ "/node_modules/paraglide-js",
11
+ // windows... https://github.com/inlang/monorepo/issues/1478
12
+ "C:\\Users\\Projects\\svelte-one\\node_modules\\@inlang\\paraglide-js",
13
+ ];
14
+ const shouldFail = ["/Users/samuel/example/repository"];
15
+ const exitSpy = vi.spyOn(process, "exit").mockImplementation(() => undefined);
16
+ // remove error message to have clean test output
17
+ vi.spyOn(consola, "error").mockImplementation(() => undefined);
18
+ for (const path of shouldSucceed) {
19
+ test("suceed with " + path, async () => {
20
+ // surpress output to have clean test output
21
+ _setStateForTest({
22
+ paraglideDirectory: path,
23
+ });
24
+ await cli.parseAsync([]);
25
+ expect(exitSpy).toHaveBeenLastCalledWith(0);
26
+ });
27
+ }
28
+ for (const path of shouldFail) {
29
+ test("fail with " + path, async () => {
30
+ _setStateForTest({
31
+ paraglideDirectory: path,
32
+ });
33
+ await cli.parseAsync([]);
34
+ expect(exitSpy).toHaveBeenLastCalledWith(1);
35
+ });
36
+ }
37
+ // for (const path of shouldFail) {
38
+ // import.meta.url = path
39
+ // const exit = vi.spyOn(process, "exit").mockImplementation(() => undefined as never)
40
+ // await cli.parseAsync([])
41
+ // expect(exit).toHaveBeenCalledWith(1)
42
+ // }
43
+ });
@@ -0,0 +1,10 @@
1
+ /**
2
+ * The absolute path to the paraglide directory.
3
+ *
4
+ * slices a path
5
+ * from '/Users/samuel/example/repository/node_modules/paraglide-js/dist/cli/main.js'
6
+ * to '/Users/samuel/example/repository/node_modules/paraglide-js'
7
+ */
8
+ export declare let paraglideDirectory: string;
9
+ export declare const _setStateForTest: (values: Record<"paraglideDirectory", any>) => void;
10
+ //# sourceMappingURL=state.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"state.d.ts","sourceRoot":"","sources":["../../src/cli/state.ts"],"names":[],"mappings":"AAYA;;;;;;GAMG;AACH,eAAO,IAAI,kBAAkB,EAAE,MAAoE,CAAA;AAInG,eAAO,MAAM,gBAAgB,WAAY,OAAO,oBAAoB,EAAE,GAAG,CAAC,SAEzE,CAAA"}
@@ -0,0 +1,23 @@
1
+ import path from "node:path";
2
+ import { fileURLToPath } from "node:url";
3
+ /**
4
+ * The posix path this file is executed from.
5
+ *
6
+ * Usually something like '/Users/samuel/example/repository/node_modules/paraglide-js/dist/cli/main.js'
7
+ */
8
+ const currentFilePath = fileURLToPath(import.meta.url)
9
+ .split(path.sep)
10
+ .join(path.posix.sep);
11
+ /**
12
+ * The absolute path to the paraglide directory.
13
+ *
14
+ * slices a path
15
+ * from '/Users/samuel/example/repository/node_modules/paraglide-js/dist/cli/main.js'
16
+ * to '/Users/samuel/example/repository/node_modules/paraglide-js'
17
+ */
18
+ export let paraglideDirectory = currentFilePath.slice(0, currentFilePath.indexOf("/dist/"));
19
+ // this is only used for testing
20
+ // use cmd + f to find all usages
21
+ export const _setStateForTest = (values) => {
22
+ paraglideDirectory = values.paraglideDirectory;
23
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@inlang/paraglide-js",
3
3
  "type": "module",
4
- "version": "1.0.0-prerelease.2",
4
+ "version": "1.0.0-prerelease.3",
5
5
  "license": "Apache-2.0",
6
6
  "publishConfig": {
7
7
  "access": "public"
@@ -1,7 +0,0 @@
1
- export function currentLanguageTag(params: {
2
- languageTag: NonNullable<unknown>;
3
- }): string;
4
- export function greeting(params: {
5
- name: NonNullable<unknown>;
6
- count: NonNullable<unknown>;
7
- }): string;
@@ -1,42 +0,0 @@
1
-
2
- import { languageTag } from "./runtime.js"
3
-
4
-
5
- /**
6
- * This message has been compiled by [inlang paraglide](https://inlang.com/marketplace/library.inlang.paraglideJs).
7
- *
8
- * - Don't edit the message manually. Use the [inlang ide extension](https://inlang.com/marketplace/app.inlang.ideExtension)
9
- * or the [web editor](https://inlang.com/marketplace/app.inlang.editor) to edit the message.
10
- *
11
- * - The params are NonNullable<unknown> because inlang can't know the value type of a param (yet).
12
- *
13
- * @param {{ languageTag: NonNullable<unknown> }} params
14
- * @returns {string}
15
- */
16
- export const currentLanguageTag = (params) => {
17
- const variants = {
18
- "en": `The current language tag is "${params.languageTag}".`,
19
- "de": `Der aktuelle Sprachtag ist "${params.languageTag}".`
20
- }
21
- return variants[languageTag()] ?? "currentLanguageTag"
22
- }
23
-
24
-
25
- /**
26
- * This message has been compiled by [inlang paraglide](https://inlang.com/marketplace/library.inlang.paraglideJs).
27
- *
28
- * - Don't edit the message manually. Use the [inlang ide extension](https://inlang.com/marketplace/app.inlang.ideExtension)
29
- * or the [web editor](https://inlang.com/marketplace/app.inlang.editor) to edit the message.
30
- *
31
- * - The params are NonNullable<unknown> because inlang can't know the value type of a param (yet).
32
- *
33
- * @param {{ name: NonNullable<unknown>, count: NonNullable<unknown> }} params
34
- * @returns {string}
35
- */
36
- export const greeting = (params) => {
37
- const variants = {
38
- "en": `Welcome ${params.name}! You have ${params.count} messages.`,
39
- "de": `Hallo ${params.name}! Du hast ${params.count} Nachrichten.`
40
- }
41
- return variants[languageTag()] ?? "greeting"
42
- }
@@ -1,38 +0,0 @@
1
- /**
2
- * The project's source language tag.
3
- *
4
- * @example
5
- * if (newlySelectedLanguageTag === sourceLanguageTag){
6
- * // do nothing as the source language tag is the default language
7
- * return
8
- * }
9
- */
10
- export const sourceLanguageTag: "en";
11
- /**
12
- * The project's available language tags.
13
- *
14
- * @example
15
- * if (availableLanguageTags.includes(userSelectedLanguageTag) === false){
16
- * throw new Error("Language tag not available")
17
- * }
18
- */
19
- export const availableLanguageTags: readonly ["en", "de"];
20
- /**
21
- * Get the current language tag.
22
- *
23
- * @example
24
- * if (languageTag() === "de"){
25
- * console.log("Germany 🇩🇪")
26
- * } else if (languageTag() === "nl"){
27
- * console.log("Netherlands 🇳🇱")
28
- * }
29
- *
30
- * @type {() => AvailableLanguageTag}
31
- */
32
- export let languageTag: () => AvailableLanguageTag;
33
- export function setLanguageTag(tag: AvailableLanguageTag | (() => AvailableLanguageTag)): void;
34
- export function onSetLanguageTag(fn: (languageTag: any) => void): void;
35
- /**
36
- * A language tag that is available in the project.
37
- */
38
- export type AvailableLanguageTag = (typeof availableLanguageTags)[number];
@@ -1,112 +0,0 @@
1
-
2
- /** @type {((tag: AvailableLanguageTag) => void) | undefined} */
3
- let _onSetLanguageTag
4
-
5
- /**
6
- * The project's source language tag.
7
- *
8
- * @example
9
- * if (newlySelectedLanguageTag === sourceLanguageTag){
10
- * // do nothing as the source language tag is the default language
11
- * return
12
- * }
13
- */
14
- export const sourceLanguageTag = "en"
15
-
16
- /**
17
- * The project's available language tags.
18
- *
19
- * @example
20
- * if (availableLanguageTags.includes(userSelectedLanguageTag) === false){
21
- * throw new Error("Language tag not available")
22
- * }
23
- */
24
- export const availableLanguageTags = /** @type {const} */ (["en","de"])
25
-
26
- /**
27
- * Get the current language tag.
28
- *
29
- * @example
30
- * if (languageTag() === "de"){
31
- * console.log("Germany 🇩🇪")
32
- * } else if (languageTag() === "nl"){
33
- * console.log("Netherlands 🇳🇱")
34
- * }
35
- *
36
- * @type {() => AvailableLanguageTag}
37
- */
38
- export let languageTag = () => sourceLanguageTag
39
-
40
- /**
41
- * Set the language tag.
42
- *
43
- * @example
44
- *
45
- * // changing to language
46
- * setLanguageTag("en")
47
- *
48
- * // passing a getter function also works.
49
- * //
50
- * // a getter function is useful for resolving a language tag
51
- * // on the server where every request has a different language tag
52
- * setLanguageTag(() => {
53
- * return request.langaugeTag
54
- * })
55
- *
56
- * @param {AvailableLanguageTag | (() => AvailableLanguageTag)} tag
57
- */
58
- export const setLanguageTag = (tag) => {
59
- if (typeof tag === "function") {
60
- languageTag = tag
61
- } else {
62
- languageTag = () => tag
63
- }
64
- // call the callback function if it has been defined
65
- if (_onSetLanguageTag !== undefined) {
66
- _onSetLanguageTag(languageTag())
67
- }
68
- }
69
-
70
- /**
71
- * Set the `onSetLanguageTag()` callback function.
72
- *
73
- * The function can be used to trigger client-side side-effects such as
74
- * making a new request to the server with the updated language tag,
75
- * or re-rendering the UI on the client (SPA apps).
76
- *
77
- * - Don't use this function on the server (!).
78
- * Triggering a side-effect is only useful on the client because a server-side
79
- * environment doesn't need to re-render the UI.
80
- *
81
- * - The `onSetLanguageTag()` callback can only be defined once to avoid unexpected behavior.
82
- *
83
- * @example
84
- * // if you use inlang paraglide on the server, make sure
85
- * // to not call `onSetLanguageTag()` on the server
86
- * if (isServer === false) {
87
- * onSetLanguageTag((tag) => {
88
- * // (for example) make a new request to the
89
- * // server with the updated language tag
90
- * window.location.href = `/${tag}/${window.location.pathname}`
91
- * })
92
- * }
93
- *
94
- * @param {(languageTag: AvailableLanguageTag) => void} fn
95
- */
96
- export const onSetLanguageTag = (fn) => {
97
- if (_onSetLanguageTag !== undefined) {
98
- throw new Error("@inlang/paraglide-js: The `onSetLanguageTag()` callback has already been defined.\n\nThe `onSetLanguageTag()` callback can only be defined once to avoid unexpected behavior.\n\n 1) Try searching for `onSetLanguageTag()` in your codebase for potential duplicated.\n 2) It might be that your framework is calling `onSetLanguageTag()` multiple times. Try to move the `onSetLanguageTag()` out of the rendering scope like a React component.")
99
- }
100
- _onSetLanguageTag = fn
101
- }
102
-
103
- // ------ TYPES ------
104
-
105
- /**
106
- * A language tag that is available in the project.
107
- *
108
- * @example
109
- * setLanguageTag(request.languageTag as AvailableLanguageTag)
110
- *
111
- * @typedef {typeof availableLanguageTags[number]} AvailableLanguageTag
112
- */
@@ -1,7 +0,0 @@
1
- export function currentLanguageTag(params: {
2
- languageTag: NonNullable<unknown>;
3
- }): string;
4
- export function greeting(params: {
5
- name: NonNullable<unknown>;
6
- count: NonNullable<unknown>;
7
- }): string;
@@ -1,42 +0,0 @@
1
-
2
- import { languageTag } from "./runtime.js"
3
-
4
-
5
- /**
6
- * This message has been compiled by [inlang paraglide](https://inlang.com/marketplace/library.inlang.paraglideJs).
7
- *
8
- * - Don't edit the message manually. Use the [inlang ide extension](https://inlang.com/marketplace/app.inlang.ideExtension)
9
- * or the [web editor](https://inlang.com/marketplace/app.inlang.editor) to edit the message.
10
- *
11
- * - The params are NonNullable<unknown> because inlang can't know the value type of a param (yet).
12
- *
13
- * @param {{ languageTag: NonNullable<unknown> }} params
14
- * @returns {string}
15
- */
16
- export const currentLanguageTag = (params) => {
17
- const variants = {
18
- "en": `The current language tag is "${params.languageTag}".`,
19
- "de": `Der aktuelle Sprachtag ist "${params.languageTag}".`
20
- }
21
- return variants[languageTag()] ?? "currentLanguageTag"
22
- }
23
-
24
-
25
- /**
26
- * This message has been compiled by [inlang paraglide](https://inlang.com/marketplace/library.inlang.paraglideJs).
27
- *
28
- * - Don't edit the message manually. Use the [inlang ide extension](https://inlang.com/marketplace/app.inlang.ideExtension)
29
- * or the [web editor](https://inlang.com/marketplace/app.inlang.editor) to edit the message.
30
- *
31
- * - The params are NonNullable<unknown> because inlang can't know the value type of a param (yet).
32
- *
33
- * @param {{ name: NonNullable<unknown>, count: NonNullable<unknown> }} params
34
- * @returns {string}
35
- */
36
- export const greeting = (params) => {
37
- const variants = {
38
- "en": `Welcome ${params.name}! You have ${params.count} messages.`,
39
- "de": `Hallo ${params.name}! Du hast ${params.count} Nachrichten.`
40
- }
41
- return variants[languageTag()] ?? "greeting"
42
- }
@@ -1,38 +0,0 @@
1
- /**
2
- * The project's source language tag.
3
- *
4
- * @example
5
- * if (newlySelectedLanguageTag === sourceLanguageTag){
6
- * // do nothing as the source language tag is the default language
7
- * return
8
- * }
9
- */
10
- export const sourceLanguageTag: "en";
11
- /**
12
- * The project's available language tags.
13
- *
14
- * @example
15
- * if (availableLanguageTags.includes(userSelectedLanguageTag) === false){
16
- * throw new Error("Language tag not available")
17
- * }
18
- */
19
- export const availableLanguageTags: readonly ["en", "de"];
20
- /**
21
- * Get the current language tag.
22
- *
23
- * @example
24
- * if (languageTag() === "de"){
25
- * console.log("Germany 🇩🇪")
26
- * } else if (languageTag() === "nl"){
27
- * console.log("Netherlands 🇳🇱")
28
- * }
29
- *
30
- * @type {() => AvailableLanguageTag}
31
- */
32
- export let languageTag: () => AvailableLanguageTag;
33
- export function setLanguageTag(tag: AvailableLanguageTag | (() => AvailableLanguageTag)): void;
34
- export function onSetLanguageTag(fn: (languageTag: any) => void): void;
35
- /**
36
- * A language tag that is available in the project.
37
- */
38
- export type AvailableLanguageTag = (typeof availableLanguageTags)[number];
@@ -1,112 +0,0 @@
1
-
2
- /** @type {((tag: AvailableLanguageTag) => void) | undefined} */
3
- let _onSetLanguageTag
4
-
5
- /**
6
- * The project's source language tag.
7
- *
8
- * @example
9
- * if (newlySelectedLanguageTag === sourceLanguageTag){
10
- * // do nothing as the source language tag is the default language
11
- * return
12
- * }
13
- */
14
- export const sourceLanguageTag = "en"
15
-
16
- /**
17
- * The project's available language tags.
18
- *
19
- * @example
20
- * if (availableLanguageTags.includes(userSelectedLanguageTag) === false){
21
- * throw new Error("Language tag not available")
22
- * }
23
- */
24
- export const availableLanguageTags = /** @type {const} */ (["en","de"])
25
-
26
- /**
27
- * Get the current language tag.
28
- *
29
- * @example
30
- * if (languageTag() === "de"){
31
- * console.log("Germany 🇩🇪")
32
- * } else if (languageTag() === "nl"){
33
- * console.log("Netherlands 🇳🇱")
34
- * }
35
- *
36
- * @type {() => AvailableLanguageTag}
37
- */
38
- export let languageTag = () => sourceLanguageTag
39
-
40
- /**
41
- * Set the language tag.
42
- *
43
- * @example
44
- *
45
- * // changing to language
46
- * setLanguageTag("en")
47
- *
48
- * // passing a getter function also works.
49
- * //
50
- * // a getter function is useful for resolving a language tag
51
- * // on the server where every request has a different language tag
52
- * setLanguageTag(() => {
53
- * return request.langaugeTag
54
- * })
55
- *
56
- * @param {AvailableLanguageTag | (() => AvailableLanguageTag)} tag
57
- */
58
- export const setLanguageTag = (tag) => {
59
- if (typeof tag === "function") {
60
- languageTag = tag
61
- } else {
62
- languageTag = () => tag
63
- }
64
- // call the callback function if it has been defined
65
- if (_onSetLanguageTag !== undefined) {
66
- _onSetLanguageTag(languageTag())
67
- }
68
- }
69
-
70
- /**
71
- * Set the `onSetLanguageTag()` callback function.
72
- *
73
- * The function can be used to trigger client-side side-effects such as
74
- * making a new request to the server with the updated language tag,
75
- * or re-rendering the UI on the client (SPA apps).
76
- *
77
- * - Don't use this function on the server (!).
78
- * Triggering a side-effect is only useful on the client because a server-side
79
- * environment doesn't need to re-render the UI.
80
- *
81
- * - The `onSetLanguageTag()` callback can only be defined once to avoid unexpected behavior.
82
- *
83
- * @example
84
- * // if you use inlang paraglide on the server, make sure
85
- * // to not call `onSetLanguageTag()` on the server
86
- * if (isServer === false) {
87
- * onSetLanguageTag((tag) => {
88
- * // (for example) make a new request to the
89
- * // server with the updated language tag
90
- * window.location.href = `/${tag}/${window.location.pathname}`
91
- * })
92
- * }
93
- *
94
- * @param {(languageTag: AvailableLanguageTag) => void} fn
95
- */
96
- export const onSetLanguageTag = (fn) => {
97
- if (_onSetLanguageTag !== undefined) {
98
- throw new Error("@inlang/paraglide-js: The `onSetLanguageTag()` callback has already been defined.\n\nThe `onSetLanguageTag()` callback can only be defined once to avoid unexpected behavior.\n\n 1) Try searching for `onSetLanguageTag()` in your codebase for potential duplicated.\n 2) It might be that your framework is calling `onSetLanguageTag()` multiple times. Try to move the `onSetLanguageTag()` out of the rendering scope like a React component.")
99
- }
100
- _onSetLanguageTag = fn
101
- }
102
-
103
- // ------ TYPES ------
104
-
105
- /**
106
- * A language tag that is available in the project.
107
- *
108
- * @example
109
- * setLanguageTag(request.languageTag as AvailableLanguageTag)
110
- *
111
- * @typedef {typeof availableLanguageTags[number]} AvailableLanguageTag
112
- */
@@ -1,7 +0,0 @@
1
- export function currentLanguageTag(params: {
2
- languageTag: NonNullable<unknown>;
3
- }): string;
4
- export function greeting(params: {
5
- name: NonNullable<unknown>;
6
- count: NonNullable<unknown>;
7
- }): string;