-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2277 from opral/2216-fix-language-switcher
SvelteKit Adapter Update
- Loading branch information
Showing
20 changed files
with
534 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@inlang/paraglide-js-adapter-sveltekit": patch | ||
--- | ||
|
||
fix reactivity issue in Svelte 5 [#2270](https://github.com/opral/monorepo/issues/2270) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@inlang/paraglide-js-adapter-sveltekit": minor | ||
--- | ||
|
||
feat: Automatically call `invalidate("paraglide:lang")` when the language changes. You can now call `depends("paraglide:lang")` in your server-load functions to have them re-run on language changes. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@inlang/paraglide-js-adapter-sveltekit": patch | ||
--- | ||
|
||
fix: Corrected comments saying the default placeholder for the text-direction is `%paraglide.dir%` when it's `%paraglide.textDirection%` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@inlang/paraglide-js-adapter-sveltekit": patch | ||
--- | ||
|
||
fix: `i18n.resolveRoute` now ignores paths that don't start with the base |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@inlang/paraglide-js-adapter-sveltekit": patch | ||
--- | ||
|
||
fix: Alternate links will not include the origin during prerendering, unless one is explicitly specified in `kit.prerender.origin` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
...source-code/paraglide/paraglide-js-adapter-sveltekit/example/src/routes/+layout.server.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,13 @@ | ||
export const prerender = true | ||
|
||
/** | ||
* @type { import("./$types").LayoutServerLoad} | ||
*/ | ||
export function load({ locals, depends }) { | ||
// This tells SvelteKit to re-run this load function when the language changes | ||
depends("paraglide:lang") | ||
|
||
return { | ||
serverLang: `The language on the server is ${locals.paraglide.lang}`, | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
...ng/source-code/paraglide/paraglide-js-adapter-sveltekit/example/src/routes/+layout.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
<script> | ||
import { ParaglideJS } from "@inlang/paraglide-js-adapter-sveltekit" | ||
import { i18n } from "$lib/i18n.js" | ||
export let data; | ||
</script> | ||
|
||
<p>{data.serverLang}</p> | ||
|
||
<ParaglideJS {i18n}> | ||
<slot /> | ||
</ParaglideJS> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
inlang/source-code/paraglide/paraglide-js-adapter-sveltekit/mocks/runtime.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
/* eslint-disable */ | ||
/** @type {((tag: AvailableLanguageTag) => void) | undefined} */ | ||
let _onSetLanguageTag | ||
|
||
/** | ||
* The project's source language tag. | ||
* | ||
* @example | ||
* if (newlySelectedLanguageTag === sourceLanguageTag){ | ||
* // do nothing as the source language tag is the default language | ||
* return | ||
* } | ||
*/ | ||
export const sourceLanguageTag = "en" | ||
|
||
/** | ||
* The project's available language tags. | ||
* | ||
* @example | ||
* if (availableLanguageTags.includes(userSelectedLanguageTag) === false){ | ||
* throw new Error("Language tag not available") | ||
* } | ||
*/ | ||
export const availableLanguageTags = /** @type {const} */ (["de", "en"]) | ||
|
||
/** | ||
* Get the current language tag. | ||
* | ||
* @example | ||
* if (languageTag() === "de"){ | ||
* console.log("Germany 🇩🇪") | ||
* } else if (languageTag() === "nl"){ | ||
* console.log("Netherlands 🇳🇱") | ||
* } | ||
* | ||
* @type {() => AvailableLanguageTag} | ||
*/ | ||
export let languageTag = () => sourceLanguageTag | ||
|
||
/** | ||
* Set the language tag. | ||
* | ||
* @example | ||
* | ||
* // changing to language | ||
* setLanguageTag("en") | ||
* | ||
* // passing a getter function also works. | ||
* // | ||
* // a getter function is useful for resolving a language tag | ||
* // on the server where every request has a different language tag | ||
* setLanguageTag(() => { | ||
* return request.langaugeTag | ||
* }) | ||
* | ||
* @param {AvailableLanguageTag | (() => AvailableLanguageTag)} tag | ||
*/ | ||
export const setLanguageTag = (tag) => { | ||
if (typeof tag === "function") { | ||
languageTag = enforceLanguageTag(tag) | ||
} else { | ||
languageTag = enforceLanguageTag(() => tag) | ||
} | ||
// call the callback function if it has been defined | ||
if (_onSetLanguageTag !== undefined) { | ||
_onSetLanguageTag(languageTag()) | ||
} | ||
} | ||
|
||
/** | ||
* Wraps an untrusted function and enforces that it returns a language tag. | ||
* @param {() => AvailableLanguageTag} unsafeLanguageTag | ||
* @returns {() => AvailableLanguageTag} | ||
*/ | ||
function enforceLanguageTag(unsafeLanguageTag) { | ||
return () => { | ||
const tag = unsafeLanguageTag() | ||
if (!isAvailableLanguageTag(tag)) { | ||
throw new Error( | ||
`languageTag() didn't return a valid language tag. Check your setLanguageTag call` | ||
) | ||
} | ||
return tag | ||
} | ||
} | ||
|
||
/** | ||
* Set the `onSetLanguageTag()` callback function. | ||
* | ||
* The function can be used to trigger client-side side-effects such as | ||
* making a new request to the server with the updated language tag, | ||
* or re-rendering the UI on the client (SPA apps). | ||
* | ||
* - Don't use this function on the server (!). | ||
* Triggering a side-effect is only useful on the client because a server-side | ||
* environment doesn't need to re-render the UI. | ||
* | ||
* - The `onSetLanguageTag()` callback can only be defined once to avoid unexpected behavior. | ||
* | ||
* @example | ||
* // if you use inlang paraglide on the server, make sure | ||
* // to not call `onSetLanguageTag()` on the server | ||
* if (isServer === false) { | ||
* onSetLanguageTag((tag) => { | ||
* // (for example) make a new request to the | ||
* // server with the updated language tag | ||
* window.location.href = `/${tag}/${window.location.pathname}` | ||
* }) | ||
* } | ||
* | ||
* @param {(languageTag: AvailableLanguageTag) => void} fn | ||
*/ | ||
export const onSetLanguageTag = (fn) => { | ||
_onSetLanguageTag = fn | ||
} | ||
|
||
/** | ||
* Check if something is an available language tag. | ||
* | ||
* @example | ||
* if (isAvailableLanguageTag(params.locale)) { | ||
* setLanguageTag(params.locale) | ||
* } else { | ||
* setLanguageTag("en") | ||
* } | ||
* | ||
* @param {any} thing | ||
* @returns {thing is AvailableLanguageTag} | ||
*/ | ||
export function isAvailableLanguageTag(thing) { | ||
return availableLanguageTags.includes(thing) | ||
} | ||
|
||
// ------ TYPES ------ | ||
|
||
/** | ||
* A language tag that is available in the project. | ||
* | ||
* @example | ||
* setLanguageTag(request.languageTag as AvailableLanguageTag) | ||
* | ||
* @typedef {typeof availableLanguageTags[number]} AvailableLanguageTag | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.