|
@@ -0,0 +1,42 @@
|
|
1
|
+import 'package:flutter/material.dart';
|
|
2
|
+import 'package:userstory2translate/localization/localization.dart';
|
|
3
|
+import 'package:shared_preferences/shared_preferences.dart';
|
|
4
|
+
|
|
5
|
+const String LAGUAGE_CODE = 'languageCode';
|
|
6
|
+
|
|
7
|
+//languages code
|
|
8
|
+const String ENGLISH = 'en';
|
|
9
|
+const String FARSI = 'fa';
|
|
10
|
+const String SPANISH = 'es';
|
|
11
|
+const String HINDI = 'hi';
|
|
12
|
+
|
|
13
|
+Future<Locale> setLocale(String languageCode) async {
|
|
14
|
+ SharedPreferences _prefs = await SharedPreferences.getInstance();
|
|
15
|
+ await _prefs.setString(LAGUAGE_CODE, languageCode);
|
|
16
|
+ return _locale(languageCode);
|
|
17
|
+}
|
|
18
|
+
|
|
19
|
+Future<Locale> getLocale() async {
|
|
20
|
+ SharedPreferences _prefs = await SharedPreferences.getInstance();
|
|
21
|
+ String languageCode = _prefs.getString(LAGUAGE_CODE) ?? "en";
|
|
22
|
+ return _locale(languageCode);
|
|
23
|
+}
|
|
24
|
+
|
|
25
|
+Locale _locale(String languageCode) {
|
|
26
|
+ switch (languageCode) {
|
|
27
|
+ case ENGLISH:
|
|
28
|
+ return Locale(ENGLISH, 'US');
|
|
29
|
+ case FARSI:
|
|
30
|
+ return Locale(FARSI, "IR");
|
|
31
|
+ case SPANISH:
|
|
32
|
+ return Locale(SPANISH, "SP");
|
|
33
|
+ case HINDI:
|
|
34
|
+ return Locale(HINDI, "IN");
|
|
35
|
+ default:
|
|
36
|
+ return Locale(ENGLISH, 'US');
|
|
37
|
+ }
|
|
38
|
+}
|
|
39
|
+
|
|
40
|
+String getTranslated(BuildContext context, String key) {
|
|
41
|
+ return DemoLocalization.of(context).translate(key);
|
|
42
|
+}
|