mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-23 19:49:50 +02:00
feat: string hooks
This commit is contained in:
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;
|
||||
}
|
||||
Reference in new issue
Block a user