mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-23 11:39:50 +02:00
feat: client side game strings
This commit is contained in:
1 parent
2d282a3261
commit
f5f3f70e35
7 files changed
+60
-13
No files matched your search
@@ -83,9 +83,10 @@ export abstract class AbstractAPI {
|
||||
> = {};
|
||||
protected abstract _groupedGameStrings(
|
||||
locale: string,
|
||||
group: string
|
||||
group: string,
|
||||
prefix: boolean
|
||||
): Promise<Record<string, string>>;
|
||||
async groupedGameStrings(locale: string, group: string) {
|
||||
async groupedGameStrings(locale: string, group: string, prefix = true) {
|
||||
this.assertLocale(locale);
|
||||
|
||||
if (this._groupedGameStringsCache[locale] === undefined) {
|
||||
@@ -94,7 +95,7 @@ export abstract class AbstractAPI {
|
||||
|
||||
if (this._groupedGameStringsCache[locale][group] === undefined) {
|
||||
this._groupedGameStringsCache[locale][group] =
|
||||
await this._groupedGameStrings(locale, group);
|
||||
await this._groupedGameStrings(locale, group, prefix);
|
||||
}
|
||||
|
||||
return this._groupedGameStringsCache[locale][group];
|
||||
|
||||
@@ -6,4 +6,27 @@ export class ClientAPI extends AbstractAPI {
|
||||
protected _avatars() {
|
||||
return fetchPB("/api/avatars/all.pb", Avatars);
|
||||
}
|
||||
|
||||
protected _gameStrings(): Promise<Record<string, string>> {
|
||||
throw new Error("Cannot fetch full game strings on client");
|
||||
}
|
||||
|
||||
protected async _groupedGameStrings(
|
||||
locale: string,
|
||||
group: string,
|
||||
prefix: boolean
|
||||
) {
|
||||
const response = await fetch(`/api/game-strings/${locale}/${group}.json`);
|
||||
const json = (await response.json()) as Record<string, string>;
|
||||
|
||||
if (!prefix) return json;
|
||||
|
||||
const strings: Record<string, string> = {};
|
||||
|
||||
for (const key in json) {
|
||||
strings[`${group}__${key}`] = json[key];
|
||||
}
|
||||
|
||||
return strings;
|
||||
}
|
||||
}
|
||||
@@ -101,14 +101,17 @@ export class ServerAPI extends AbstractAPI {
|
||||
return strings;
|
||||
}
|
||||
|
||||
protected async _groupedGameStrings(locale: string, group: string) {
|
||||
protected async _groupedGameStrings(
|
||||
locale: string,
|
||||
group: string,
|
||||
prefix: boolean
|
||||
) {
|
||||
const strings = await this.gameStrings(locale);
|
||||
const filtered: Record<string, string> = {};
|
||||
|
||||
for (const key in strings) {
|
||||
if (key.startsWith(`${group}__`)) {
|
||||
const trimmed = key.substring(group.length + 2);
|
||||
filtered[trimmed] = strings[key];
|
||||
filtered[prefix ? key : key.substring(group.length + 2)] = strings[key];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user