mirror of
https://github.com/dz0ny/meshcore-sar.git
synced 2026-08-11 16:30:28 +00:00
Add Croatian and Slovenian localizations, implement locale preferences, and update settings screen
- 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.
This commit is contained in:
70
lib/services/locale_preferences.dart
Normal file
70
lib/services/locale_preferences.dart
Normal file
@@ -0,0 +1,70 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user