147 lines
4.0 KiB
TypeScript

/**
* Klasse welche Methoden zur Verfügung stellt welche lokalisierte Texte anbieten
*/
export class LocalizationManager {
private static _instance: LocalizationManager;
private client: any;
private text: any;
private annotations: any;
private initDone: boolean;
/**
* Erstellt eine Instanz
*/
private constructor() {
this.client = {};
this.text = {};
this.annotations = {};
this.initDone = false;
//this.init();
}
/**
* Instanz
*/
static getInstance(): LocalizationManager{
if (!LocalizationManager._instance) {
LocalizationManager._instance = new LocalizationManager();
}
return LocalizationManager._instance;
}
public setData(data: any, source: LocalizationSource): void {
let self = this;
if (source === LocalizationSource.Client) {
self.client = data;
return;
}
if (source === LocalizationSource.Text) {
self.text = data;
return;
}
if (source === LocalizationSource.Annotations) {
self.annotations = data;
return;
}
}
/**
* Initialisiert den Manager.
* Lädt die Resource-Datei
*/
private async init(): Promise<boolean> {
let self = this;
let clientUrl = $("#jsGetResourcesClientUrl").val() as string;
if (clientUrl !== undefined && clientUrl !== null && clientUrl.length > 0) {
var x = await $.getJSON(clientUrl, data => {
self.client = data;
});
}
let textUrl = $("#jsGetResourcesTextUrl").val() as string;
if (textUrl !== undefined && textUrl !== null && textUrl!.length > 0) {
await $.getJSON(textUrl, data => {
self.text = data;
});
}
let annotationsUrl = $("#jsGetResourcesAnnotationsUrl").val() as string;
if (annotationsUrl !== undefined && annotationsUrl !== null && annotationsUrl!.length > 0) {
await $.getJSON(annotationsUrl, data => {
self.annotations = data;
self.initDone = true;
});
}
return true;
}
/**
* Gibt einen Text aus den "Client-Resources" zurück
* @param key Gesuchter Schlüssel
*/
private getClient(key: string): string {
var self = this;
return self.client[key];
}
/**
* Gibt einen Text aus den "Text-Resources" zurück
* @param key Gesuchter Schlüssel
*/
private getText(key: string): string {
var self = this;
return self.text[key];
}
/**
* Gibt einen Text aus den "Annotation-Resources" zurück
* @param key Gesuchter Schlüssel
*/
private getAnnotation(key: string): string {
var self = this;
return self.annotations[key];
}
/**
* Gibt einen text aus den Resources zurück.
* @param key Gesuchter Schlüssel
* @param source Quelle die durchsucht werden soll
*/
public get(key: string, source: LocalizationSource = LocalizationSource.All): string {
var self = this;
var result = "";
if (source === LocalizationSource.All || source === LocalizationSource.Client) {
result = this.getClient(key);
if (result !== undefined && result !== null && result.length > 0)
return result;
}
if (source === LocalizationSource.All || source === LocalizationSource.Text) {
result = self.getText(key);
if (result !== undefined && result !== null && result.length > 0)
return result;
}
if (source === LocalizationSource.All || source === LocalizationSource.Annotations) {
result = self.getAnnotation(key);
if (result !== undefined && result !== null && result.length > 0)
return result;
}
return result;
}
}
/**
* Übersetzungs-Quellen
*/
export enum LocalizationSource {
All,
Text,
Client,
Annotations
}