mirror of
https://github.com/dz0ny/meshcore-sar.git
synced 2026-08-11 16:30:28 +00:00
- Created `app_localizations_hr.dart` and `app_localizations_sl.dart` for Croatian and Slovenian translations. - Added `app_sl.arb` file for Slovenian localization strings. - Enhanced `main.dart` to support locale selection and initialization. - Updated `home_screen.dart` and `settings_screen.dart` to handle locale changes and display current language. - Implemented `locale_preferences.dart` service for managing locale settings using SharedPreferences. - Modified `pubspec.yaml` to include `flutter_localizations` and enable localization file generation.
71 lines
1.8 KiB
Dart
71 lines
1.8 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
/// Service for managing locale preferences
|
|
class LocalePreferences {
|
|
static const String _localeKey = 'app_locale';
|
|
|
|
/// Supported locales
|
|
static const List<Locale> supportedLocales = [
|
|
Locale('en'), // English
|
|
Locale('sl'), // Slovenian
|
|
Locale('hr'), // Croatian
|
|
];
|
|
|
|
/// Get the saved locale or return null to use system locale
|
|
static Future<Locale?> getLocale() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final localeCode = prefs.getString(_localeKey);
|
|
|
|
if (localeCode == null) {
|
|
return null; // Use system locale
|
|
}
|
|
|
|
return Locale(localeCode);
|
|
}
|
|
|
|
/// Save the selected locale
|
|
static Future<void> setLocale(Locale? locale) async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
|
|
if (locale == null) {
|
|
// Remove preference to use system locale
|
|
await prefs.remove(_localeKey);
|
|
} else {
|
|
await prefs.setString(_localeKey, locale.languageCode);
|
|
}
|
|
}
|
|
|
|
/// Get display name for a locale
|
|
static String getDisplayName(Locale? locale) {
|
|
if (locale == null) {
|
|
return 'System Default';
|
|
}
|
|
|
|
switch (locale.languageCode) {
|
|
case 'en':
|
|
return 'English';
|
|
case 'sl':
|
|
return 'Slovenščina';
|
|
case 'hr':
|
|
return 'Hrvatski';
|
|
default:
|
|
return locale.languageCode;
|
|
}
|
|
}
|
|
|
|
/// Get native display name for a locale (shown in selection dialog)
|
|
static String getNativeDisplayName(Locale locale) {
|
|
switch (locale.languageCode) {
|
|
case 'en':
|
|
return 'English';
|
|
case 'sl':
|
|
return 'Slovenščina';
|
|
case 'hr':
|
|
return 'Hrvatski';
|
|
default:
|
|
return locale.languageCode;
|
|
}
|
|
}
|
|
}
|