Lang save
This commit is contained in:
@@ -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,
|
||||
{
|
||||
|
||||
@@ -57,6 +57,8 @@
|
||||
align-items: center;
|
||||
span {
|
||||
font-weight: 600;
|
||||
user-select: none;
|
||||
-webkit-user-select: none;
|
||||
}
|
||||
fa-icon {
|
||||
font-size: 1.2rem;
|
||||
|
||||
@@ -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 ?? {};
|
||||
|
||||
Reference in New Issue
Block a user