Lang save

This commit is contained in:
2024-03-10 13:53:57 +03:00
parent e3dac5bc83
commit c4665737dc
3 changed files with 7 additions and 6 deletions

View File

@@ -1,17 +1,15 @@
import { APP_INITIALIZER, ApplicationConfig } from "@angular/core";
import { provideRouter } from "@angular/router";
import { provideHttpClient } from "@angular/common/http";
import { routes } from "./app.routes";
import { TranslateService } from "./services/translate.service";
export function setupTranslateServiceFactory(service: TranslateService) {
return () => service.use("en");
const lang = localStorage.getItem("lang") ?? "en";
return () => service.use(lang);
}
export const appConfig: ApplicationConfig = {
providers: [
provideRouter(routes),
provideHttpClient(),
TranslateService,
{

View File

@@ -57,6 +57,8 @@
align-items: center;
span {
font-weight: 600;
user-select: none;
-webkit-user-select: none;
}
fa-icon {
font-size: 1.2rem;

View File

@@ -5,12 +5,13 @@ import { Injectable } from "@angular/core";
export class TranslateService {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
data: Record<string, any> = {};
lang = "en";
lang: string | null = null;
constructor(private http: HttpClient) {}
use(lang: string): Promise<object> {
this.lang = lang ?? "en";
localStorage.setItem("lang", this.lang);
return new Promise<object>((resolve) => {
const langPath = `assets/i18n/${lang ?? "en"}.json`;
const langPath = `assets/i18n/${this.lang}.json`;
this.http.get(langPath).subscribe({
next: (response) => {
this.data = response ?? {};