12345678910111213141516171819202122232425262728293031323334353637 |
- export class LanguageService {
- private listeners: Array<() => void> = [];
- private document: Document = document;
- constructor (private lsKey: string, private defaultLang = 'pt') {
- this.setupListener();
- }
- setupListener (): void {
- if(this.document !== null) {
- this.document.addEventListener("storage",(event => {
- const e = event as StorageEvent;
- if(e.key === this.lsKey) {
- this.listeners.forEach(l => l());
- }
- }));
- }
- }
- getLang (): string {
- const lang = localStorage.getItem(this.lsKey);
- if(lang === null || lang === undefined) {
- console.warn("Internal Error: User language information has not been set. Returning default...");
- return this.getDefaultLang();
- }
- return lang;
- }
- getDefaultLang (): string {
- return this.defaultLang;
- }
- registerLanguageChangeListener (listener: () => void) {
- this.listeners.push(listener);
- }
- }
|