Skip to content

Commit

Permalink
Add Basic Translationservice
Browse files Browse the repository at this point in the history
  • Loading branch information
undyingwraith committed Dec 11, 2024
1 parent 5871a5c commit 2481a22
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 6 deletions.
4 changes: 3 additions & 1 deletion packages/core/src/Modules/CoreModule.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { IIndexManagerSymbol, IKeyValueStoreSymbol, IObjectStoreSymbol } from 'ipmc-interfaces';
import { IIndexManagerSymbol, IKeyValueStoreSymbol, IObjectStoreSymbol, ITranslationServiceSymbol } from 'ipmc-interfaces';
import { MemoryKeyValueStore } from '../Services/MemoryKeyValueStore';
import { ObjectStore } from '../Services/ObjectStore';
import { IModule } from './IModule';
import { IndexManager } from '../Services/IndexManager';
import { TaskManager } from '../Services/TaskManager';
import { ITaskManagerSymbol } from 'ipmc-interfaces';
import { TranslationService } from '../Services/TranslationService';

export const CoreModule: IModule = (app) => {
app.register(MemoryKeyValueStore, IKeyValueStoreSymbol);
app.register(ObjectStore, IObjectStoreSymbol);
app.register(IndexManager, IIndexManagerSymbol);
app.register(TaskManager, ITaskManagerSymbol);
app.register(TranslationService, ITranslationServiceSymbol);
};
7 changes: 4 additions & 3 deletions packages/core/src/Services/IndexManager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Signal } from '@preact/signals-core';
import { inject, injectable, postConstruct, preDestroy } from 'inversify';
import { IIndexManager, IIpfsService, IIpfsServiceSymbol, ILibrary, ILibraryIndex, IObjectStore, IObjectStoreSymbol, IProfile, IProfileSymbol, ITask, isMovieLibrary, isSeriesLibrary } from 'ipmc-interfaces';
import { IIndexManager, IIpfsService, IIpfsServiceSymbol, ILibrary, ILibraryIndex, IObjectStore, IObjectStoreSymbol, IProfile, IProfileSymbol, ITask, ITranslationService, ITranslationServiceSymbol, isMovieLibrary, isSeriesLibrary } from 'ipmc-interfaces';
import { MovieIndexFetcher, SeriesIndexFetcher } from './Indexer';
import { ITaskManager, ITaskManagerSymbol } from 'ipmc-interfaces';

Expand All @@ -10,7 +10,8 @@ export class IndexManager implements IIndexManager {
@inject(IProfileSymbol) private readonly profile: IProfile,
@inject(IIpfsServiceSymbol) private readonly ipfs: IIpfsService,
@inject(IObjectStoreSymbol) private readonly objectStore: IObjectStore,
@inject(ITaskManagerSymbol) private readonly taskManager: ITaskManager
@inject(ITaskManagerSymbol) private readonly taskManager: ITaskManager,
@inject(ITranslationServiceSymbol) private readonly translationService: ITranslationService,
) {
for (const lib of this.profile.libraries) {
this.libraries.set(lib.name, new Signal<ILibrary>(lib));
Expand Down Expand Up @@ -51,7 +52,7 @@ export class IndexManager implements IIndexManager {
if (!this.updates.has(lib.name)) {
this.taskManager.runTask({
task: () => this.updateLibrary(lib),
title: '',
title: this.translationService.translate('UpdatingLibrary', { name: lib.name }),
onEnd: () => {
this.updates.delete(lib.name);
},
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/Services/TaskManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ export class TaskManager implements ITaskManager {
title: task.title,
};
this.status.value = [...this.status.value, status];
task.task()
task.task((progress) => {
this.status.value = this.status.value.map(t => t.id === status.id ? ({ ...t, progress }) : t);
})
.finally(() => {
this.status.value = this.status.value.filter(s => s.id !== status.id);
});
Expand Down
24 changes: 24 additions & 0 deletions packages/core/src/Services/TranslationService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import i18next from 'i18next';
import { inject, injectable } from 'inversify';
import { ITranslationService, ITranslationsSymbol } from 'ipmc-interfaces';

@injectable()
export class TranslationService implements ITranslationService {
constructor(@inject(ITranslationsSymbol) translations: any) {
i18next.init({
resources: translations,
lng: "en", // language to use, more information here: https://www.i18next.com/overview/configuration-options#languages-namespaces-resources
interpolation: {
escapeValue: false // react already safes from xss
}
});
}

translate(key: string, values?: { [key: string]: string; }): string {
if (i18next.exists(key)) {
return i18next.t(key, values);
} else {
return `<${key}>`;
}
}
}
2 changes: 1 addition & 1 deletion packages/interfaces/src/Services/ITaskManager/ITask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export interface ITask {
/**
* The task to run.
*/
task: () => Promise<void>;
task: (onProgress: (progress: number) => void) => Promise<void>;

/**
* Start event handler.
Expand Down
7 changes: 7 additions & 0 deletions packages/interfaces/src/Services/ITranslationService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const ITranslationServiceSymbol = Symbol.for('ITranslationService');

export const ITranslationsSymbol = Symbol.for('ITranslations');

export interface ITranslationService {
translate(key: string, values?: { [key: string]: string; }): string;
}
1 change: 1 addition & 0 deletions packages/interfaces/src/Services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export { type IIpfsService, IIpfsServiceSymbol } from './IIpfsService';
export { type IKeyValueStore, IKeyValueStoreSymbol } from './IKeyValueStore';
export { type INodeService } from './INodeService';
export { type IObjectStore, IObjectStoreSymbol } from './IObjectStore';
export { type ITranslationService, ITranslationServiceSymbol, ITranslationsSymbol } from './ITranslationService';
15 changes: 15 additions & 0 deletions packages/ui/src/IpmcApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,33 @@ import { AppContextProvider } from './context';

// Setup translations
import './i18n';
import { ITranslationsSymbol } from 'ipmc-interfaces';

export interface IIpmcAppProps {
setup?: IModule;
}

// Translations
import en from './translations/en.json';
import de from './translations/de.json';

const resources = {
en: {
translation: en
},
de: {
translation: de
}
};

export function IpmcApp(props: IIpmcAppProps) {
const { setup } = props;

return (
<AppContextProvider setup={(app) => {
app.use(CoreModule);
app.use(BrowserModule);
app.registerConstant<any>(resources, ITranslationsSymbol);
setup && app.use(setup);
}}>
<AppBar />
Expand Down

0 comments on commit 2481a22

Please sign in to comment.