feat: string hooks

This commit is contained in:
TrèsAbhi committed 2026-01-13 20:15:55 -06:00
1 parent f5f3f70e35
commit 27abf1b0de
7 files changed
+82 -12

No files matched your search

@@ -0,0 +1,13 @@
import { api } from "../core/api/dynamic";
import { useAwait } from "./useAwait";
import { useLocale } from "./useLocale";
export function useGameStrings(group: string) {
const { locale } = useLocale();
const strings = useAwait(
() => api.groupedGameStrings(locale, group),
`game-strings-${locale}-${group}`
);
return strings;
}
@@ -0,0 +1,39 @@
import locales from "@blitzkit/i18n/locales.json";
import { createContext, useContext, type ReactNode } from "react";
import { api } from "../core/api/dynamic";
import { useAwait } from "./useAwait";
const LocaleContext = createContext<{
locale: string;
} | null>(null);
interface LocaleProviderProps extends LocaleAcceptorProps {
children: ReactNode;
}
export function LocaleProvider({ locale, children }: LocaleProviderProps) {
return (
<LocaleContext.Provider value={{ locale: locale ?? locales.default }}>
{children}
</LocaleContext.Provider>
);
}
export function useLocale() {
const context = useContext(LocaleContext);
if (!context) {
throw new Error("useLocale must be used within a LocaleProvider");
}
const strings = useAwait(
() => api.strings(context.locale),
`strings-${context.locale}`
);
return { locale: context.locale, strings };
}
export interface LocaleAcceptorProps {
locale: string | undefined;
}