попытки в локализацию

This commit is contained in:
MaD 2024-12-16 23:50:05 +04:00
parent 8b6d1d8dab
commit 2e52b5d09d
5 changed files with 63 additions and 60 deletions

View File

@ -72,16 +72,6 @@ abstract class AppLocale {
static const LocalizationsDelegate<AppLocale> delegate = _AppLocaleDelegate(); static const LocalizationsDelegate<AppLocale> delegate = _AppLocaleDelegate();
/// A list of this localizations delegate along with the default localizations
/// delegates.
///
/// Returns a list of localizations delegates containing this delegate along with
/// GlobalMaterialLocalizations.delegate, GlobalCupertinoLocalizations.delegate,
/// and GlobalWidgetsLocalizations.delegate.
///
/// Additional delegates can be added by appending to this list in
/// MaterialApp. This list does not have to be used at all if a custom list
/// of delegates is preferred or required.
static const List<LocalizationsDelegate<dynamic>> localizationsDelegates = <LocalizationsDelegate<dynamic>>[ static const List<LocalizationsDelegate<dynamic>> localizationsDelegates = <LocalizationsDelegate<dynamic>>[
delegate, delegate,
GlobalMaterialLocalizations.delegate, GlobalMaterialLocalizations.delegate,
@ -89,35 +79,21 @@ abstract class AppLocale {
GlobalWidgetsLocalizations.delegate, GlobalWidgetsLocalizations.delegate,
]; ];
/// A list of this localizations delegate's supported locales.
static const List<Locale> supportedLocales = <Locale>[ static const List<Locale> supportedLocales = <Locale>[
Locale('en'), Locale('en'),
Locale('ru') Locale('ru')
]; ];
/// No description provided for @search. // Существующие строки
///
/// In ru, this message translates to:
/// **'Поиск'**
String get search; String get search;
/// No description provided for @liked.
///
/// In ru, this message translates to:
/// **'нравится!'**
String get liked; String get liked;
/// No description provided for @disliked.
///
/// In ru, this message translates to:
/// **'не нравится :<'**
String get disliked; String get disliked;
/// No description provided for @arbEnding.
///
/// In ru, this message translates to:
/// **'ЗАПЯТАЯ'**
String get arbEnding; String get arbEnding;
// Новые строки для HeroDetailScreen
String get heroDetailsTitle;
String get heroNoImage;
String get heroNoDescription;
} }
class _AppLocaleDelegate extends LocalizationsDelegate<AppLocale> { class _AppLocaleDelegate extends LocalizationsDelegate<AppLocale> {

View File

@ -16,5 +16,15 @@ class AppLocaleEn extends AppLocale {
String get disliked => 'disliked :<'; String get disliked => 'disliked :<';
@override @override
String get arbEnding => 'ЗАПЯТАЯ'; String get arbEnding => 'COMMA';
}
// Добавленные строки
@override
String get heroDetailsTitle => 'Hero Details';
@override
String get heroNoImage => 'No image available';
@override
String get heroNoDescription => 'No description available';
}

View File

@ -1,6 +1,6 @@
import 'app_locale.dart'; import 'app_locale.dart';
// ignore_for_file: type=lint import 'app_locale.dart';
/// The translations for Russian (`ru`). /// The translations for Russian (`ru`).
class AppLocaleRu extends AppLocale { class AppLocaleRu extends AppLocale {
@ -17,4 +17,14 @@ class AppLocaleRu extends AppLocale {
@override @override
String get arbEnding => 'ЗАПЯТАЯ'; String get arbEnding => 'ЗАПЯТАЯ';
}
// Добавленные строки
@override
String get heroDetailsTitle => 'Детали героя';
@override
String get heroNoImage => 'Изображение отсутствует';
@override
String get heroNoDescription => 'Описание отсутствует';
}

View File

@ -3,6 +3,7 @@ import 'package:flutter_bloc/flutter_bloc.dart';
import '../../presentation/home_page/bloc/hero_detail_bloc.dart'; import '../../presentation/home_page/bloc/hero_detail_bloc.dart';
import '../../data/dtos/hero_dto.dart'; import '../../data/dtos/hero_dto.dart';
import '../../data/repositories/hero_repository.dart'; import '../../data/repositories/hero_repository.dart';
import '../../Components/locale/l10n/app_locale.dart';
class HeroDetailScreen extends StatelessWidget { class HeroDetailScreen extends StatelessWidget {
final int heroId; final int heroId;
@ -13,43 +14,45 @@ class HeroDetailScreen extends StatelessWidget {
Widget build(BuildContext context) { Widget build(BuildContext context) {
final heroRepository = context.read<HeroRepository>(); final heroRepository = context.read<HeroRepository>();
// Получаем текущую локализацию
final locale = AppLocale.of(context)!;
return BlocProvider( return BlocProvider(
create: (_) => HeroDetailBloc(heroRepository)..add(FetchHeroDetails(heroId)), create: (_) => HeroDetailBloc(heroRepository)..add(FetchHeroDetails(heroId)),
child: Scaffold( child: Scaffold(
appBar: AppBar(title: Text('Hero Details')), appBar: AppBar(
title: Text(locale.heroDetailsTitle),
),
body: BlocBuilder<HeroDetailBloc, HeroDetailState>( body: BlocBuilder<HeroDetailBloc, HeroDetailState>(
builder: (context, state) { builder: (context, state) {
if (state is HeroDetailLoading) { if (state is HeroDetailLoading) {
return Center(child: CircularProgressIndicator()); return const Center(child: CircularProgressIndicator());
} else if (state is HeroDetailLoaded) { } else if (state is HeroDetailLoaded) {
final hero = state.hero; final hero = state.hero;
return Padding( return Column(
padding: const EdgeInsets.all(16.0), children: [
child: Column( hero.portraitUrl != null
crossAxisAlignment: CrossAxisAlignment.start, ? Image.network(hero.portraitUrl!)
children: [ : Column(
Center( children: [
child: hero.portraitUrl != null const Icon(Icons.image, size: 100),
? Image.network(hero.portraitUrl!, height: 150) Text(locale.heroNoImage),
: Icon(Icons.image, size: 150), ],
), ),
SizedBox(height: 16), Text(
Text( hero.name,
hero.name, style: const TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold), ),
), Text(
SizedBox(height: 16), hero.description ?? locale.heroNoDescription,
Text( style: const TextStyle(fontSize: 16),
hero.description ?? 'No description available', ),
style: TextStyle(fontSize: 16), ],
),
],
),
); );
} else if (state is HeroDetailError) { } else if (state is HeroDetailError) {
return Center(child: Text('Error: ${state.message}')); return Center(child: Text(state.message));
} }
return SizedBox.shrink(); return const SizedBox.shrink();
}, },
), ),
), ),

View File

@ -8,6 +8,8 @@ import 'package:provider/provider.dart';
import 'presentation/home_page/bloc/hero_search_bloc.dart'; import 'presentation/home_page/bloc/hero_search_bloc.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart'; import 'package:flutter_localizations/flutter_localizations.dart';
import '../../Components/locale/l10n/app_locale.dart';
@ -47,6 +49,8 @@ class MyApp extends StatelessWidget {
); );
} }
return MaterialApp( return MaterialApp(
localizationsDelegates: AppLocale.localizationsDelegates,
supportedLocales: AppLocale.supportedLocales,
home: Scaffold( home: Scaffold(
body: Center( body: Center(
child: CircularProgressIndicator(), child: CircularProgressIndicator(),