added language routing system

This commit is contained in:
sdarbinyan
2026-02-26 22:23:08 +04:00
parent a4765ffe98
commit e4206d8abc
34 changed files with 197 additions and 98 deletions

View File

@@ -1,4 +1,5 @@
import { Injectable, signal } from '@angular/core';
import { Router } from '@angular/router';
export interface Language {
code: string;
@@ -22,7 +23,7 @@ export class LanguageService {
currentLanguage = this.currentLanguageSignal.asReadonly();
constructor() {
constructor(private router: Router) {
// Load saved language from localStorage
const savedLang = localStorage.getItem('selectedLanguage');
if (savedLang && this.languages.find(l => l.code === savedLang && l.enabled)) {
@@ -38,6 +39,19 @@ export class LanguageService {
}
}
/** Change language and navigate to the same page with the new prefix */
switchLanguage(langCode: string): void {
const lang = this.languages.find(l => l.code === langCode);
if (!lang?.enabled) return;
const currentUrl = this.router.url;
const currentLang = this.currentLanguageSignal();
const newUrl = currentUrl.replace(new RegExp(`^/${currentLang}`), `/${langCode}`);
this.setLanguage(langCode);
this.router.navigateByUrl(newUrl);
}
getCurrentLanguage(): Language | undefined {
return this.languages.find(l => l.code === this.currentLanguageSignal());
}