modify helper files to support launcher

This commit is contained in:
llama 2025-10-14 09:20:23 +08:00
parent 8f6b1497e0
commit 581618610e
No known key found for this signature in database
GPG key ID: 0B7543854B9413C3
2 changed files with 10 additions and 4 deletions

View file

@ -4,21 +4,25 @@
export interface PostProtoOptions {
/** True by default. Shows a dialog with the error message, then rethrows. */
alertOnError?: boolean;
customProtocol?: boolean;
}
const CUSTOM_PROTOCOL_URI = (navigator.platform.indexOf("Win") == -1) ? "anki://localhost" : "http://anki.localhost";
export async function postProto<T>(
method: string,
input: { toBinary(): Uint8Array; getType(): { typeName: string } },
outputType: { fromBinary(arr: Uint8Array): T },
options: PostProtoOptions = {},
): Promise<T> {
const { alertOnError = true, customProtocol = false } = options;
try {
const inputBytes = input.toBinary();
const path = `/_anki/${method}`;
const backendUrl = customProtocol ? CUSTOM_PROTOCOL_URI : "/_anki";
const path = `${backendUrl}/${method}`;
const outputBytes = await postProtoInner(path, inputBytes);
return outputType.fromBinary(outputBytes);
} catch (err) {
const { alertOnError = true } = options;
if (alertOnError && !(err instanceof Error && err.message === "500: Interrupted")) {
alert(err);
}

View file

@ -4,6 +4,7 @@
import "intl-pluralrules";
import { i18nResources } from "@generated/backend";
import { i18nResources as launcherI18nResources } from "@generated/backend-launcher";
import type { ModuleName } from "@generated/ftl";
import { FluentBundle, FluentResource } from "@generated/ftl";
import { firstLanguage, setBundles } from "@generated/ftl";
@ -77,8 +78,9 @@ export function withoutUnicodeIsolation(s: string): string {
return s.replace(/[\u2068-\u2069]+/g, "");
}
export async function setupI18n(args: { modules: ModuleName[] }): Promise<void> {
const resources = await i18nResources(args);
export async function setupI18n(args: { modules: ModuleName[] }, launcher = false): Promise<void> {
const fn = launcher ? launcherI18nResources : i18nResources;
const resources = await fn(args);
const json = JSON.parse(new TextDecoder().decode(resources.json));
const newBundles: FluentBundle[] = [];