-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GDB-10443 Implement i18n in the new workbench (#1485)
* Move the services to a separate directory. * Implements Service Provider What Implemented a service provider. Why The new service provider is responsible for managing shared service instances across microfrontends. Frameworks like Angular and React provide dependency injection, allowing a single instance to be injected into all components. In a single-spa application, such functionality does not exist by default. The service provider addresses this need. How The service provider includes lazy loading functionality that returns an instance of an already instantiated service. If the service is not present, a new one is created and stored in a static map. The static modifier ensures that only one instance of each service class is used across the microfrontends. * GDB-10443: Implement i18n in the New Workbench ## What - Implemented core functionality for emitting events across all microfrontends. - Implemented global-context.service.ts. - Implemented language.service.ts. ## Why - Microfrontends are separate modules and should not be aware of each other. The API is a shared module across all of them. The event emitting functionality enables communication between these modules. - The global context will maintain shared state between all microfrontends. - The service will manage language functionality. ## How - Utilized the browser's built-in custom event propagation. Events are emitted from the document body, ensuring that all microfrontends can listen to them. - The global state is designed to be extendable with additional data as needed. Currently, it includes a language field that holds the user's selected language. This can be accessed through the onLanguageChanged function, which returns a Subject<string> and emits a value every time the language changes. - The language service manages language-related functionality. All microfrontends can use it to fetch and update the language. * GDB-10443: Implement i18n in the New Workbench ## What - Implemented core functionality for emitting events across all microfrontends; - Implemented global-context.service.ts; - Implemented language.service.ts; - Implemented translated label component in shared-components. ## Why - Microfrontends are separate modules and should not be aware of each other. The API is a shared module across all of them. The event emitting functionality enables communication between these modules; - The global context will maintain shared state between all microfrontends; - The service will manage language functionality; - To translate labels when the language is changed. ## How - Utilized the browser's built-in custom event propagation. Events are emitted from the document body, ensuring that all microfrontends can listen to them; - The global state is designed to be extendable with additional data as needed. Currently, it includes a language field that holds the user's selected language. This can be accessed through the onLanguageChanged function, which returns a Subject<string> and emits a value every time the language changes; - The language service manages language-related functionality. All microfrontends can use it to fetch and update the language. * GDB-10443: Implement i18n in the Legacy Workbench What Integrated the translation functionality of the legacy workbench with the new language change implementation. Why The language selection functionality has been moved to the shared-component microfrontend, centralizing language changes across the application. How Added a listener for the language change event. When the event is triggered, the new language value is set in the translation service, which triggers the re-translating the entire workbench. * Removes the Workbench prefix. * Remove usage of global context * Add some missing documentation and changed the event emitter to use the document.body as a source for the emitted events instead of just `div` --------- Co-authored-by: svilen.velikov <[email protected]>
- Loading branch information
1 parent
1cdc543
commit 026f047
Showing
36 changed files
with
652 additions
and
36 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* A generic shape of our internal event data payloads. | ||
*/ | ||
export class Event { | ||
/** | ||
* The name of the event. | ||
*/ | ||
readonly NAME; | ||
/** | ||
* The payload of the event. | ||
*/ | ||
readonly payload; | ||
/** | ||
* Creates a new instance of the event. | ||
* | ||
* @param name - the name of the event. | ||
* @param payload - the payload of the event. This is optional and if omitted, the event will have no payload, | ||
* just a name. | ||
*/ | ||
constructor(name: string, payload?: any) { | ||
this.NAME = name; | ||
this.payload = payload; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
// Anything exported from this file is importable by other in-browser modules. | ||
export {AuthenticationService} from './authentication-service'; | ||
export {RepositoryService} from './repository-service'; | ||
export {AuthenticationService} from './services/authentication.service'; | ||
export {RepositoryService} from './services/repository.service'; | ||
export {ServiceProvider} from './service.provider' | ||
export {EventService} from './services/event.service'; | ||
export {LanguageService} from './services/language.service'; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import {Service} from './services/service'; | ||
|
||
/** | ||
* Service provider for all {@link Service} instances. | ||
* This provider caches all workbench services created on demand, ensuring that all micro frontends share a single instance of each service. | ||
*/ | ||
export class ServiceProvider { | ||
|
||
/** | ||
* The static modifier ensures the map is the same for all ServiceProviders. Each micro-frontend will have its | ||
* own instance of {@see ServiceFactoryService}, but the map with instances will be shared. | ||
* | ||
* @private | ||
*/ | ||
private static readonly SERVICE_INSTANCES = new Map<string, Service> | ||
|
||
public static get<T extends Service>(type: { new(service: Service): T; }): T { | ||
if (!ServiceProvider.SERVICE_INSTANCES.has(type.name)) { | ||
ServiceProvider.SERVICE_INSTANCES.set(type.name, new type(this)); | ||
} | ||
return this.SERVICE_INSTANCES.get(type.name) as T; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import {Service} from './service'; | ||
|
||
export class AuthenticationService implements Service { | ||
login(): string { | ||
return "Athentication.login from the API"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import {Event} from '../models/events/event'; | ||
import {Service} from './service'; | ||
|
||
/** | ||
* Service used for global communication within all modules. It allows emitting and subscribing to CustomEvents across | ||
* the application where the events are emitted via the <code>document.body</code> element. This allows for the events | ||
* to be caught by any component in any module. | ||
*/ | ||
export class EventService implements Service { | ||
/** | ||
* Emits a {@link CustomEvent} of type passed <code>event.NAME</code> and detail <code>event.payload</code>. | ||
* | ||
* @param event - the event to be emitted. | ||
* @return the emitted event. | ||
*/ | ||
emit(event: Event): CustomEvent { | ||
const customEvent = new CustomEvent(event.NAME, {detail: event.payload}); | ||
this.getHostElement().dispatchEvent(customEvent); | ||
return customEvent; | ||
} | ||
|
||
/** | ||
* Subscribes for event of type <code>eventName</code>. | ||
* | ||
* @param eventName - type of subscription event. | ||
* @param callback - callback function that will be called when the event occurred. | ||
* | ||
* @return unsubscribe function which can be used for manual unsubscription. | ||
*/ | ||
subscribe(eventName: string, callback: (payload: any) => void): () => void { | ||
const listener = (event) => { | ||
if (event instanceof CustomEvent) { | ||
callback(event.detail); | ||
} | ||
}; | ||
this.getHostElement().addEventListener(eventName, listener); | ||
|
||
return () => this.getHostElement().removeEventListener(eventName, listener); | ||
} | ||
|
||
private getHostElement(): HTMLElement { | ||
return document.body; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import {Service} from './service'; | ||
import {ReplaySubject, Subject} from '@reactivex/rxjs/dist/package'; | ||
|
||
/** | ||
* The LanguageService class manages the application's language settings. | ||
*/ | ||
export class LanguageService implements Service { | ||
|
||
static readonly DEFAULT_LANGUAGE = 'en'; | ||
private readonly selectedLanguage = new ReplaySubject<string>(1); | ||
|
||
/** | ||
* Constructs a new LanguageService instance. | ||
* Reads the initial language setting from local storage (not yet implemented) | ||
* and sets it as the first value of the ReplaySubject. | ||
*/ | ||
constructor() { | ||
// TODO read it from local store and pass it as first value | ||
} | ||
|
||
/** | ||
* Changes the current language of the application. | ||
* This method updates the language setting and notifies all subscribers | ||
* about the language change. The new language is also intended to be saved | ||
* to local storage (not yet implemented). | ||
* | ||
* @param {string} locale - The new language code to set (e.g., 'en', 'fr', 'de'). | ||
*/ | ||
changeLanguage(locale: string): void { | ||
// TODO save it to local store. | ||
this.selectedLanguage.next(locale); | ||
} | ||
|
||
/** | ||
* Returns an observable that emits the current language whenever it changes. | ||
* Subscribers to this observable will receive updates whenever the language | ||
* is changed using the `changeLanguage` method. | ||
* | ||
* @returns {Subject<string>} An observable stream of language changes. | ||
*/ | ||
onLanguageChanged(): Subject<string> { | ||
return this.selectedLanguage; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import {Service} from './service'; | ||
|
||
export class RepositoryService implements Service { | ||
getRepositories(): Promise<Response> { | ||
return fetch("http://localhost:9000/rest/repositories/all"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/** | ||
* Interface for identifying a service as part of the workbench system. | ||
*/ | ||
export interface Service {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,3 +24,4 @@ $RECYCLE.BIN/ | |
Thumbs.db | ||
UserInterfaceState.xcuserstate | ||
.env | ||
api/ |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes
File renamed without changes
Oops, something went wrong.