Skip to content

Commit fdbb562

Browse files
committed
feat: add support for custom translation functions
1 parent 96cb930 commit fdbb562

20 files changed

+65
-45
lines changed

_build/js/src/executor.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { globalState } from './ui/localChat/state';
1+
import { globalState } from './globalState';
2+
import { lng } from './lng';
23

34
type ServiceType = 'chatgpt' | 'claude' | 'gemini';
45
type BufferMode = 'buffered' | 'stream';
@@ -176,7 +177,7 @@ const services: ServiceHandlers = {
176177
const content = data?.choices?.[0]?.message?.content;
177178

178179
if (!content) {
179-
throw new Error(_('modai.cmp.failed_request'));
180+
throw new Error(lng('modai.cmp.failed_request'));
180181
}
181182

182183
const id = data.id;
@@ -193,7 +194,7 @@ const services: ServiceHandlers = {
193194
url = data?.data?.[0]?.b64_json;
194195

195196
if (!url) {
196-
throw new Error(_('modai.cmp.failed_request'));
197+
throw new Error(lng('modai.cmp.failed_request'));
197198
}
198199

199200
url = `data:image/png;base64,${url}`;
@@ -210,7 +211,7 @@ const services: ServiceHandlers = {
210211
const content = data?.content?.[0]?.text;
211212

212213
if (!content) {
213-
throw new Error(_('modai.cmp.failed_request'));
214+
throw new Error(lng('modai.cmp.failed_request'));
214215
}
215216

216217
const id = data.id;
@@ -226,7 +227,7 @@ const services: ServiceHandlers = {
226227
const content = data?.candidates?.[0]?.content?.parts?.[0]?.text;
227228

228229
if (!content) {
229-
throw new Error(_('modai.cmp.failed_request'));
230+
throw new Error(lng('modai.cmp.failed_request'));
230231
}
231232

232233
return {
@@ -238,7 +239,7 @@ const services: ServiceHandlers = {
238239
const base64 = data?.predictions?.[0]?.bytesBase64Encoded;
239240

240241
if (!base64) {
241-
throw new Error(_('modai.cmp.failed_request'));
242+
throw new Error(lng('modai.cmp.failed_request'));
242243
}
243244

244245
return {
@@ -455,7 +456,7 @@ const serviceExecutor = async <D extends ServiceResponse>(
455456

456457
const callStreamService = async (details: ForExecutor) => {
457458
if (executorDetails.parser !== 'content') {
458-
throw new Error(_('modai.cmp.service_unsupported'));
459+
throw new Error(lng('modai.cmp.service_unsupported'));
459460
}
460461

461462
const res = await fetch(details.url, {
@@ -476,15 +477,15 @@ const serviceExecutor = async <D extends ServiceResponse>(
476477
};
477478

478479
if (!executorDetails.service || !executorDetails.parser) {
479-
throw new Error(_('modai.cmp.service_required'));
480+
throw new Error(lng('modai.cmp.service_required'));
480481
}
481482

482483
if (
483484
!services[executorDetails.stream ? 'stream' : ('buffered' as BufferMode)]?.[
484485
executorDetails.service as ServiceType
485486
]?.[executorDetails.parser as keyof ServiceHandlers[BufferMode][ServiceType]]
486487
) {
487-
throw new Error(_('modai.cmp.service_unsupported'));
488+
throw new Error(lng('modai.cmp.service_unsupported'));
488489
}
489490

490491
if (executorDetails.stream) {
@@ -557,7 +558,7 @@ const aiFetch = async <D extends ServiceResponse>(
557558

558559
if (!service || !parser) {
559560
controller.abort();
560-
throw new Error(_('modai.cmp.service_required'));
561+
throw new Error(lng('modai.cmp.service_required'));
561562
}
562563

563564
if (
@@ -566,7 +567,7 @@ const aiFetch = async <D extends ServiceResponse>(
566567
]
567568
) {
568569
controller.abort();
569-
throw new Error(_('modai.cmp.service_unsupported'));
570+
throw new Error(lng('modai.cmp.service_unsupported'));
570571
}
571572

572573
if (!stream) {
@@ -577,7 +578,7 @@ const aiFetch = async <D extends ServiceResponse>(
577578
}
578579

579580
if (parser !== 'content') {
580-
throw new Error(_('modai.cmp.service_unsupported'));
581+
throw new Error(lng('modai.cmp.service_unsupported'));
581582
}
582583

583584
return (await handleStream(

_build/js/src/global.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
declare function _(key: string, params?: Record<string, string>): string;
21
declare const modAI: {
32
apiURL: string;
43
resourceFields?: string[];

_build/js/src/globalState.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import type { Config } from './index';
2+
import type { Modal } from './ui/localChat/types';
3+
4+
export const globalState = {
5+
modalOpen: false,
6+
alertOpen: false,
7+
config: {} as Config,
8+
modal: {} as Modal,
9+
};

_build/js/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import { chatHistory } from './chatHistory';
22
import { executor } from './executor';
3+
import { globalState } from './globalState';
34
import { history } from './history';
45
import { initOnResource } from './resource';
56
import { ui } from './ui';
6-
import { globalState } from './ui/localChat/state';
77

88
export type Config = {
99
name?: string;
1010
apiURL: string;
1111
cssURL: string;
12+
translateFn?: (key: string, params?: Record<string, string>) => string;
1213
};
1314

1415
export const init = (config: Config) => {

_build/js/src/lng.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { globalState } from './globalState';
2+
3+
export type Lng = (key: string, params?: Record<string, string>) => string;
4+
5+
// Declare _ locally to avoid using it in other files
6+
declare function _(key: string, params?: Record<string, string>): string;
7+
8+
export const lng: Lng = (key, params) => {
9+
return (globalState.config.translateFn || _)(key, params);
10+
};

_build/js/src/ui/dom/modAIShadow.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { globalState } from '../localChat/state';
1+
import { globalState } from '../../globalState';
22
import { createElement } from '../utils';
33

44
export const createModAIShadow = <R extends HTMLElement>(

_build/js/src/ui/generateButton/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { executor, TextData, TextParams } from '../../executor';
22
import { DataOutput, history } from '../../history';
3+
import { lng } from '../../lng';
34
import { confirmDialog } from '../cofirmDialog';
45
import { button } from '../dom/button';
56
import { icon } from '../dom/icon';
@@ -153,7 +154,9 @@ const createForcedTextPrompt = ({
153154
done();
154155
confirmDialog({
155156
title: 'Failed',
156-
content: _('modai.cmp.failed_try_again', { msg: err instanceof Error ? err.message : '' }),
157+
content: lng('modai.cmp.failed_try_again', {
158+
msg: err instanceof Error ? err.message : '',
159+
}),
157160
confirmText: 'Close',
158161
showCancel: false,
159162
onConfirm: () => {},
@@ -258,7 +261,9 @@ const createVisionPrompt = (config: VisionConfig & Target) => {
258261
done();
259262
confirmDialog({
260263
title: 'Failed',
261-
content: _('modai.cmp.failed_try_again', { msg: err instanceof Error ? err.message : '' }),
264+
content: lng('modai.cmp.failed_try_again', {
265+
msg: err instanceof Error ? err.message : '',
266+
}),
262267
confirmText: 'Close',
263268
showCancel: false,
264269
onConfirm: () => {},

_build/js/src/ui/localChat/actionButton.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
import { globalState } from '../../globalState';
12
import { button } from '../dom/button';
23
import { check } from '../icons';
34
import { createElement } from '../utils';
4-
import { globalState } from './state';
55

66
import type { Modal } from './types';
77
import type { Message } from '../../chatHistory';

_build/js/src/ui/localChat/dragHandlers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { globalState } from './state';
1+
import { globalState } from '../../globalState';
22

33
export const initDrag = (e: MouseEvent) => {
44
globalState.modal.isDragging = true;

_build/js/src/ui/localChat/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { closeModal, sendMessage } from './modalActions';
22
import { buildModal } from './modalBuilder';
3-
import { globalState } from './state';
3+
import { globalState } from '../../globalState';
44

55
import type { LocalChatConfig } from './types';
66

0 commit comments

Comments
 (0)