aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib
diff options
context:
space:
mode:
authorivarlovlie <git@ivarlovlie.no>2023-02-11 23:37:12 +0100
committerivarlovlie <git@ivarlovlie.no>2023-02-11 23:37:12 +0100
commit9b2c63d92ff77ebce0f90a7be05437504422bf45 (patch)
tree682447cde9d3eed555973ff1aff369b4443da07f /src/lib
parent23ab0a24ba82004fa449491b4e08698a1de9e6a0 (diff)
downloadauroraklinikken.no-9b2c63d92ff77ebce0f90a7be05437504422bf45.tar.xz
auroraklinikken.no-9b2c63d92ff77ebce0f90a7be05437504422bf45.zip
feat: Render localized content from sanity
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/sanity-client.ts9
-rw-r--r--src/lib/utils.ts27
2 files changed, 36 insertions, 0 deletions
diff --git a/src/lib/sanity-client.ts b/src/lib/sanity-client.ts
new file mode 100644
index 0000000..70be363
--- /dev/null
+++ b/src/lib/sanity-client.ts
@@ -0,0 +1,9 @@
+import { env } from "$env/dynamic/private";
+import sanityClient from "@sanity/client";
+
+export const sanity = sanityClient({
+ projectId: env.SANITY_STUDIO_API_PROJECT_ID,
+ dataset: env.SANITY_STUDIO_API_DATASET,
+ apiVersion: "2022-03-24",
+ useCdn: true,
+}); \ No newline at end of file
diff --git a/src/lib/utils.ts b/src/lib/utils.ts
new file mode 100644
index 0000000..4ec8b01
--- /dev/null
+++ b/src/lib/utils.ts
@@ -0,0 +1,27 @@
+// Replaces the locale slug in a URL.
+//
+// If the `full` argument is set to `true`, the full URL is returned as a string.
+// e.g. https://mywebsite.com/en/blog/article-1 => https://mywebsite.com/de/blog/article-1
+//
+// Otherwise (default) the URL relative to the base is returned.
+// e.g. https://mywebsite.com/en/blog/article-1 => /de/blog/article-1
+export const replaceLocaleInUrl = (url: URL, locale: string, full = false): string => {
+ const [, , ...rest] = url.pathname.split('/')
+ const new_pathname = `/${[locale, ...rest].join('/')}`
+ if (!full) {
+ return `${new_pathname}${url.search}`
+ }
+ const newUrl = new URL(url.toString())
+ newUrl.pathname = new_pathname
+ return newUrl.toString()
+}
+
+export function fromLocalizedString(localizedString: string | object, locale: Locales) {
+ if (typeof localizedString === "string") return localizedString;
+ // @ts-ignore
+ if (localizedString[locale]) return localizedString[locale];
+ // @ts-ignore
+ if (localizedString["nb"]) return localizedString["nb"];
+ // @ts-ignore
+ if (localizedString["en"]) return localizedString["en"];
+}