From 20eae72ab0be4478759480d8c7160cba75659209 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=B8=D1=82=D0=B0=20=D0=9F=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=D0=BF=D0=BE=D0=B2?= Date: Thu, 19 Dec 2024 14:49:26 +0400 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D1=8B=20=D0=B4=D0=BB=D1=8F=20?= =?UTF-8?q?=D0=BB=D0=BE=D0=BA=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86=D0=B8?= =?UTF-8?q?=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- l10n.yaml | 6 + l10n/app_en.arb | 9 ++ l10n/app_ru.arb | 9 ++ lib/components/extensions/content_x.dart | 6 + lib/components/locale/l10n/app_locale.dart | 153 ++++++++++++++++++ lib/components/locale/l10n/app_locale_en.dart | 20 +++ lib/components/locale/l10n/app_locale_ru.dart | 20 +++ lib/presentation/home_page/bloc/state.g.dart | 16 +- lib/presentation/like/bloc.dart | 0 lib/presentation/like/events.dart | 0 lib/presentation/like/state.dart | 0 lib/presentation/locale/bloc.dart | 19 +++ lib/presentation/locale/events.dart | 7 + lib/presentation/locale/state.dart | 16 ++ lib/presentation/locale/state.g.dart | 57 +++++++ 15 files changed, 329 insertions(+), 9 deletions(-) create mode 100644 l10n.yaml create mode 100644 l10n/app_en.arb create mode 100644 l10n/app_ru.arb create mode 100644 lib/components/extensions/content_x.dart create mode 100644 lib/components/locale/l10n/app_locale.dart create mode 100644 lib/components/locale/l10n/app_locale_en.dart create mode 100644 lib/components/locale/l10n/app_locale_ru.dart create mode 100644 lib/presentation/like/bloc.dart create mode 100644 lib/presentation/like/events.dart create mode 100644 lib/presentation/like/state.dart create mode 100644 lib/presentation/locale/bloc.dart create mode 100644 lib/presentation/locale/events.dart create mode 100644 lib/presentation/locale/state.dart create mode 100644 lib/presentation/locale/state.g.dart diff --git a/l10n.yaml b/l10n.yaml new file mode 100644 index 0000000..d26d702 --- /dev/null +++ b/l10n.yaml @@ -0,0 +1,6 @@ +arb-dir: l10n +template-arb-file: app_ru.arb +output-localization-file: app_locale.dart +output-dir: lib/components/locale/l10n +output-class: AppLocale +synthetic-package: false \ No newline at end of file diff --git a/l10n/app_en.arb b/l10n/app_en.arb new file mode 100644 index 0000000..a01051c --- /dev/null +++ b/l10n/app_en.arb @@ -0,0 +1,9 @@ +{ + "@@locale": "en", + + "search": "Search", + "liked": "liked!", + "disliked": "disliked :(", + + "arbEnding": "Чтобы не забыть про отсутствие запятой :)" +} \ No newline at end of file diff --git a/l10n/app_ru.arb b/l10n/app_ru.arb new file mode 100644 index 0000000..7b30d41 --- /dev/null +++ b/l10n/app_ru.arb @@ -0,0 +1,9 @@ +{ + "@@locale": "ru", + + "search": "Поиск", + "liked": "понравился!", + "disliked": "разонравился :(", + + "arbEnding": "Чтобы не забыть про отсутствие запятой :)" +} \ No newline at end of file diff --git a/lib/components/extensions/content_x.dart b/lib/components/extensions/content_x.dart new file mode 100644 index 0000000..69e02c6 --- /dev/null +++ b/lib/components/extensions/content_x.dart @@ -0,0 +1,6 @@ +import 'package:flutter/widgets.dart'; +import 'package:pmu/components/locale/l10n/app_locale.dart'; + +extension LocalContextX on BuildContext { + AppLocale get locale => AppLocale.of(this)!; +} \ No newline at end of file diff --git a/lib/components/locale/l10n/app_locale.dart b/lib/components/locale/l10n/app_locale.dart new file mode 100644 index 0000000..0922eb1 --- /dev/null +++ b/lib/components/locale/l10n/app_locale.dart @@ -0,0 +1,153 @@ +import 'dart:async'; + +import 'package:flutter/foundation.dart'; +import 'package:flutter/widgets.dart'; +import 'package:flutter_localizations/flutter_localizations.dart'; +import 'package:intl/intl.dart' as intl; + +import 'app_locale_en.dart'; +import 'app_locale_ru.dart'; + +// ignore_for_file: type=lint + +/// Callers can lookup localized strings with an instance of AppLocale +/// returned by `AppLocale.of(context)`. +/// +/// Applications need to include `AppLocale.delegate()` in their app's +/// `localizationDelegates` list, and the locales they support in the app's +/// `supportedLocales` list. For example: +/// +/// ```dart +/// import 'l10n/app_locale.dart'; +/// +/// return MaterialApp( +/// localizationsDelegates: AppLocale.localizationsDelegates, +/// supportedLocales: AppLocale.supportedLocales, +/// home: MyApplicationHome(), +/// ); +/// ``` +/// +/// ## Update pubspec.yaml +/// +/// Please make sure to update your pubspec.yaml to include the following +/// packages: +/// +/// ```yaml +/// dependencies: +/// # Internationalization support. +/// flutter_localizations: +/// sdk: flutter +/// intl: any # Use the pinned version from flutter_localizations +/// +/// # Rest of dependencies +/// ``` +/// +/// ## iOS Applications +/// +/// iOS applications define key application metadata, including supported +/// locales, in an Info.plist file that is built into the application bundle. +/// To configure the locales supported by your app, you’ll need to edit this +/// file. +/// +/// First, open your project’s ios/Runner.xcworkspace Xcode workspace file. +/// Then, in the Project Navigator, open the Info.plist file under the Runner +/// project’s Runner folder. +/// +/// Next, select the Information Property List item, select Add Item from the +/// Editor menu, then select Localizations from the pop-up menu. +/// +/// Select and expand the newly-created Localizations item then, for each +/// locale your application supports, add a new item and select the locale +/// you wish to add from the pop-up menu in the Value field. This list should +/// be consistent with the languages listed in the AppLocale.supportedLocales +/// property. +abstract class AppLocale { + AppLocale(String locale) : localeName = intl.Intl.canonicalizedLocale(locale.toString()); + + final String localeName; + + static AppLocale? of(BuildContext context) { + return Localizations.of(context, AppLocale); + } + + static const LocalizationsDelegate 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> localizationsDelegates = >[ + delegate, + GlobalMaterialLocalizations.delegate, + GlobalCupertinoLocalizations.delegate, + GlobalWidgetsLocalizations.delegate, + ]; + + /// A list of this localizations delegate's supported locales. + static const List supportedLocales = [ + Locale('en'), + Locale('ru') + ]; + + /// No description provided for @search. + /// + /// In ru, this message translates to: + /// **'Поиск'** + String get search; + + /// No description provided for @liked. + /// + /// In ru, this message translates to: + /// **'понравился!'** + String get liked; + + /// No description provided for @disliked. + /// + /// In ru, this message translates to: + /// **'разонравился :('** + String get disliked; + + /// No description provided for @arbEnding. + /// + /// In ru, this message translates to: + /// **'Чтобы не забыть про отсутствие запятой :)'** + String get arbEnding; +} + +class _AppLocaleDelegate extends LocalizationsDelegate { + const _AppLocaleDelegate(); + + @override + Future load(Locale locale) { + return SynchronousFuture(lookupAppLocale(locale)); + } + + @override + bool isSupported(Locale locale) => ['en', 'ru'].contains(locale.languageCode); + + @override + bool shouldReload(_AppLocaleDelegate old) => false; +} + +AppLocale lookupAppLocale(Locale locale) { + + + // Lookup logic when only language code is specified. + switch (locale.languageCode) { + case 'en': return AppLocaleEn(); + case 'ru': return AppLocaleRu(); + } + + throw FlutterError( + 'AppLocale.delegate failed to load unsupported locale "$locale". This is likely ' + 'an issue with the localizations generation tool. Please file an issue ' + 'on GitHub with a reproducible sample app and the gen-l10n configuration ' + 'that was used.' + ); +} diff --git a/lib/components/locale/l10n/app_locale_en.dart b/lib/components/locale/l10n/app_locale_en.dart new file mode 100644 index 0000000..599548c --- /dev/null +++ b/lib/components/locale/l10n/app_locale_en.dart @@ -0,0 +1,20 @@ +import 'app_locale.dart'; + +// ignore_for_file: type=lint + +/// The translations for English (`en`). +class AppLocaleEn extends AppLocale { + AppLocaleEn([String locale = 'en']) : super(locale); + + @override + String get search => 'Search'; + + @override + String get liked => 'liked!'; + + @override + String get disliked => 'disliked :('; + + @override + String get arbEnding => 'Чтобы не забыть про отсутствие запятой :)'; +} diff --git a/lib/components/locale/l10n/app_locale_ru.dart b/lib/components/locale/l10n/app_locale_ru.dart new file mode 100644 index 0000000..b8d5444 --- /dev/null +++ b/lib/components/locale/l10n/app_locale_ru.dart @@ -0,0 +1,20 @@ +import 'app_locale.dart'; + +// ignore_for_file: type=lint + +/// The translations for Russian (`ru`). +class AppLocaleRu extends AppLocale { + AppLocaleRu([String locale = 'ru']) : super(locale); + + @override + String get search => 'Поиск'; + + @override + String get liked => 'понравился!'; + + @override + String get disliked => 'разонравился :('; + + @override + String get arbEnding => 'Чтобы не забыть про отсутствие запятой :)'; +} diff --git a/lib/presentation/home_page/bloc/state.g.dart b/lib/presentation/home_page/bloc/state.g.dart index 114ac25..cd19bc1 100644 --- a/lib/presentation/home_page/bloc/state.g.dart +++ b/lib/presentation/home_page/bloc/state.g.dart @@ -23,8 +23,8 @@ abstract class _$HomeStateCWProxy { /// ```` HomeState call({ HomeData? data, - bool? isLoading, - bool? isPaginationLoading, + bool isLoading, + bool isPaginationLoading, String? error, }); } @@ -67,16 +67,14 @@ class _$HomeStateCWProxyImpl implements _$HomeStateCWProxy { ? _value.data // ignore: cast_nullable_to_non_nullable : data as HomeData?, - isLoading: isLoading == const $CopyWithPlaceholder() || isLoading == null + isLoading: isLoading == const $CopyWithPlaceholder() ? _value.isLoading // ignore: cast_nullable_to_non_nullable : isLoading as bool, - isPaginationLoading: - isPaginationLoading == const $CopyWithPlaceholder() || - isPaginationLoading == null - ? _value.isPaginationLoading - // ignore: cast_nullable_to_non_nullable - : isPaginationLoading as bool, + isPaginationLoading: isPaginationLoading == const $CopyWithPlaceholder() + ? _value.isPaginationLoading + // ignore: cast_nullable_to_non_nullable + : isPaginationLoading as bool, error: error == const $CopyWithPlaceholder() ? _value.error // ignore: cast_nullable_to_non_nullable diff --git a/lib/presentation/like/bloc.dart b/lib/presentation/like/bloc.dart new file mode 100644 index 0000000..e69de29 diff --git a/lib/presentation/like/events.dart b/lib/presentation/like/events.dart new file mode 100644 index 0000000..e69de29 diff --git a/lib/presentation/like/state.dart b/lib/presentation/like/state.dart new file mode 100644 index 0000000..e69de29 diff --git a/lib/presentation/locale/bloc.dart b/lib/presentation/locale/bloc.dart new file mode 100644 index 0000000..1cc8ed7 --- /dev/null +++ b/lib/presentation/locale/bloc.dart @@ -0,0 +1,19 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:pmu/components/locale/l10n/app_locale.dart'; +import 'package:pmu/presentation/locale/events.dart'; +import 'package:pmu/presentation/locale/state.dart'; + +class LocaleBloc extends Bloc { + LocaleBloc(Locale defaultLocale) + : super(LocaleState(currentLocale: defaultLocale)) { + on(_onChangeLocale); + } + + Future _onChangeLocale( + ChangeLocaleEvent event, Emitter emit) async { + final toChange = AppLocale.supportedLocales + .firstWhere((e) => e.languageCode != state.currentLocale.languageCode); + emit(state.copyWith(currentLocale: toChange)); + } +} diff --git a/lib/presentation/locale/events.dart b/lib/presentation/locale/events.dart new file mode 100644 index 0000000..c08cd1b --- /dev/null +++ b/lib/presentation/locale/events.dart @@ -0,0 +1,7 @@ +abstract class LocaleEvent { + const LocaleEvent(); +} + +class ChangeLocaleEvent extends LocaleEvent { + const ChangeLocaleEvent(); +} diff --git a/lib/presentation/locale/state.dart b/lib/presentation/locale/state.dart new file mode 100644 index 0000000..523c20c --- /dev/null +++ b/lib/presentation/locale/state.dart @@ -0,0 +1,16 @@ +import 'dart:ui'; + +import 'package:copy_with_extension/copy_with_extension.dart'; +import 'package:equatable/equatable.dart'; + +part 'state.g.dart'; + +@CopyWith() +class LocaleState extends Equatable { + final Locale currentLocale; + + const LocaleState({required this.currentLocale}); + + @override + List get props => [currentLocale]; +} diff --git a/lib/presentation/locale/state.g.dart b/lib/presentation/locale/state.g.dart new file mode 100644 index 0000000..cc063be --- /dev/null +++ b/lib/presentation/locale/state.g.dart @@ -0,0 +1,57 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'state.dart'; + +// ************************************************************************** +// CopyWithGenerator +// ************************************************************************** + +abstract class _$LocaleStateCWProxy { + LocaleState currentLocale(Locale currentLocale); + + /// This function **does support** nullification of nullable fields. All `null` values passed to `non-nullable` fields will be ignored. You can also use `LocaleState(...).copyWith.fieldName(...)` to override fields one at a time with nullification support. + /// + /// Usage + /// ```dart + /// LocaleState(...).copyWith(id: 12, name: "My name") + /// ```` + LocaleState call({ + Locale currentLocale, + }); +} + +/// Proxy class for `copyWith` functionality. This is a callable class and can be used as follows: `instanceOfLocaleState.copyWith(...)`. Additionally contains functions for specific fields e.g. `instanceOfLocaleState.copyWith.fieldName(...)` +class _$LocaleStateCWProxyImpl implements _$LocaleStateCWProxy { + const _$LocaleStateCWProxyImpl(this._value); + + final LocaleState _value; + + @override + LocaleState currentLocale(Locale currentLocale) => + this(currentLocale: currentLocale); + + @override + + /// This function **does support** nullification of nullable fields. All `null` values passed to `non-nullable` fields will be ignored. You can also use `LocaleState(...).copyWith.fieldName(...)` to override fields one at a time with nullification support. + /// + /// Usage + /// ```dart + /// LocaleState(...).copyWith(id: 12, name: "My name") + /// ```` + LocaleState call({ + Object? currentLocale = const $CopyWithPlaceholder(), + }) { + return LocaleState( + currentLocale: currentLocale == const $CopyWithPlaceholder() + ? _value.currentLocale + // ignore: cast_nullable_to_non_nullable + : currentLocale as Locale, + ); + } +} + +extension $LocaleStateCopyWith on LocaleState { + /// Returns a callable class that can be used as follows: `instanceOfLocaleState.copyWith(...)` or like so:`instanceOfLocaleState.copyWith.fieldName(...)`. + // ignore: library_private_types_in_public_api + _$LocaleStateCWProxy get copyWith => _$LocaleStateCWProxyImpl(this); +}