@inlang/paraglide-js 1.0.0-prerelease.0
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/README.md +239 -0
- package/bin/run.js +5 -0
- package/dist/cli/commands/compile.d.ts +3 -0
- package/dist/cli/commands/compile.d.ts.map +1 -0
- package/dist/cli/commands/compile.js +57 -0
- package/dist/cli/main.d.ts +11 -0
- package/dist/cli/main.d.ts.map +1 -0
- package/dist/cli/main.js +40 -0
- package/dist/compiled-output/example-javascript/messages.d.ts +7 -0
- package/dist/compiled-output/example-javascript/messages.js +42 -0
- package/dist/compiled-output/example-javascript/runtime.d.ts +38 -0
- package/dist/compiled-output/example-javascript/runtime.js +112 -0
- package/dist/compiled-output/example-typescript/messages.d.ts +7 -0
- package/dist/compiled-output/example-typescript/messages.js +42 -0
- package/dist/compiled-output/example-typescript/runtime.d.ts +38 -0
- package/dist/compiled-output/example-typescript/runtime.js +112 -0
- package/dist/compiled-output/nextjs-example/messages.d.ts +7 -0
- package/dist/compiled-output/nextjs-example/messages.js +42 -0
- package/dist/compiled-output/nextjs-example/runtime.d.ts +38 -0
- package/dist/compiled-output/nextjs-example/runtime.js +112 -0
- package/dist/compiled-output/react-example/messages.d.ts +7 -0
- package/dist/compiled-output/react-example/messages.js +42 -0
- package/dist/compiled-output/react-example/runtime.d.ts +38 -0
- package/dist/compiled-output/react-example/runtime.js +112 -0
- package/dist/compiled-output/svelte-example/messages.d.ts +7 -0
- package/dist/compiled-output/svelte-example/messages.js +42 -0
- package/dist/compiled-output/svelte-example/runtime.d.ts +38 -0
- package/dist/compiled-output/svelte-example/runtime.js +112 -0
- package/dist/compiled-output/sveltekit-example/messages.d.ts +7 -0
- package/dist/compiled-output/sveltekit-example/messages.js +42 -0
- package/dist/compiled-output/sveltekit-example/runtime.d.ts +38 -0
- package/dist/compiled-output/sveltekit-example/runtime.js +112 -0
- package/dist/compiler/compile.d.ts +15 -0
- package/dist/compiler/compile.d.ts.map +1 -0
- package/dist/compiler/compile.js +170 -0
- package/dist/compiler/compile.test.d.ts +2 -0
- package/dist/compiler/compile.test.d.ts.map +1 -0
- package/dist/compiler/compile.test.js +302 -0
- package/dist/compiler/compileMessage.d.ts +3 -0
- package/dist/compiler/compileMessage.d.ts.map +1 -0
- package/dist/compiler/compileMessage.js +44 -0
- package/dist/compiler/compileMessage.test.d.ts +2 -0
- package/dist/compiler/compileMessage.test.d.ts.map +1 -0
- package/dist/compiler/compileMessage.test.js +91 -0
- package/dist/compiler/compilePattern.d.ts +18 -0
- package/dist/compiler/compilePattern.d.ts.map +1 -0
- package/dist/compiler/compilePattern.js +28 -0
- package/dist/compiler/compilePattern.test.d.ts +2 -0
- package/dist/compiler/compilePattern.test.d.ts.map +1 -0
- package/dist/compiler/compilePattern.test.js +26 -0
- package/dist/compiler/jsdocFromParams.d.ts +12 -0
- package/dist/compiler/jsdocFromParams.d.ts.map +1 -0
- package/dist/compiler/jsdocFromParams.js +17 -0
- package/dist/compiler/jsdocFromParams.test.d.ts +2 -0
- package/dist/compiler/jsdocFromParams.test.d.ts.map +1 -0
- package/dist/compiler/jsdocFromParams.test.js +6 -0
- package/dist/compiler/paramsType.d.ts +12 -0
- package/dist/compiler/paramsType.d.ts.map +1 -0
- package/dist/compiler/paramsType.js +17 -0
- package/dist/compiler/paramsType.test.d.ts +2 -0
- package/dist/compiler/paramsType.test.d.ts.map +1 -0
- package/dist/compiler/paramsType.test.js +6 -0
- package/package.json +44 -0
|
@@ -0,0 +1,112 @@
|
|
|
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
|
+
*/
|
|
@@ -0,0 +1,42 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
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];
|
|
@@ -0,0 +1,112 @@
|
|
|
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
|
+
*/
|
|
@@ -0,0 +1,42 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
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];
|
|
@@ -0,0 +1,112 @@
|
|
|
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
|
+
*/
|
|
@@ -0,0 +1,42 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
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];
|