mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-21 18:57:03 +02:00
api strings
This commit is contained in:
1 parent
99317c2075
commit
5f24887535
23 files changed
+132
-118
No files matched your search
@@ -0,0 +1,5 @@
|
||||
export async function fetchJSON<Type>(url: string) {
|
||||
const response = await fetch(url);
|
||||
const json = await response.json();
|
||||
return json as Type;
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { MessageFns } from '../../../protos/src/blitzkit';
|
||||
import type { MessageFns } from "@blitzkit/protos/src/blitzkit";
|
||||
|
||||
export async function fetchPB<Type>(url: string, message: MessageFns<Type>) {
|
||||
const response = await fetch(url);
|
||||
File renamed without changes.
@@ -10,9 +10,10 @@ import {
|
||||
SkillDefinitions,
|
||||
TankDefinitions,
|
||||
} from "@blitzkit/core";
|
||||
import { Strings } from "@blitzkit/i18n";
|
||||
import { BlitzScripts } from "../../types/blitzScripts";
|
||||
|
||||
export abstract class BlitzKitAPI {
|
||||
export abstract class AbstractBlitzKitAPI {
|
||||
abstract skills(): Promise<SkillDefinitions>;
|
||||
abstract provisions(): Promise<ProvisionDefinitions>;
|
||||
abstract consumables(): Promise<ConsumableDefinitions>;
|
||||
@@ -25,6 +26,8 @@ export abstract class BlitzKitAPI {
|
||||
abstract gallery(): Promise<Gallery>;
|
||||
abstract scripts(): Promise<BlitzScripts>;
|
||||
|
||||
abstract strings(locale: string): Promise<Strings>;
|
||||
|
||||
async tank(id: number) {
|
||||
const tanks = await this.tanks();
|
||||
return tanks.tanks[id];
|
||||
@@ -1,4 +1,5 @@
|
||||
import { alias } from "@blitzkit/core";
|
||||
import { BlitzKitStrings } from "@blitzkit/i18n";
|
||||
import {
|
||||
CamouflageDefinitions,
|
||||
ConsumableDefinitions,
|
||||
@@ -11,11 +12,12 @@ import {
|
||||
SkillDefinitions,
|
||||
TankDefinitions,
|
||||
} from "@blitzkit/protos";
|
||||
import { fetchPB } from "../../protobuf";
|
||||
import { fetchPB } from "../../api";
|
||||
import { fetchJSON } from "../../api/fetchJSON";
|
||||
import { BlitzScripts } from "../../types/blitzScripts";
|
||||
import { BlitzKitAPI } from "./base";
|
||||
import { AbstractBlitzKitAPI } from "./abstract";
|
||||
|
||||
export class ClientBlitzKitAPI extends BlitzKitAPI {
|
||||
export class ClientBlitzKitAPI extends AbstractBlitzKitAPI {
|
||||
game() {
|
||||
return fetchPB(alias("api", "/definitions/game.pb"), GameDefinitions);
|
||||
}
|
||||
@@ -68,9 +70,11 @@ export class ClientBlitzKitAPI extends BlitzKitAPI {
|
||||
return fetchPB(alias("api", "/definitions/maps.pb"), MapDefinitions);
|
||||
}
|
||||
|
||||
async scripts() {
|
||||
const response = await fetch(alias("api", "/definitions/scripts.json"));
|
||||
const json = await response.json();
|
||||
return json as BlitzScripts;
|
||||
scripts() {
|
||||
return fetchJSON<BlitzScripts>(alias("api", "/definitions/scripts.json"));
|
||||
}
|
||||
|
||||
strings(locale: string) {
|
||||
return fetchJSON<BlitzKitStrings>(alias("api", `/strings/${locale}.json`));
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
import { AbstractVFS, BlitzKitAPI } from "@blitzkit/core";
|
||||
import { AbstractBlitzKitAPI, AbstractVFS } from "@blitzkit/core";
|
||||
|
||||
export async function createDynamicAPI(vfs: () => Promise<AbstractVFS>) {
|
||||
let _api: BlitzKitAPI;
|
||||
let _api: AbstractBlitzKitAPI;
|
||||
|
||||
if (import.meta.env.SSR) {
|
||||
const { ServerBlitzKitAPI } = await import(
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
export * from "./base";
|
||||
export * from "./abstract";
|
||||
export * from "./client";
|
||||
export * from "./dynamic";
|
||||
export * from "./server";
|
||||
@@ -25,13 +25,12 @@ import {
|
||||
VehicleDefinitionList,
|
||||
} from "@blitzkit/core";
|
||||
import { AbstractVFS } from "@blitzkit/core/src/blitzkit/vfs/abstract";
|
||||
import { SUPPORTED_LOCALE_BLITZ_MAP } from "@blitzkit/i18n";
|
||||
import locales from "@blitzkit/i18n/locales.json";
|
||||
import type { extname, parse } from "node:path";
|
||||
import { parse as parseYaml } from "yaml";
|
||||
import { BlitzKitAPI } from "../base";
|
||||
import { AbstractBlitzKitAPI } from "../abstract";
|
||||
|
||||
export abstract class ServerBlitzKitAPI0 extends BlitzKitAPI {
|
||||
export abstract class ServerBlitzKitAPI0 extends AbstractBlitzKitAPI {
|
||||
protected vfs: AbstractVFS;
|
||||
protected parsePath?: typeof parse;
|
||||
protected extname?: typeof extname;
|
||||
@@ -171,8 +170,8 @@ export abstract class ServerBlitzKitAPI0 extends BlitzKitAPI {
|
||||
|
||||
let fetchedLocalizations = 0;
|
||||
await Promise.all(
|
||||
locales.supported.map(async ({ locale }) => {
|
||||
const blitzLocale = SUPPORTED_LOCALE_BLITZ_MAP[locale];
|
||||
locales.supported.map(async (locale) => {
|
||||
const blitzLocale = locale.variant_blitz_cdn ?? locale.locale;
|
||||
const cache = await fetch(
|
||||
`https://stufficons.wgcdn.co/localizations/${blitzLocale}.yaml`,
|
||||
)
|
||||
@@ -182,7 +181,7 @@ export abstract class ServerBlitzKitAPI0 extends BlitzKitAPI {
|
||||
`Data/Strings/${blitzLocale}.yaml`,
|
||||
);
|
||||
|
||||
this.stringsI18n[locale] = {
|
||||
this.stringsI18n[locale.locale] = {
|
||||
...cache,
|
||||
...preInstalled,
|
||||
};
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
import { ConsumablesCommon, OptionalDevices } from "../../../types";
|
||||
import { BlitzScripts } from "../../../types/blitzScripts";
|
||||
import { AbstractVFS } from "../../vfs";
|
||||
import { Cache } from "./0_base";
|
||||
import { ServerBlitzKitAPI10 } from "./10_avatars";
|
||||
|
||||
export abstract class ServerBlitzKitAPI11 extends ServerBlitzKitAPI10 {
|
||||
@Cache()
|
||||
async scripts() {
|
||||
const scripts: BlitzScripts = {
|
||||
consumables: {},
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
import { Strings } from "@blitzkit/i18n";
|
||||
import locales from "@blitzkit/i18n/locales.json";
|
||||
import { DeepPartial } from "@blitzkit/protos";
|
||||
import { merge } from "lodash-es";
|
||||
import { Cache } from "./0_base";
|
||||
import { ServerBlitzKitAPI11 } from "./11_scripts";
|
||||
|
||||
const globbedStrings = import.meta.glob("../../../../../i18n/strings/*.json", {
|
||||
import: "default",
|
||||
});
|
||||
|
||||
export abstract class ServerBlitzKitAPI12 extends ServerBlitzKitAPI11 {
|
||||
@Cache()
|
||||
async strings(locale: string) {
|
||||
const localized = (await globbedStrings[
|
||||
`../../../../../i18n/strings/${locale}.json`
|
||||
]()) as DeepPartial<Strings>;
|
||||
const defaults = (await globbedStrings[
|
||||
`../../../../../i18n/strings/${locales.default}.json`
|
||||
]()) as Strings;
|
||||
const strings = merge({}, defaults, localized);
|
||||
|
||||
return strings;
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,3 @@
|
||||
import { ServerBlitzKitAPI11 } from "./11_scripts";
|
||||
import { ServerBlitzKitAPI12 } from "./12_strings";
|
||||
|
||||
export class ServerBlitzKitAPI extends ServerBlitzKitAPI11 {}
|
||||
export class ServerBlitzKitAPI extends ServerBlitzKitAPI12 {}
|
||||
@@ -1,6 +1,6 @@
|
||||
import { staticAsset } from "@blitzkit/core";
|
||||
import { fetchPB } from "../../protobuf";
|
||||
import { AverageDefinitions } from "../../../../protos/src/blitzkit";
|
||||
import { fetchPB } from "../../api";
|
||||
|
||||
export interface AverageDefinitionsManifest {
|
||||
version: 1;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { staticAsset } from "@blitzkit/core";
|
||||
import { fetchPB } from "../protobuf";
|
||||
import { PopularTanks } from "../../../protos/src/blitzkit";
|
||||
import { fetchPB } from "../api";
|
||||
|
||||
export function fetchPopularTanks() {
|
||||
return fetchPB(staticAsset("definitions/popular-tanks.pb"), PopularTanks);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { staticAsset } from "@blitzkit/core";
|
||||
import { fetchPB } from "../protobuf";
|
||||
import { Reviews } from "../../../protos/src/blitzkit";
|
||||
import { fetchPB } from "../api";
|
||||
|
||||
export function fetchReviews() {
|
||||
return fetchPB(staticAsset("definitions/reviews.pb"), Reviews);
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
export * from "../../protos/src/blitzkit";
|
||||
export * from "./api";
|
||||
export * from "./blitz";
|
||||
export * from "./blitzkit";
|
||||
export * from "./imgur";
|
||||
export * from "./math";
|
||||
export * from "./patreon";
|
||||
export * from "./protobuf";
|
||||
export * from "../../protos/src/blitzkit";
|
||||
export * from "./statistics";
|
||||
export * from "./streams";
|
||||
export * from "./types";
|
||||
@@ -1 +0,0 @@
|
||||
export type PBMap<T> = T[keyof T] extends number ? T[keyof T] : never;
|
||||
@@ -1,14 +1,45 @@
|
||||
{
|
||||
"default": "en",
|
||||
"supported": [
|
||||
{ "locale": "en" },
|
||||
{ "locale": "es" },
|
||||
{ "locale": "fr" },
|
||||
{ "locale": "ja" },
|
||||
{ "locale": "pl" },
|
||||
{ "locale": "pt" },
|
||||
{ "locale": "ru" },
|
||||
{ "locale": "uk" },
|
||||
{ "locale": "zh", "blitz": "zh_Hans_SG" }
|
||||
{
|
||||
"locale": "en",
|
||||
"flag": "🇺🇸"
|
||||
},
|
||||
{
|
||||
"locale": "es",
|
||||
"flag": "🇪🇸"
|
||||
},
|
||||
{
|
||||
"locale": "fr",
|
||||
"flag": "🇫🇷"
|
||||
},
|
||||
{
|
||||
"locale": "ja",
|
||||
"flag": "🇯🇵"
|
||||
},
|
||||
{
|
||||
"locale": "pl",
|
||||
"flag": "🇵🇱"
|
||||
},
|
||||
{
|
||||
"locale": "pt",
|
||||
"flag": "🇵🇹"
|
||||
},
|
||||
{
|
||||
"locale": "ru",
|
||||
"flag": "🇷🇺"
|
||||
},
|
||||
{
|
||||
"locale": "uk",
|
||||
"flag": "🇺🇦"
|
||||
},
|
||||
{
|
||||
"locale": "zh",
|
||||
|
||||
"variant_blitz_local": "zh_Hans_SG",
|
||||
"variant_blitz_cdn": "zh-Hans",
|
||||
|
||||
"flag": "🇨🇳"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -1,68 +1,3 @@
|
||||
import { merge } from "ts-deepmerge";
|
||||
import locales from "../locales.json";
|
||||
import en from "../strings/en.json";
|
||||
import es from "../strings/es.json";
|
||||
import fr from "../strings/fr.json";
|
||||
import ja from "../strings/ja.json";
|
||||
import pl from "../strings/pl.json";
|
||||
import pt from "../strings/pt.json";
|
||||
import ru from "../strings/ru.json";
|
||||
import uk from "../strings/uk.json";
|
||||
import zh from "../strings/zh.json";
|
||||
|
||||
export type BlitzKitStrings = typeof en;
|
||||
|
||||
const stringsRaw: Record<string, TranslationTree> = {
|
||||
en,
|
||||
es,
|
||||
fr,
|
||||
ja,
|
||||
pl,
|
||||
pt,
|
||||
ru,
|
||||
uk,
|
||||
zh,
|
||||
};
|
||||
|
||||
export const STRINGS: Record<string, BlitzKitStrings> = {};
|
||||
|
||||
for (const locale in stringsRaw) {
|
||||
if (locale === locales.default) {
|
||||
STRINGS[locales.default] = stringsRaw[locales.default] as BlitzKitStrings;
|
||||
continue;
|
||||
}
|
||||
|
||||
const localeStrings = stringsRaw[locale];
|
||||
STRINGS[locale] = merge(
|
||||
stringsRaw[locales.default],
|
||||
localeStrings,
|
||||
) as BlitzKitStrings;
|
||||
}
|
||||
|
||||
export const SUPPORTED_LOCALE_BLITZ_MAP: Record<string, string> = {
|
||||
en: "en",
|
||||
es: "es",
|
||||
fr: "fr",
|
||||
ja: "ja",
|
||||
pt: "pt",
|
||||
ru: "ru",
|
||||
uk: "uk",
|
||||
zh: "zh-Hans",
|
||||
pl: "pl",
|
||||
};
|
||||
|
||||
export const SUPPORTED_LOCALE_FLAGS: Record<string, string> = {
|
||||
en: "🇺🇸",
|
||||
es: "🇪🇸",
|
||||
fr: "🇫🇷",
|
||||
ja: "🇯🇵",
|
||||
pt: "🇵🇹",
|
||||
ru: "🇷🇺",
|
||||
uk: "🇺🇦",
|
||||
zh: "🇨🇳",
|
||||
pl: "🇵🇱",
|
||||
};
|
||||
|
||||
export type TranslationTree = { [key: string]: TranslationNode };
|
||||
|
||||
export type TranslationNode = string | TranslationTree;
|
||||
export type Strings = typeof en;
|
||||
@@ -1,4 +1,6 @@
|
||||
import { useProtagonist } from "../../hooks/useProtagonist";
|
||||
import { useStrings } from "../../hooks/useStrings";
|
||||
import { Tankopedia } from "../../stores/tankopedia";
|
||||
import { hasUpgrades } from "../../tankopedia/hasUpgrades";
|
||||
import type { ComputedCharacteristics } from "../../types/characteristics";
|
||||
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
import { STRINGS } from "@blitzkit/i18n";
|
||||
|
||||
export function getStrings(locale: string) {
|
||||
if (locale in STRINGS) return STRINGS[locale];
|
||||
throw new Error(`Unsupported locale: ${locale}`);
|
||||
}
|
||||
@@ -1,11 +1,8 @@
|
||||
import { unwrapper } from "@blitzkit/i18n";
|
||||
import locales from "@blitzkit/i18n/locales.json";
|
||||
import { createContext, useContext, type ReactNode } from "react";
|
||||
import { getStrings } from "../core/i18n/getStrings";
|
||||
|
||||
const LocaleContext = createContext<{
|
||||
locale: string;
|
||||
localeRaw: string | undefined;
|
||||
} | null>(null);
|
||||
|
||||
interface LocaleProviderProps extends LocaleAcceptorProps {
|
||||
@@ -14,9 +11,7 @@ interface LocaleProviderProps extends LocaleAcceptorProps {
|
||||
|
||||
export function LocaleProvider({ locale, children }: LocaleProviderProps) {
|
||||
return (
|
||||
<LocaleContext.Provider
|
||||
value={{ locale: locale ?? locales.default, localeRaw: locale }}
|
||||
>
|
||||
<LocaleContext.Provider value={{ locale: locale ?? locales.default }}>
|
||||
{children}
|
||||
</LocaleContext.Provider>
|
||||
);
|
||||
@@ -29,12 +24,9 @@ export function useLocale() {
|
||||
throw new Error("useLocale must be used within a LocaleProvider");
|
||||
}
|
||||
|
||||
const strings = getStrings(context.locale);
|
||||
const unwrap = unwrapper(context.locale);
|
||||
|
||||
return { locale: context.locale, strings, unwrap };
|
||||
return context.locale;
|
||||
}
|
||||
|
||||
export interface LocaleAcceptorProps {
|
||||
locale: string | undefined;
|
||||
locale: string;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import { api } from "../blitzkit/api";
|
||||
import { useAwait } from "./useAwait";
|
||||
import { useLocale } from "./useLocale";
|
||||
|
||||
export function useStrings() {
|
||||
const locale = useLocale();
|
||||
const strings = useAwait(() => api.strings(locale), `strings-${locale}`);
|
||||
return strings;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import locales from "@blitzkit/i18n/locales.json";
|
||||
import type { APIContext } from "astro";
|
||||
import { mixStaticPaths } from "../../../astro/mixStaticPaths";
|
||||
import { api } from "../../../blitzkit/api";
|
||||
import { getStaticPaths as _getStaticPaths } from "../_index";
|
||||
|
||||
export const getStaticPaths = mixStaticPaths(_getStaticPaths, () => {
|
||||
return locales.supported.map(({ locale }) => ({ params: { locale } }));
|
||||
});
|
||||
|
||||
export async function GET({ params }: APIContext<never, { locale: string }>) {
|
||||
const strings = await api.strings(params.locale);
|
||||
return Response.json(strings);
|
||||
}
|
||||
Reference in new issue
Block a user