-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathLocaleSettingsService.ts
More file actions
82 lines (72 loc) · 2.16 KB
/
Copy pathLocaleSettingsService.ts
File metadata and controls
82 lines (72 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import AsyncStorage from '@react-native-async-storage/async-storage';
import * as RNLocalize from 'react-native-localize';
import {
resolveActiveLocale,
type OdeUiLocale,
type UiLocalePreference,
} from '../lib/locale';
import AppConfigService from './AppConfigService';
const STORAGE_KEY = '@ode/uiLocale';
export class LocaleSettingsService {
private static instance: LocaleSettingsService | null = null;
private preference: UiLocalePreference = 'auto';
private loaded = false;
static getInstance(): LocaleSettingsService {
if (!LocaleSettingsService.instance) {
LocaleSettingsService.instance = new LocaleSettingsService();
}
return LocaleSettingsService.instance;
}
async load(): Promise<void> {
if (this.loaded) return;
try {
const stored = await AsyncStorage.getItem(STORAGE_KEY);
if (
stored === 'auto' ||
stored === 'en' ||
stored === 'pt' ||
stored === 'fr'
) {
this.preference = stored;
}
} catch (err) {
console.warn('[LocaleSettingsService] Failed to load preference:', err);
}
this.loaded = true;
}
getPreference(): UiLocalePreference {
return this.preference;
}
async setPreference(preference: UiLocalePreference): Promise<void> {
this.preference = preference;
this.loaded = true;
await AsyncStorage.setItem(STORAGE_KEY, preference);
}
getDeviceLocale(): string {
try {
const locales = RNLocalize.getLocales();
if (locales.length > 0 && locales[0]?.languageTag) {
return locales[0].languageTag;
}
} catch {
// ignore
}
return 'en';
}
/**
* Resolved catalog locale for Formplayer bridge and i18next.
*/
async resolveActiveLocale(
sessionOverride?: string | null,
): Promise<OdeUiLocale> {
await this.load();
const appConfig = await AppConfigService.getInstance().getConfig();
return resolveActiveLocale({
preference: this.preference,
deviceLocale: this.getDeviceLocale(),
bundleDefaultLocale: appConfig?.defaultLocale ?? null,
sessionOverride,
});
}
}
export const localeSettingsService = LocaleSettingsService.getInstance();