diff options
author | Kristóf Marussy <kristof@marussy.com> | 2022-03-30 21:47:45 +0200 |
---|---|---|
committer | Kristóf Marussy <kristof@marussy.com> | 2022-05-16 00:54:57 +0200 |
commit | 85d91c64b5b3ec31df8acecd68a1fa6a68d57ff9 (patch) | |
tree | 277ab45a66a1c74e2d0a885c8a354aea27128d12 /packages/shared | |
parent | feat(main): Translation hot reloading during development (diff) | |
download | sophie-85d91c64b5b3ec31df8acecd68a1fa6a68d57ff9.tar.gz sophie-85d91c64b5b3ec31df8acecd68a1fa6a68d57ff9.tar.zst sophie-85d91c64b5b3ec31df8acecd68a1fa6a68d57ff9.zip |
feat(renderer): Renderer translations
Add react-i18n to make us able to use i18next translations in the
renderer process just like we do in the main process.
Translations are hot-reloaded automatically.
Signed-off-by: Kristóf Marussy <kristof@marussy.com>
Diffstat (limited to 'packages/shared')
-rw-r--r-- | packages/shared/esbuild.config.js | 2 | ||||
-rw-r--r-- | packages/shared/package.json | 1 | ||||
-rw-r--r-- | packages/shared/src/contextBridge/SophieRenderer.ts | 7 | ||||
-rw-r--r-- | packages/shared/src/index.ts | 4 | ||||
-rw-r--r-- | packages/shared/src/ipc.ts | 2 | ||||
-rw-r--r-- | packages/shared/src/schemas/Action.ts | 7 | ||||
-rw-r--r-- | packages/shared/src/schemas/Translation.ts | 33 | ||||
-rw-r--r-- | packages/shared/src/stores/SharedStoreBase.ts | 1 |
8 files changed, 56 insertions, 1 deletions
diff --git a/packages/shared/esbuild.config.js b/packages/shared/esbuild.config.js index e8dcc2f..6e46742 100644 --- a/packages/shared/esbuild.config.js +++ b/packages/shared/esbuild.config.js | |||
@@ -14,5 +14,5 @@ export default getEsbuildConfig({ | |||
14 | minify: false, | 14 | minify: false, |
15 | platform: 'node', | 15 | platform: 'node', |
16 | target: [chrome, node], | 16 | target: [chrome, node], |
17 | external: ['mobx', 'mobx-state-tree', 'zod'], | 17 | external: ['i18next', 'mobx', 'mobx-state-tree', 'zod'], |
18 | }); | 18 | }); |
diff --git a/packages/shared/package.json b/packages/shared/package.json index 5b075e3..d39d7ee 100644 --- a/packages/shared/package.json +++ b/packages/shared/package.json | |||
@@ -11,6 +11,7 @@ | |||
11 | "types": "yarn g:types" | 11 | "types": "yarn g:types" |
12 | }, | 12 | }, |
13 | "dependencies": { | 13 | "dependencies": { |
14 | "i18next": "^21.6.14", | ||
14 | "mobx": "^6.4.1", | 15 | "mobx": "^6.4.1", |
15 | "mobx-state-tree": "^5.1.3", | 16 | "mobx-state-tree": "^5.1.3", |
16 | "zod": "^3.12.0" | 17 | "zod": "^3.12.0" |
diff --git a/packages/shared/src/contextBridge/SophieRenderer.ts b/packages/shared/src/contextBridge/SophieRenderer.ts index dc77c97..732f941 100644 --- a/packages/shared/src/contextBridge/SophieRenderer.ts +++ b/packages/shared/src/contextBridge/SophieRenderer.ts | |||
@@ -18,11 +18,18 @@ | |||
18 | * SPDX-License-Identifier: AGPL-3.0-only | 18 | * SPDX-License-Identifier: AGPL-3.0-only |
19 | */ | 19 | */ |
20 | 20 | ||
21 | import type { ResourceKey } from 'i18next'; | ||
22 | |||
21 | import { Action } from '../schemas/Action'; | 23 | import { Action } from '../schemas/Action'; |
24 | import { Translation } from '../schemas/Translation'; | ||
22 | import { SharedStoreListener } from '../stores/SharedStoreBase'; | 25 | import { SharedStoreListener } from '../stores/SharedStoreBase'; |
23 | 26 | ||
24 | export default interface SophieRenderer { | 27 | export default interface SophieRenderer { |
25 | onSharedStoreChange(this: void, listener: SharedStoreListener): Promise<void>; | 28 | onSharedStoreChange(this: void, listener: SharedStoreListener): Promise<void>; |
26 | 29 | ||
27 | dispatchAction(this: void, action: Action): void; | 30 | dispatchAction(this: void, action: Action): void; |
31 | |||
32 | getTranslation(this: void, translation: Translation): Promise<ResourceKey>; | ||
33 | |||
34 | onReloadTranslations(this: void, listener: () => void): void; | ||
28 | } | 35 | } |
diff --git a/packages/shared/src/index.ts b/packages/shared/src/index.ts index f7c5bcf..51f9f06 100644 --- a/packages/shared/src/index.ts +++ b/packages/shared/src/index.ts | |||
@@ -18,6 +18,8 @@ | |||
18 | * SPDX-License-Identifier: AGPL-3.0-only | 18 | * SPDX-License-Identifier: AGPL-3.0-only |
19 | */ | 19 | */ |
20 | 20 | ||
21 | export const fallbackLng = ['en']; | ||
22 | |||
21 | export type { default as SophieRenderer } from './contextBridge/SophieRenderer'; | 23 | export type { default as SophieRenderer } from './contextBridge/SophieRenderer'; |
22 | 24 | ||
23 | export { MainToRendererIpcMessage, RendererToMainIpcMessage } from './ipc'; | 25 | export { MainToRendererIpcMessage, RendererToMainIpcMessage } from './ipc'; |
@@ -30,6 +32,8 @@ export { ServiceAction } from './schemas/ServiceAction'; | |||
30 | 32 | ||
31 | export { ThemeSource } from './schemas/ThemeSource'; | 33 | export { ThemeSource } from './schemas/ThemeSource'; |
32 | 34 | ||
35 | export { Translation } from './schemas/Translation'; | ||
36 | |||
33 | export type { CertificateSnapshotIn } from './stores/Certificate'; | 37 | export type { CertificateSnapshotIn } from './stores/Certificate'; |
34 | export { default as Certificate } from './stores/Certificate'; | 38 | export { default as Certificate } from './stores/Certificate'; |
35 | 39 | ||
diff --git a/packages/shared/src/ipc.ts b/packages/shared/src/ipc.ts index 54d761a..2716633 100644 --- a/packages/shared/src/ipc.ts +++ b/packages/shared/src/ipc.ts | |||
@@ -20,9 +20,11 @@ | |||
20 | 20 | ||
21 | export enum MainToRendererIpcMessage { | 21 | export enum MainToRendererIpcMessage { |
22 | SharedStorePatch = 'sophie-main-to-renderer:shared-store-patch', | 22 | SharedStorePatch = 'sophie-main-to-renderer:shared-store-patch', |
23 | ReloadTranslations = 'sophie-main-to-renderer:reload-translations', | ||
23 | } | 24 | } |
24 | 25 | ||
25 | export enum RendererToMainIpcMessage { | 26 | export enum RendererToMainIpcMessage { |
26 | GetSharedStoreSnapshot = 'sophie-renderer-to-main:get-shared-store-snapshot', | 27 | GetSharedStoreSnapshot = 'sophie-renderer-to-main:get-shared-store-snapshot', |
28 | GetTranslation = 'sophie-renderer-to-main:get-translation', | ||
27 | DispatchAction = 'sophie-renderer-to-main:dispatch-action', | 29 | DispatchAction = 'sophie-renderer-to-main:dispatch-action', |
28 | } | 30 | } |
diff --git a/packages/shared/src/schemas/Action.ts b/packages/shared/src/schemas/Action.ts index 8d6ca3a..ce983fa 100644 --- a/packages/shared/src/schemas/Action.ts +++ b/packages/shared/src/schemas/Action.ts | |||
@@ -49,6 +49,13 @@ export const Action = /* @__PURE__ */ (() => | |||
49 | action: z.literal('reload-all-translations'), | 49 | action: z.literal('reload-all-translations'), |
50 | }), | 50 | }), |
51 | z.object({ | 51 | z.object({ |
52 | action: z.literal('add-missing-translation'), | ||
53 | languages: z.string().nonempty().array(), | ||
54 | namespace: z.string().nonempty(), | ||
55 | key: z.string().nonempty(), | ||
56 | value: z.string(), | ||
57 | }), | ||
58 | z.object({ | ||
52 | action: z.literal('dispatch-service-action'), | 59 | action: z.literal('dispatch-service-action'), |
53 | serviceId: z.string(), | 60 | serviceId: z.string(), |
54 | serviceAction: ServiceAction, | 61 | serviceAction: ServiceAction, |
diff --git a/packages/shared/src/schemas/Translation.ts b/packages/shared/src/schemas/Translation.ts new file mode 100644 index 0000000..ab513b5 --- /dev/null +++ b/packages/shared/src/schemas/Translation.ts | |||
@@ -0,0 +1,33 @@ | |||
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 { z } from 'zod'; | ||
22 | |||
23 | export const Translation = /* @__PURE__ */ (() => | ||
24 | z.object({ | ||
25 | language: z.string().nonempty(), | ||
26 | namespace: z.string().nonempty(), | ||
27 | }))(); | ||
28 | |||
29 | /* | ||
30 | eslint-disable-next-line @typescript-eslint/no-redeclare -- | ||
31 | Intentionally naming the type the same as the schema definition. | ||
32 | */ | ||
33 | export type Translation = z.infer<typeof Translation>; | ||
diff --git a/packages/shared/src/stores/SharedStoreBase.ts b/packages/shared/src/stores/SharedStoreBase.ts index 8d6624b..86bd0fc 100644 --- a/packages/shared/src/stores/SharedStoreBase.ts +++ b/packages/shared/src/stores/SharedStoreBase.ts | |||
@@ -43,6 +43,7 @@ export function defineSharedStoreModel< | |||
43 | servicesById: types.map(service), | 43 | servicesById: types.map(service), |
44 | services: types.array(types.reference(service)), | 44 | services: types.array(types.reference(service)), |
45 | shouldUseDarkColors: false, | 45 | shouldUseDarkColors: false, |
46 | language: 'en', | ||
46 | }); | 47 | }); |
47 | } | 48 | } |
48 | 49 | ||