mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-23 19:49:50 +02:00
feat: remote strings
This commit is contained in:
1 parent
af53713c60
commit
2e13bcfee2
2 files changed
+24
-4
No files matched your search
@@ -1,7 +1,6 @@
|
|||||||
import locales from '@blitzkit/i18n/locales.json' with { type: 'json' };
|
import locales from '@blitzkit/i18n/locales.json' with { type: 'json' };
|
||||||
import type { BlitzKitStrings } from 'packages/i18n/src';
|
import type { BlitzKitStrings } from 'packages/i18n/src';
|
||||||
import { createContext, use, useContext } from 'react';
|
import { createContext, use, useContext } from 'react';
|
||||||
|
|
||||||
const LocaleContext = createContext<{
|
const LocaleContext = createContext<{
|
||||||
locale: string;
|
locale: string;
|
||||||
gameStrings: Record<string, string>;
|
gameStrings: Record<string, string>;
|
||||||
@@ -45,9 +44,17 @@ const cache = new Map<string, Promise<BlitzKitStrings>>();
|
|||||||
function loadLocale(locale: string) {
|
function loadLocale(locale: string) {
|
||||||
if (cache.has(locale)) return cache.get(locale)!;
|
if (cache.has(locale)) return cache.get(locale)!;
|
||||||
|
|
||||||
const promise = strings[
|
let promise: Promise<BlitzKitStrings>;
|
||||||
`../../../i18n/strings/${locale}.json`
|
|
||||||
]() as Promise<BlitzKitStrings>;
|
if (import.meta.env.SSR) {
|
||||||
|
promise = import('../core/i18n/getStrings').then((module) =>
|
||||||
|
module.getStrings(locale),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
promise = fetch(`/api/strings/${locale}.json`).then((response) =>
|
||||||
|
response.json(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
cache.set(locale, promise);
|
cache.set(locale, promise);
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
import locales from '@blitzkit/i18n/locales.json';
|
||||||
|
import type { APIRoute, GetStaticPaths } from 'astro';
|
||||||
|
import { getStrings } from 'packages/website-ue/src/core/i18n/getStrings';
|
||||||
|
|
||||||
|
export const getStaticPaths: GetStaticPaths = () => {
|
||||||
|
return locales.supported.map(({ locale }) => ({
|
||||||
|
params: { locale },
|
||||||
|
}));
|
||||||
|
};
|
||||||
|
|
||||||
|
export const GET: APIRoute<{}, { locale: string }> = async ({ params }) => {
|
||||||
|
return Response.json(await getStrings(params.locale));
|
||||||
|
};
|
||||||
Reference in new issue
Block a user