Default language from browser.

This commit is contained in:
Artur 2024-11-09 15:47:52 +01:00
parent c553353741
commit 38e904857c
1 changed files with 20 additions and 4 deletions

View File

@ -9,10 +9,26 @@ import { TranslateService, TranslateModule} from "@ngx-translate/core";
styleUrl: './app.component.css'
})
export class AppComponent {
constructor( public translate: TranslateService ){
this.translate.addLangs(['pl', 'en']);
// Ustaw domyślny język aplikacji
this.translate.setDefaultLang('pl');
this.translate.use('pl');
// Pobierz język przeglądarki i obetnij kod regionu // np. 'en' z 'en-US'
const browserLang = navigator.language .slice(0, 2);
// Wyświetl w konsoli dla weryfikacji
console.log('Język przeglądarki krótki:', browserLang);
// Ustaw język pobrany z przeglądarki, jeśli jest obsługiwany
this.translate.use(browserLang.match(/en|pl/) ? browserLang : 'pl');
}
// app.component.ts
@ -20,5 +36,5 @@ export class AppComponent {
this.translate.use(language);
}
title = 'arti-angular-app';
title = 'Strona';
}