v1.4.1+62
All checks were successful
Flutter Schmutter / build (push) Successful in 3m48s

This commit is contained in:
2025-06-21 13:40:44 +02:00
parent 2b5aaad331
commit 73a44bb269
15 changed files with 608 additions and 515 deletions

View File

@ -6,6 +6,13 @@ import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:encrypt_shared_preferences/provider.dart';
const Map<String, Locale> supportedLocales = {
'en_US': Locale('en', 'US'),
'de_DE': Locale('de', 'DE'),
'fr_FR': Locale('fr', 'FR'),
'nl_NL': Locale('nl', 'NL'),
};
class MyTranslations extends Translations {
static final MyTranslations instance = MyTranslations._internal();
MyTranslations._internal();
@ -13,15 +20,18 @@ class MyTranslations extends Translations {
static final Map<String, Map<String, String>> _translations = {};
static Future<void> loadTranslations() async {
final locales = ['en_US', 'de_DE', 'fr_FR', 'nl_NL'];
for (final locale in locales) {
final String jsonString = await rootBundle.loadString(
'assets/i18n/$locale.json',
);
final Map<String, dynamic> jsonMap = json.decode(jsonString);
_translations[locale] = jsonMap.map(
(key, value) => MapEntry(key, value.toString()),
);
for (final localeKey in supportedLocales.keys) {
try {
final String jsonString = await rootBundle.loadString(
'assets/i18n/$localeKey.json',
);
final Map<String, dynamic> jsonMap = json.decode(jsonString);
_translations[localeKey] = jsonMap.map(
(key, value) => MapEntry(key, value.toString()),
);
} catch (e) {
debugPrint('Konnte Übersetzung für $localeKey nicht laden: $e');
}
}
}
@ -32,7 +42,7 @@ class MyTranslations extends Translations {
class LocalizationController extends GetxController {
final EncryptedSharedPreferencesAsync storage =
EncryptedSharedPreferencesAsync.getInstance();
Rx<Locale> currentLocale = const Locale('en', 'US').obs;
Rx<Locale> currentLocale = supportedLocales['en_US']!.obs;
@override
void onInit() {
@ -41,25 +51,29 @@ class LocalizationController extends GetxController {
}
Future<void> loadLocale() async {
String? savedLocale = await storage.getString(
String? savedLocaleKey = await storage.getString(
'locale',
defaultValue: 'en_US',
);
if (savedLocale != null && savedLocale.isNotEmpty) {
final List<String> parts = savedLocale.split('_');
currentLocale.value = parts.length == 2
? Locale(parts[0], parts[1])
: Locale(parts[0]);
Get.locale = currentLocale.value;
}
final Locale locale =
supportedLocales[savedLocaleKey ?? 'en_US'] ??
supportedLocales['en_US']!;
currentLocale.value = locale;
Get.locale = locale;
}
Future<void> changeLocale(Locale newLocale) async {
final String localeKey = supportedLocales.entries
.firstWhere(
(entry) => entry.value == newLocale,
orElse: () => supportedLocales.entries.first,
)
.key;
currentLocale.value = newLocale;
Get.updateLocale(newLocale);
await storage.setString(
'locale',
'${newLocale.languageCode}_${newLocale.countryCode}',
);
await storage.setString('locale', localeKey);
}
}