mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-12-04 23:32:54 +01:00
migrate settings
This commit is contained in:
25 files changed
+335
-237
No files matched your search
@@ -0,0 +1,12 @@
|
||||
import locales from "@blitzkit/i18n/locales.json";
|
||||
|
||||
export function getCurrentPathLocale() {
|
||||
const pathname = window.location.pathname;
|
||||
|
||||
return (
|
||||
locales.supported.find(
|
||||
({ locale }) =>
|
||||
pathname === `/${locale}` || pathname.startsWith(`/${locale}/`),
|
||||
)?.locale ?? locales.default
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import locales from "@blitzkit/i18n/locales.json";
|
||||
import { api } from "../../api/dynamic";
|
||||
|
||||
export interface LocaleOption {
|
||||
locale: string;
|
||||
flag: string;
|
||||
name: string;
|
||||
}
|
||||
|
||||
// each locale is named in its own language; run at build time so clients never fetch every locale's strings
|
||||
export async function getLocaleOptions() {
|
||||
return Promise.all(
|
||||
locales.supported.map(async ({ locale, flag }) => {
|
||||
const strings = await api.strings(locale);
|
||||
const name = (strings.common.locales as Record<string, string>)[locale];
|
||||
|
||||
return { locale, flag, name } satisfies LocaleOption;
|
||||
}),
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import locales from "@blitzkit/i18n/locales.json";
|
||||
import { getCurrentPathLocale } from "./getCurrentPathLocale";
|
||||
|
||||
// keeps the current page, swapping only the locale prefix
|
||||
export function navigateToLocale(locale: string) {
|
||||
const pathname = window.location.pathname;
|
||||
const currentLocale = getCurrentPathLocale();
|
||||
const rawPath =
|
||||
currentLocale === locales.default
|
||||
? pathname
|
||||
: pathname.slice(`/${currentLocale}`.length) || "/";
|
||||
|
||||
window.location.pathname = `${
|
||||
locale === locales.default ? "" : `/${locale}`
|
||||
}${rawPath}`;
|
||||
}
|
||||
Reference in new issue
Block a user