Skip to content

Commit

Permalink
Add language change in settings
Browse files Browse the repository at this point in the history
  • Loading branch information
GravityDarkLab committed Jan 30, 2024
1 parent 99f3107 commit 0a5d264
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 6 deletions.
4 changes: 3 additions & 1 deletion lib/l10n/app_de.arb
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,7 @@
"username": "Benutzername",
"password": "Passwort",
"enter_your_password": "Geben Sie Ihr Passwort ein",
"home": "Startseite"
"home": "Startseite",
"language_selection": "Sprache",
"language_selection_description": "Wählen Sie Ihre bevorzugte Sprache aus"
}
4 changes: 3 additions & 1 deletion lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,7 @@
"username": "username",
"password": "password",
"enter_your_password": "Enter your password",
"home": "Home"
"home": "Home",
"language_selection": "Language",
"language_selection_description": "Select your preferred language"
}
4 changes: 3 additions & 1 deletion lib/l10n/app_es.arb
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,7 @@
"username": "nombre de usuario",
"password": "contraseña",
"enter_your_password": "Introduce tu contraseña",
"home": "Inicio"
"home": "Inicio",
"language_selection": "Idioma",
"language_selection_description": "Selecciona tu idioma preferido."
}
4 changes: 3 additions & 1 deletion lib/l10n/app_fr.arb
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,7 @@
"username": "nom d'utilisateur",
"password": "mot de passe",
"enter_your_password": "Entrez votre mot de passe",
"home": "Accueil"
"home": "Accueil",
"language_selection": "Langue",
"language_selection_description": "Choisissez votre langue préférée."
}
4 changes: 3 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:gocast_mobile/models/user/user_state_model.dart';
import 'package:gocast_mobile/providers.dart';
import 'package:gocast_mobile/utils/UserPreferences.dart';
import 'package:gocast_mobile/utils/globals.dart';
import 'package:gocast_mobile/utils/theme.dart';
import 'package:gocast_mobile/navigation_tab.dart';
Expand All @@ -21,6 +22,7 @@ Future<void> main() async {
Logger.level = Level.info;
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
await UserPreferences.init();

runApp(
const ProviderScope(
Expand Down Expand Up @@ -56,7 +58,7 @@ class App extends ConsumerWidget {
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: L10n.all,
locale: const Locale('de'),
locale: Locale(UserPreferences.getLanguage()),
theme: appTheme, // Your light theme
darkTheme: darkAppTheme, // Define your dark theme
themeMode:
Expand Down
27 changes: 27 additions & 0 deletions lib/utils/UserPreferences.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import 'package:shared_preferences/shared_preferences.dart';

class UserPreferences {
static SharedPreferences? _preferences;

static const _keyLanguage = 'language';

static Future init() async => _preferences = await SharedPreferences.getInstance();

static Future setLanguage(String language) async => await _preferences?.setString(_keyLanguage, language);

static String getLanguage() => _preferences?.getString(_keyLanguage) ?? 'en';

static String getLanguageName(String lang) {
switch (lang) {
case 'en':
return 'English';
case 'es':
return 'Español';
case 'fr':
return 'Français';
case 'de':
return 'Deutsch';
}
return 'English';
}
}
1 change: 1 addition & 0 deletions lib/view_models/setting_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,5 @@ class SettingViewModel extends StateNotifier<SettingState> {
void setLoading(bool isLoading) {
state = state.copyWith(isLoading: isLoading);
}

}
35 changes: 34 additions & 1 deletion lib/views/settings_view/settings_screen_view.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:gocast_mobile/providers.dart';
import 'package:gocast_mobile/utils/UserPreferences.dart';
import 'package:gocast_mobile/views/on_boarding_view/welcome_screen_view.dart';
import 'package:gocast_mobile/views/settings_view/playback_speed_settings_view.dart';
import 'package:gocast_mobile/views/settings_view/preferred_greeting_view.dart';
Expand Down Expand Up @@ -71,6 +72,7 @@ class _SettingsScreenState extends ConsumerState<SettingsScreen> {
ref: ref,
),
_buildThemeSelectionTile(context, ref),
_builLanguageSelectionTile(context, ref),
_buildSwitchListTile(
title: AppLocalizations.of(context)!.download_over_wifi_only,
value: settingState.isDownloadWithWifiOnly,
Expand Down Expand Up @@ -126,7 +128,6 @@ class _SettingsScreenState extends ConsumerState<SettingsScreen> {
} else {
themeModeText = AppLocalizations.of(context)!.system_default;
}

return ListTile(
title: Text(AppLocalizations.of(context)!.choose_theme),
subtitle: Text(themeModeText),
Expand All @@ -135,6 +136,38 @@ class _SettingsScreenState extends ConsumerState<SettingsScreen> {
);
}

ListTile _builLanguageSelectionTile(BuildContext context, WidgetRef ref) {
return ListTile(
title: Text(AppLocalizations.of(context)!.language_selection),
subtitle: Text(UserPreferences.getLanguageName(UserPreferences.getLanguage())),
trailing: const Icon(Icons.arrow_forward_ios),
onTap: () async {
String? selectedLanguage = await showDialog<String>(
context: context,
builder: (BuildContext context) {
return SimpleDialog(
title: Text(AppLocalizations.of(context)!.language_selection_description),
children: <String>['en', 'fr', 'de', 'es']
.map((String lang) => SimpleDialogOption(
onPressed: () {
Navigator.pop(context, lang);
},
child: Text(UserPreferences.getLanguageName(lang)), // Assuming you have a method to get the language name
))
.toList(),
);
},
);
if (selectedLanguage != null) {
await UserPreferences.setLanguage(selectedLanguage);
//to rebuild the app
ref.read(settingViewModelProvider.notifier).setLoading(false);
}
},
);
}


void _showThemeSelectionSheet(BuildContext context, WidgetRef ref) {
showModalBottomSheet(
context: context,
Expand Down

0 comments on commit 0a5d264

Please sign in to comment.