diff options
Diffstat (limited to 'packages/preload/src/contextBridge/TranslationsConnector.ts')
-rw-r--r-- | packages/preload/src/contextBridge/TranslationsConnector.ts | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/packages/preload/src/contextBridge/TranslationsConnector.ts b/packages/preload/src/contextBridge/TranslationsConnector.ts new file mode 100644 index 0000000..284b793 --- /dev/null +++ b/packages/preload/src/contextBridge/TranslationsConnector.ts | |||
@@ -0,0 +1,71 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2022 Kristóf Marussy <kristof@marussy.com> | ||
3 | * | ||
4 | * This file is part of Sophie. | ||
5 | * | ||
6 | * Sophie is free software: you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU Affero General Public License as | ||
8 | * published by the Free Software Foundation, version 3. | ||
9 | * | ||
10 | * This program is distributed in the hope that it will be useful, | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | * GNU Affero General Public License for more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU Affero General Public License | ||
16 | * along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
17 | * | ||
18 | * SPDX-License-Identifier: AGPL-3.0-only | ||
19 | */ | ||
20 | |||
21 | import { | ||
22 | MainToRendererIpcMessage, | ||
23 | RendererToMainIpcMessage, | ||
24 | Translation, | ||
25 | } from '@sophie/shared'; | ||
26 | import { ipcRenderer } from 'electron'; | ||
27 | import type { ResourceKey } from 'i18next'; | ||
28 | import log from 'loglevel'; | ||
29 | |||
30 | export default class TranslationsConnector { | ||
31 | private listener: (() => void) | undefined; | ||
32 | |||
33 | constructor(private readonly devMode: boolean) { | ||
34 | ipcRenderer.on(MainToRendererIpcMessage.ReloadTranslations, () => { | ||
35 | try { | ||
36 | this.listener?.(); | ||
37 | } catch (error) { | ||
38 | log.error('Translations listener onReloadTranslations failed', error); | ||
39 | this.listener = undefined; | ||
40 | } | ||
41 | }); | ||
42 | } | ||
43 | |||
44 | onReloadTranslations(listener: () => void): void { | ||
45 | if (!this.devMode) { | ||
46 | throw new Error( | ||
47 | 'Translation reloading is only supported in development mode', | ||
48 | ); | ||
49 | } | ||
50 | this.listener = listener; | ||
51 | } | ||
52 | } | ||
53 | |||
54 | export async function getTranslation( | ||
55 | translation: Translation, | ||
56 | ): Promise<ResourceKey> { | ||
57 | const parsedTranslation = Translation.parse(translation); | ||
58 | try { | ||
59 | // We don't have any way to validate translations, | ||
60 | // but they are coming from a trusted source and will be validated | ||
61 | // in the renderer anyways, so we should be fine. | ||
62 | return (await ipcRenderer.invoke( | ||
63 | RendererToMainIpcMessage.GetTranslation, | ||
64 | parsedTranslation, | ||
65 | )) as ResourceKey; | ||
66 | } catch (error) { | ||
67 | const message = 'Failed to get translation'; | ||
68 | log.error(message, translation, error); | ||
69 | throw new Error(message); | ||
70 | } | ||
71 | } | ||