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:
@@ -5,7 +5,11 @@ import 'auth_interceptor.dart';
|
|||||||
class ApiClient {
|
class ApiClient {
|
||||||
late final Dio _dio;
|
late final Dio _dio;
|
||||||
|
|
||||||
ApiClient({required String baseUrl, required SecureStorageService storage}) {
|
ApiClient({
|
||||||
|
required String baseUrl,
|
||||||
|
required SecureStorageService storage,
|
||||||
|
required String Function() languageGetter,
|
||||||
|
}) {
|
||||||
_dio = Dio(BaseOptions(
|
_dio = Dio(BaseOptions(
|
||||||
baseUrl: baseUrl,
|
baseUrl: baseUrl,
|
||||||
connectTimeout: const Duration(seconds: 60),
|
connectTimeout: const Duration(seconds: 60),
|
||||||
@@ -14,7 +18,7 @@ class ApiClient {
|
|||||||
));
|
));
|
||||||
|
|
||||||
_dio.interceptors.addAll([
|
_dio.interceptors.addAll([
|
||||||
AuthInterceptor(storage: storage, dio: _dio),
|
AuthInterceptor(storage: storage, dio: _dio, languageGetter: languageGetter),
|
||||||
LogInterceptor(requestBody: true, responseBody: true),
|
LogInterceptor(requestBody: true, responseBody: true),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,21 +4,28 @@ import '../auth/secure_storage.dart';
|
|||||||
class AuthInterceptor extends Interceptor {
|
class AuthInterceptor extends Interceptor {
|
||||||
final SecureStorageService _storage;
|
final SecureStorageService _storage;
|
||||||
final Dio _dio;
|
final Dio _dio;
|
||||||
|
final String Function() _languageGetter;
|
||||||
|
|
||||||
// Prevents multiple simultaneous token refresh requests
|
// Prevents multiple simultaneous token refresh requests
|
||||||
bool _isRefreshing = false;
|
bool _isRefreshing = false;
|
||||||
final List<({RequestOptions options, ErrorInterceptorHandler handler})>
|
final List<({RequestOptions options, ErrorInterceptorHandler handler})>
|
||||||
_pendingRequests = [];
|
_pendingRequests = [];
|
||||||
|
|
||||||
AuthInterceptor({required SecureStorageService storage, required Dio dio})
|
AuthInterceptor({
|
||||||
: _storage = storage,
|
required SecureStorageService storage,
|
||||||
_dio = dio;
|
required Dio dio,
|
||||||
|
required String Function() languageGetter,
|
||||||
|
}) : _storage = storage,
|
||||||
|
_dio = dio,
|
||||||
|
_languageGetter = languageGetter;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> onRequest(
|
Future<void> onRequest(
|
||||||
RequestOptions options,
|
RequestOptions options,
|
||||||
RequestInterceptorHandler handler,
|
RequestInterceptorHandler handler,
|
||||||
) async {
|
) async {
|
||||||
|
options.headers['Accept-Language'] = _languageGetter();
|
||||||
|
|
||||||
if (options.path.startsWith('/auth/')) {
|
if (options.path.startsWith('/auth/')) {
|
||||||
return handler.next(options);
|
return handler.next(options);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,13 @@
|
|||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
|
import 'package:firebase_auth/firebase_auth.dart' as fb;
|
||||||
|
|
||||||
import '../../shared/models/user.dart';
|
import '../../shared/models/user.dart';
|
||||||
import '../api/api_client.dart';
|
import '../api/api_client.dart';
|
||||||
import '../config/app_config.dart';
|
import '../config/app_config.dart';
|
||||||
|
import '../locale/language_provider.dart';
|
||||||
import 'auth_service.dart';
|
import 'auth_service.dart';
|
||||||
import 'secure_storage.dart';
|
import 'secure_storage.dart';
|
||||||
import 'package:firebase_auth/firebase_auth.dart' as fb;
|
|
||||||
|
|
||||||
enum AuthStatus { unknown, authenticated, unauthenticated }
|
enum AuthStatus { unknown, authenticated, unauthenticated }
|
||||||
|
|
||||||
@@ -144,6 +145,7 @@ final apiClientProvider = Provider<ApiClient>((ref) {
|
|||||||
return ApiClient(
|
return ApiClient(
|
||||||
baseUrl: config.apiBaseUrl,
|
baseUrl: config.apiBaseUrl,
|
||||||
storage: storage,
|
storage: storage,
|
||||||
|
languageGetter: () => ref.read(languageProvider),
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
23
client/lib/core/locale/language_provider.dart
Normal file
23
client/lib/core/locale/language_provider.dart
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
|
|
||||||
|
/// Supported ISO 639-1 language codes with their native names.
|
||||||
|
/// Must match the backend locale.Supported map.
|
||||||
|
const supportedLanguages = <String, String>{
|
||||||
|
'en': 'English',
|
||||||
|
'ru': 'Русский',
|
||||||
|
'es': 'Español',
|
||||||
|
'de': 'Deutsch',
|
||||||
|
'fr': 'Français',
|
||||||
|
'it': 'Italiano',
|
||||||
|
'pt': 'Português',
|
||||||
|
'zh': '中文',
|
||||||
|
'ja': '日本語',
|
||||||
|
'ko': '한국어',
|
||||||
|
'ar': 'العربية',
|
||||||
|
'hi': 'हिन्दी',
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Current app language (ISO 639-1 code).
|
||||||
|
/// Synced from user.preferences['language'] after the profile loads or is updated.
|
||||||
|
/// Defaults to 'ru'.
|
||||||
|
final languageProvider = StateProvider<String>((_) => 'ru');
|
||||||
@@ -1,32 +1,48 @@
|
|||||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
|
|
||||||
|
import '../../core/locale/language_provider.dart';
|
||||||
import '../../shared/models/user.dart';
|
import '../../shared/models/user.dart';
|
||||||
import 'profile_service.dart';
|
import 'profile_service.dart';
|
||||||
|
|
||||||
class ProfileNotifier extends StateNotifier<AsyncValue<User>> {
|
class ProfileNotifier extends StateNotifier<AsyncValue<User>> {
|
||||||
final ProfileService _service;
|
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();
|
load();
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> load() async {
|
Future<void> load() async {
|
||||||
state = const AsyncValue.loading();
|
state = const AsyncValue.loading();
|
||||||
state = await AsyncValue.guard(() => _service.getProfile());
|
state = await AsyncValue.guard(() => _service.getProfile());
|
||||||
|
_syncLanguage();
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<bool> update(UpdateProfileRequest req) async {
|
Future<bool> update(UpdateProfileRequest req) async {
|
||||||
try {
|
try {
|
||||||
final updated = await _service.updateProfile(req);
|
final updated = await _service.updateProfile(req);
|
||||||
state = AsyncValue.data(updated);
|
state = AsyncValue.data(updated);
|
||||||
|
_syncLanguage();
|
||||||
return true;
|
return true;
|
||||||
} catch (_) {
|
} catch (_) {
|
||||||
return false;
|
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 =
|
final profileProvider =
|
||||||
StateNotifierProvider<ProfileNotifier, AsyncValue<User>>(
|
StateNotifierProvider<ProfileNotifier, AsyncValue<User>>(
|
||||||
(ref) => ProfileNotifier(ref.read(profileServiceProvider)),
|
(ref) => ProfileNotifier(
|
||||||
|
ref.read(profileServiceProvider),
|
||||||
|
(lang) => ref.read(languageProvider.notifier).state = lang,
|
||||||
|
),
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import 'package:flutter/services.dart';
|
|||||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
|
|
||||||
import '../../core/auth/auth_provider.dart';
|
import '../../core/auth/auth_provider.dart';
|
||||||
|
import '../../core/locale/language_provider.dart';
|
||||||
import '../../core/theme/app_colors.dart';
|
import '../../core/theme/app_colors.dart';
|
||||||
import '../../shared/models/user.dart';
|
import '../../shared/models/user.dart';
|
||||||
import 'profile_provider.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),
|
const SizedBox(height: 32),
|
||||||
|
|
||||||
_LogoutButton(),
|
_LogoutButton(),
|
||||||
@@ -327,6 +340,7 @@ class _EditProfileSheetState extends ConsumerState<EditProfileSheet> {
|
|||||||
String? _gender;
|
String? _gender;
|
||||||
String? _goal;
|
String? _goal;
|
||||||
String? _activity;
|
String? _activity;
|
||||||
|
String? _language;
|
||||||
bool _saving = false;
|
bool _saving = false;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -341,6 +355,7 @@ class _EditProfileSheetState extends ConsumerState<EditProfileSheet> {
|
|||||||
_gender = u.gender;
|
_gender = u.gender;
|
||||||
_goal = u.goal;
|
_goal = u.goal;
|
||||||
_activity = u.activity;
|
_activity = u.activity;
|
||||||
|
_language = u.preferences['language'] as String? ?? 'ru';
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -367,6 +382,7 @@ class _EditProfileSheetState extends ConsumerState<EditProfileSheet> {
|
|||||||
gender: _gender,
|
gender: _gender,
|
||||||
goal: _goal,
|
goal: _goal,
|
||||||
activity: _activity,
|
activity: _activity,
|
||||||
|
language: _language,
|
||||||
);
|
);
|
||||||
|
|
||||||
final ok = await ref.read(profileProvider.notifier).update(req);
|
final ok = await ref.read(profileProvider.notifier).update(req);
|
||||||
@@ -555,6 +571,21 @@ class _EditProfileSheetState extends ConsumerState<EditProfileSheet> {
|
|||||||
onSelectionChanged: (s) => setState(
|
onSelectionChanged: (s) => setState(
|
||||||
() => _activity = s.isEmpty ? null : s.first),
|
() => _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),
|
const SizedBox(height: 32),
|
||||||
|
|
||||||
// Save
|
// Save
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ class UpdateProfileRequest {
|
|||||||
final String? gender;
|
final String? gender;
|
||||||
final String? activity;
|
final String? activity;
|
||||||
final String? goal;
|
final String? goal;
|
||||||
|
final String? language;
|
||||||
|
|
||||||
const UpdateProfileRequest({
|
const UpdateProfileRequest({
|
||||||
this.name,
|
this.name,
|
||||||
@@ -25,6 +26,7 @@ class UpdateProfileRequest {
|
|||||||
this.gender,
|
this.gender,
|
||||||
this.activity,
|
this.activity,
|
||||||
this.goal,
|
this.goal,
|
||||||
|
this.language,
|
||||||
});
|
});
|
||||||
|
|
||||||
Map<String, dynamic> toJson() {
|
Map<String, dynamic> toJson() {
|
||||||
@@ -36,6 +38,7 @@ class UpdateProfileRequest {
|
|||||||
if (gender != null) map['gender'] = gender;
|
if (gender != null) map['gender'] = gender;
|
||||||
if (activity != null) map['activity'] = activity;
|
if (activity != null) map['activity'] = activity;
|
||||||
if (goal != null) map['goal'] = goal;
|
if (goal != null) map['goal'] = goal;
|
||||||
|
if (language != null) map['preferences'] = {'language': language};
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user