feat: implement client-side localization infrastructure

- Add languageProvider (StateProvider<String>, default 'ru') with
  supportedLanguages map matching backend locale.Supported
- Wire Accept-Language header into AuthInterceptor via languageGetter
  callback; all API requests now carry the current language
- Sync language from user profile preferences into languageProvider
  on every ProfileNotifier load/update
- Add language field to UpdateProfileRequest, serialized as
  preferences.language in PUT /profile
- Profile screen: НАСТРОЙКИ section displays current language;
  edit sheet adds DropdownButtonFormField for language selection

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
dbastrikin
2026-02-27 23:34:51 +02:00
parent c0cf1b38ea
commit 0567d90784
7 changed files with 94 additions and 8 deletions

View File

@@ -1,32 +1,48 @@
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../core/locale/language_provider.dart';
import '../../shared/models/user.dart';
import 'profile_service.dart';
class ProfileNotifier extends StateNotifier<AsyncValue<User>> {
final ProfileService _service;
final void Function(String) _setLanguage;
ProfileNotifier(this._service) : super(const AsyncValue.loading()) {
ProfileNotifier(this._service, this._setLanguage)
: super(const AsyncValue.loading()) {
load();
}
Future<void> load() async {
state = const AsyncValue.loading();
state = await AsyncValue.guard(() => _service.getProfile());
_syncLanguage();
}
Future<bool> update(UpdateProfileRequest req) async {
try {
final updated = await _service.updateProfile(req);
state = AsyncValue.data(updated);
_syncLanguage();
return true;
} catch (_) {
return false;
}
}
// Propagates the user's preferred language to the global languageProvider.
void _syncLanguage() {
final lang = state.valueOrNull?.preferences['language'] as String?;
if (lang != null && supportedLanguages.containsKey(lang)) {
_setLanguage(lang);
}
}
}
final profileProvider =
StateNotifierProvider<ProfileNotifier, AsyncValue<User>>(
(ref) => ProfileNotifier(ref.read(profileServiceProvider)),
(ref) => ProfileNotifier(
ref.read(profileServiceProvider),
(lang) => ref.read(languageProvider.notifier).state = lang,
),
);

View File

@@ -3,6 +3,7 @@ import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../core/auth/auth_provider.dart';
import '../../core/locale/language_provider.dart';
import '../../core/theme/app_colors.dart';
import '../../shared/models/user.dart';
import 'profile_provider.dart';
@@ -114,6 +115,18 @@ class _ProfileBody extends StatelessWidget {
),
),
],
const SizedBox(height: 16),
// Settings
_SectionLabel('НАСТРОЙКИ'),
const SizedBox(height: 6),
_InfoCard(children: [
_InfoRow(
'Язык',
supportedLanguages[user.preferences['language'] as String? ?? 'ru'] ??
'Русский',
),
]),
const SizedBox(height: 32),
_LogoutButton(),
@@ -327,6 +340,7 @@ class _EditProfileSheetState extends ConsumerState<EditProfileSheet> {
String? _gender;
String? _goal;
String? _activity;
String? _language;
bool _saving = false;
@override
@@ -341,6 +355,7 @@ class _EditProfileSheetState extends ConsumerState<EditProfileSheet> {
_gender = u.gender;
_goal = u.goal;
_activity = u.activity;
_language = u.preferences['language'] as String? ?? 'ru';
}
@override
@@ -367,6 +382,7 @@ class _EditProfileSheetState extends ConsumerState<EditProfileSheet> {
gender: _gender,
goal: _goal,
activity: _activity,
language: _language,
);
final ok = await ref.read(profileProvider.notifier).update(req);
@@ -555,6 +571,21 @@ class _EditProfileSheetState extends ConsumerState<EditProfileSheet> {
onSelectionChanged: (s) => setState(
() => _activity = s.isEmpty ? null : s.first),
),
const SizedBox(height: 20),
// Language
DropdownButtonFormField<String>(
value: _language,
decoration:
const InputDecoration(labelText: 'Язык интерфейса'),
items: supportedLanguages.entries
.map((e) => DropdownMenuItem(
value: e.key,
child: Text(e.value),
))
.toList(),
onChanged: (v) => setState(() => _language = v),
),
const SizedBox(height: 32),
// Save

View File

@@ -16,6 +16,7 @@ class UpdateProfileRequest {
final String? gender;
final String? activity;
final String? goal;
final String? language;
const UpdateProfileRequest({
this.name,
@@ -25,6 +26,7 @@ class UpdateProfileRequest {
this.gender,
this.activity,
this.goal,
this.language,
});
Map<String, dynamic> toJson() {
@@ -36,6 +38,7 @@ class UpdateProfileRequest {
if (gender != null) map['gender'] = gender;
if (activity != null) map['activity'] = activity;
if (goal != null) map['goal'] = goal;
if (language != null) map['preferences'] = {'language': language};
return map;
}
}