feat(web): integrate localized application catalogs
- compose app, UI, and independently imported block catalogs from JSON configuration\n- persist the selected locale on the server and activate it without hydration flicker\n- add a flag-based language switcher and localized navigation descriptors\n- mount the redesigned i18n devtool only in development\n- regenerate English and Simplified Chinese catalogs and add switcher coverage
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
import { createServerFn } from "@tanstack/react-start"
|
||||
|
||||
import { isAppLocale } from "@/i18n/app-locales"
|
||||
import { readAppLocale, writeAppLocale } from "./app-locale.server"
|
||||
|
||||
export const getAppLocale = createServerFn({ method: "GET" }).handler(() => {
|
||||
return readAppLocale()
|
||||
})
|
||||
|
||||
export const setAppLocale = createServerFn({ method: "POST" })
|
||||
.validator((value: unknown) => {
|
||||
if (!isAppLocale(value)) {
|
||||
throw new Error("Invalid app locale")
|
||||
}
|
||||
|
||||
return value
|
||||
})
|
||||
.handler(({ data }) => {
|
||||
writeAppLocale(data)
|
||||
})
|
||||
@@ -0,0 +1,25 @@
|
||||
import "@tanstack/react-start/server-only"
|
||||
import { getCookie, setCookie } from "@tanstack/react-start/server"
|
||||
|
||||
import {
|
||||
DEFAULT_APP_LOCALE,
|
||||
isAppLocale,
|
||||
type AppLocale,
|
||||
} from "@/i18n/app-locales"
|
||||
|
||||
const APP_LOCALE_COOKIE = "ui-locale"
|
||||
const COOKIE_MAX_AGE = 60 * 60 * 24 * 365
|
||||
|
||||
export function readAppLocale(): AppLocale {
|
||||
const locale = getCookie(APP_LOCALE_COOKIE)
|
||||
|
||||
return isAppLocale(locale) ? locale : DEFAULT_APP_LOCALE
|
||||
}
|
||||
|
||||
export function writeAppLocale(locale: AppLocale) {
|
||||
setCookie(APP_LOCALE_COOKIE, locale, {
|
||||
maxAge: COOKIE_MAX_AGE,
|
||||
path: "/",
|
||||
sameSite: "lax",
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user