forked from student-hub/acs-upb-mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocale_provider.dart
51 lines (44 loc) · 1.32 KB
/
locale_provider.dart
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
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:preferences/preference_service.dart';
import 'package:rrule/rrule.dart';
import 'package:time_machine/time_machine.dart';
mixin Localizable {
String toLocalizedString();
}
class LocaleProvider {
LocaleProvider._();
static String defaultLocale = 'en';
static List<String> supportedLocales = ['en', 'ro'];
static Map<String, Culture> cultures;
static Map<String, RruleL10n> rruleL10ns;
static String get localeString {
final languagePref = PrefService.get('language');
if (languagePref == 'auto') {
final systemLocale = Intl.defaultLocale.substring(0, 2);
if (supportedLocales.contains(systemLocale)) {
return systemLocale;
} else {
return defaultLocale;
}
} else {
return languagePref;
}
}
static Locale localeFromString(String preferenceString) {
switch (preferenceString) {
case 'auto':
return localeFromString(Intl.defaultLocale);
case 'ro':
return const Locale('ro', 'RO');
case 'en':
return const Locale('en', 'US');
default:
return localeFromString(defaultLocale);
}
}
static Locale get locale {
return localeFromString(localeString);
}
static RruleL10n get rruleL10n => rruleL10ns[localeString];
}