Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

refactor(i18n): one home for translating toasts fired outside React

EtienneLescotc7c0ca526b97
diff --git a/src/i18n/toastText.ts b/src/i18n/toastText.ts
new file mode 100644
index 000000000..9b20d57e9
--- /dev/null
+++ b/src/i18n/toastText.ts
@@ -0,0 +1,25 @@
+// Translation for toasts fired OUTSIDE React -- stores, and anything else that reports
+// to the user without a component around it to call `useScopedT`.
+
+import { DEFAULT_LOCALE, type I18nNamespace, LOCALE_STORAGE_KEY, type Locale } from "./config";
+import { getAvailableLocales, translate } from "./loader";
+
+/**
+ * Toasts fired outside React still have to speak the user's language. Same source as
+ * `I18nProvider` (stored preference, else the default), validated so a stale value
+ * cannot push `translate` onto a locale it does not have.
+ */
+export function toastText(
+ namespace: I18nNamespace,
+ key: string,
+ vars?: Record<string, string | number>,
+): string {
+ let locale: Locale = DEFAULT_LOCALE;
+ try {
+ const stored = localStorage.getItem(LOCALE_STORAGE_KEY);
+ if (stored && getAvailableLocales().includes(stored as Locale)) locale = stored as Locale;
+ } catch {
+ // localStorage may be unavailable -- the default locale is a fine answer.
+ }
+ return translate(locale, namespace, key, vars);
+}
diff --git a/src/lib/ai-edition/store/transcriptionStore.ts b/src/lib/ai-edition/store/transcriptionStore.ts
index 1cee92d11..3c3c1139f 100644
--- a/src/lib/ai-edition/store/transcriptionStore.ts
+++ b/src/lib/ai-edition/store/transcriptionStore.ts
@@ -25,8 +25,7 @@
import { useEffect, useMemo } from "react";
import { toast } from "sonner";
import { create } from "zustand";
-import { DEFAULT_LOCALE, LOCALE_STORAGE_KEY, type Locale } from "@/i18n/config";
-import { getAvailableLocales, translate } from "@/i18n/loader";
+import { toastText as translateToast } from "@/i18n/toastText";
import { transcribeAsset, withTranscript } from "../document/transcribe";
import type { AxcutDocument } from "../schema";
import {
@@ -85,21 +84,9 @@ function hasLocalSttEngine(): boolean {
return typeof window.electronAPI?.stt?.transcribe === "function";
}
-/**
- * Toasts fired outside React still have to speak the user's language. Same
- * source as `I18nProvider` (stored preference, else the default), validated so
- * a stale value can't push `translate` onto a locale it doesn't have.
- */
-function toastText(key: string, vars?: Record<string, string | number>): string {
- let locale: Locale = DEFAULT_LOCALE;
- try {
- const stored = localStorage.getItem(LOCALE_STORAGE_KEY);
- if (stored && getAvailableLocales().includes(stored as Locale)) locale = stored as Locale;
- } catch {
- // localStorage may be unavailable — the default locale is a fine answer.
- }
- return translate(locale, "editor", key, vars);
-}
+/** This store's toasts all live in the `editor` namespace. */
+const toastText = (key: string, vars?: Record<string, string | number>) =>
+ translateToast("editor", key, vars);
export const useTranscriptionStore = create<TranscriptionState>((set, get) => ({
projectId: null,
@@ -321,24 +308,26 @@ async function persistPermanentFailure(
const doc = project.document;
if (!doc || doc.project.id !== projectId) return;
if (!doc.assets.some((a) => a.id === assetId)) return;
- try {
- await project.saveDocument({
- ...doc,
- assets: doc.assets.map((a) =>
- a.id === assetId
- ? {
- ...a,
- transcriptionFailure: {
- kind,
- message: failure.message,
- at: new Date().toISOString(),
- },
- }
- : a,
- ),
- });
- } catch (error) {
- console.warn("[transcription] could not persist the failure on the asset:", error);
+ // Best-effort bookkeeping: `saveDocument` reports its own failures and resolves
+ // false rather than throwing, and a note on the asset is not worth a second
+ // message on top of the one the user already got.
+ const persisted = await project.saveDocument({
+ ...doc,
+ assets: doc.assets.map((a) =>
+ a.id === assetId
+ ? {
+ ...a,
+ transcriptionFailure: {
+ kind,
+ message: failure.message,
+ at: new Date().toISOString(),
+ },
+ }
+ : a,
+ ),
+ });
+ if (!persisted) {
+ console.warn("[transcription] could not persist the failure on the asset");
}
}