Skip to content

Commit 02b3434

Browse files
committed
feat: Add locale support to the app
- Added locale to AppState - Mapped language code to Locale - Added default locale if not found
1 parent d7bae8c commit 02b3434

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

lib/app/bloc/app_bloc.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ class AppBloc extends Bloc<AppEvent, AppState> {
101101
final newAppTextScaleFactor = _mapTextScaleFactor(
102102
userAppSettings.displaySettings.textScaleFactor,
103103
);
104+
// Map language code to Locale
105+
final newLocale = Locale(userAppSettings.language);
104106

105107
emit(
106108
state.copyWith(
@@ -109,6 +111,7 @@ class AppBloc extends Bloc<AppEvent, AppState> {
109111
appTextScaleFactor: newAppTextScaleFactor,
110112
fontFamily: newFontFamily,
111113
settings: userAppSettings, // Store the fetched settings
114+
locale: newLocale, // Store the new locale
112115
),
113116
);
114117
} on NotFoundException {
@@ -120,6 +123,7 @@ class AppBloc extends Bloc<AppEvent, AppState> {
120123
themeMode: ThemeMode.system,
121124
flexScheme: FlexScheme.material,
122125
appTextScaleFactor: AppTextScaleFactor.medium, // Default enum value
126+
locale: const Locale('en'), // Default to English if settings not found
123127
settings: UserAppSettings(
124128
id: state.user!.id,
125129
), // Provide default settings

lib/app/bloc/app_state.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class AppState extends Equatable {
2727
this.fontFamily,
2828
this.status = AppStatus.initial,
2929
this.user, // User is now nullable and defaults to null
30+
this.locale, // Added locale
3031
});
3132

3233
/// The index of the currently selected item in the bottom navigation bar.
@@ -54,6 +55,9 @@ class AppState extends Equatable {
5455
/// User-specific application settings.
5556
final UserAppSettings settings; // Add settings property
5657

58+
/// The current application locale.
59+
final Locale? locale; // Added locale
60+
5761
/// Creates a copy of the current state with updated values.
5862
AppState copyWith({
5963
int? selectedBottomNavigationIndex,
@@ -64,7 +68,9 @@ class AppState extends Equatable {
6468
AppStatus? status,
6569
User? user,
6670
UserAppSettings? settings, // Add settings to copyWith
71+
Locale? locale, // Added locale
6772
bool clearFontFamily = false,
73+
bool clearLocale = false, // Added to allow clearing locale
6874
}) {
6975
return AppState(
7076
selectedBottomNavigationIndex:
@@ -76,6 +82,7 @@ class AppState extends Equatable {
7682
status: status ?? this.status,
7783
user: user ?? this.user,
7884
settings: settings ?? this.settings, // Copy settings
85+
locale: clearLocale ? null : locale ?? this.locale, // Added locale
7986
);
8087
}
8188

@@ -89,5 +96,6 @@ class AppState extends Equatable {
8996
status,
9097
user,
9198
settings, // Include settings in props
99+
locale, // Added locale to props
92100
];
93101
}

0 commit comments

Comments
 (0)