ciyidogan commited on
Commit
b74fc10
·
verified ·
1 Parent(s): c71870a

Create locale-manager.service.ts

Browse files
flare-ui/src/app/services/locale-manager.service.ts ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { Injectable } from '@angular/core';
2
+ import { HttpClient, HttpHeaders } from '@angular/common/http';
3
+ import { Observable, of } from 'rxjs';
4
+ import { map, catchError } from 'rxjs/operators';
5
+ import { AuthService } from './auth.service';
6
+
7
+ export interface Locale {
8
+ code: string;
9
+ name: string;
10
+ english_name: string;
11
+ }
12
+
13
+ export interface LocaleDetails extends Locale {
14
+ native_name?: string;
15
+ direction: string;
16
+ date_format: string;
17
+ time_format: string;
18
+ datetime_format: string;
19
+ currency: string;
20
+ currency_symbol: string;
21
+ decimal_separator: string;
22
+ thousands_separator: string;
23
+ week_starts_on: number;
24
+ months?: string[];
25
+ days?: string[];
26
+ am_pm?: string[];
27
+ common_phrases?: { [key: string]: string };
28
+ }
29
+
30
+ @Injectable({
31
+ providedIn: 'root'
32
+ })
33
+ export class LocaleManagerService {
34
+ private apiUrl = '/api';
35
+ private localesCache?: Locale[];
36
+
37
+ constructor(
38
+ private http: HttpClient,
39
+ private authService: AuthService
40
+ ) {}
41
+
42
+ private getAuthHeaders(): HttpHeaders {
43
+ const token = this.authService.getToken();
44
+ return new HttpHeaders({
45
+ 'Authorization': `Bearer ${token}`,
46
+ 'Content-Type': 'application/json'
47
+ });
48
+ }
49
+
50
+ getAvailableLocales(): Observable<Locale[]> {
51
+ if (this.localesCache) {
52
+ return of(this.localesCache);
53
+ }
54
+
55
+ return this.http.get<{ locales: Locale[], default: string }>(
56
+ `${this.apiUrl}/locales`,
57
+ { headers: this.getAuthHeaders() }
58
+ ).pipe(
59
+ map(response => {
60
+ this.localesCache = response.locales;
61
+ return response.locales;
62
+ }),
63
+ catchError(() => {
64
+ // Fallback locales if API fails
65
+ const fallback = [
66
+ { code: 'tr-TR', name: 'Türkçe', english_name: 'Turkish' },
67
+ { code: 'en-US', name: 'English', english_name: 'English (US)' }
68
+ ];
69
+ this.localesCache = fallback;
70
+ return of(fallback);
71
+ })
72
+ );
73
+ }
74
+
75
+ getLocaleDetails(code: string): Observable<LocaleDetails | null> {
76
+ return this.http.get<LocaleDetails>(
77
+ `${this.apiUrl}/locales/${code}`,
78
+ { headers: this.getAuthHeaders() }
79
+ ).pipe(
80
+ catchError(() => of(null))
81
+ );
82
+ }
83
+
84
+ validateLanguages(languages: string[]): Observable<string[]> {
85
+ return this.getAvailableLocales().pipe(
86
+ map(locales => {
87
+ const availableCodes = locales.map(l => l.code);
88
+ return languages.filter(lang => !availableCodes.includes(lang));
89
+ })
90
+ );
91
+ }
92
+
93
+ clearCache(): void {
94
+ this.localesCache = undefined;
95
+ }
96
+ }