Lang save
This commit is contained in:
@@ -1,17 +1,15 @@
|
|||||||
import { APP_INITIALIZER, ApplicationConfig } from "@angular/core";
|
import { APP_INITIALIZER, ApplicationConfig } from "@angular/core";
|
||||||
import { provideRouter } from "@angular/router";
|
|
||||||
|
|
||||||
import { provideHttpClient } from "@angular/common/http";
|
import { provideHttpClient } from "@angular/common/http";
|
||||||
import { routes } from "./app.routes";
|
|
||||||
import { TranslateService } from "./services/translate.service";
|
import { TranslateService } from "./services/translate.service";
|
||||||
|
|
||||||
export function setupTranslateServiceFactory(service: TranslateService) {
|
export function setupTranslateServiceFactory(service: TranslateService) {
|
||||||
return () => service.use("en");
|
const lang = localStorage.getItem("lang") ?? "en";
|
||||||
|
return () => service.use(lang);
|
||||||
}
|
}
|
||||||
|
|
||||||
export const appConfig: ApplicationConfig = {
|
export const appConfig: ApplicationConfig = {
|
||||||
providers: [
|
providers: [
|
||||||
provideRouter(routes),
|
|
||||||
provideHttpClient(),
|
provideHttpClient(),
|
||||||
TranslateService,
|
TranslateService,
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -57,6 +57,8 @@
|
|||||||
align-items: center;
|
align-items: center;
|
||||||
span {
|
span {
|
||||||
font-weight: 600;
|
font-weight: 600;
|
||||||
|
user-select: none;
|
||||||
|
-webkit-user-select: none;
|
||||||
}
|
}
|
||||||
fa-icon {
|
fa-icon {
|
||||||
font-size: 1.2rem;
|
font-size: 1.2rem;
|
||||||
|
|||||||
@@ -5,12 +5,13 @@ import { Injectable } from "@angular/core";
|
|||||||
export class TranslateService {
|
export class TranslateService {
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
data: Record<string, any> = {};
|
data: Record<string, any> = {};
|
||||||
lang = "en";
|
lang: string | null = null;
|
||||||
constructor(private http: HttpClient) {}
|
constructor(private http: HttpClient) {}
|
||||||
use(lang: string): Promise<object> {
|
use(lang: string): Promise<object> {
|
||||||
this.lang = lang ?? "en";
|
this.lang = lang ?? "en";
|
||||||
|
localStorage.setItem("lang", this.lang);
|
||||||
return new Promise<object>((resolve) => {
|
return new Promise<object>((resolve) => {
|
||||||
const langPath = `assets/i18n/${lang ?? "en"}.json`;
|
const langPath = `assets/i18n/${this.lang}.json`;
|
||||||
this.http.get(langPath).subscribe({
|
this.http.get(langPath).subscribe({
|
||||||
next: (response) => {
|
next: (response) => {
|
||||||
this.data = response ?? {};
|
this.data = response ?? {};
|
||||||
|
|||||||
Reference in New Issue
Block a user