40 lines
1.1 KiB
TypeScript
40 lines
1.1 KiB
TypeScript
import { Injectable, signal } from '@angular/core';
|
|
|
|
export type SupportedLanguage = 'en' | 'sl';
|
|
|
|
@Injectable({
|
|
providedIn: 'root',
|
|
})
|
|
export class LanguageService {
|
|
private readonly LANGUAGE_KEY = 'selectedLanguage';
|
|
|
|
currentLanguage = signal<SupportedLanguage>(this.#getInitialLanguage());
|
|
|
|
#getInitialLanguage(): SupportedLanguage {
|
|
// Try to get language from localStorage
|
|
const storedLang = localStorage.getItem(
|
|
this.LANGUAGE_KEY
|
|
) as SupportedLanguage | null;
|
|
if (storedLang && (storedLang === 'en' || storedLang === 'sl')) {
|
|
return storedLang;
|
|
}
|
|
|
|
// Try to detect from browser settings
|
|
const browserLang = navigator.language.substring(0, 2).toLowerCase();
|
|
if (browserLang === 'sl') {
|
|
return 'sl';
|
|
}
|
|
|
|
// Default to English
|
|
return 'en';
|
|
}
|
|
|
|
setLanguage(lang: SupportedLanguage): void {
|
|
this.currentLanguage.set(lang);
|
|
localStorage.setItem(this.LANGUAGE_KEY, lang);
|
|
// This is where you would typically trigger translation changes
|
|
// in a real implementation
|
|
console.log(`Language set to: ${lang}`);
|
|
}
|
|
}
|