From 08c4ec1dbcac62b021be3e8770289bb780c76989 Mon Sep 17 00:00:00 2001 From: RN Date: Tue, 21 Feb 2023 12:38:05 +0100 Subject: [PATCH] adding localization mvp --- l10n.yaml | 5 + lib/generated/intl/messages_all.dart | 71 +++++++++++++ lib/generated/intl/messages_de.dart | 37 +++++++ lib/generated/intl/messages_en.dart | 34 ++++++ lib/generated/intl/messages_nl.dart | 37 +++++++ lib/generated/l10n.dart | 140 +++++++++++++++++++++++++ lib/l10n/intl_de.arb | 8 ++ lib/l10n/intl_en.arb | 8 ++ lib/l10n/intl_nl.arb | 8 ++ lib/presentation/core/app_widget.dart | 11 ++ lib/presentation/demo/demo_screen.dart | 6 +- pubspec.lock | 5 + pubspec.yaml | 5 + 13 files changed, 373 insertions(+), 2 deletions(-) create mode 100644 l10n.yaml create mode 100644 lib/generated/intl/messages_all.dart create mode 100644 lib/generated/intl/messages_de.dart create mode 100644 lib/generated/intl/messages_en.dart create mode 100644 lib/generated/intl/messages_nl.dart create mode 100644 lib/generated/l10n.dart create mode 100644 lib/l10n/intl_de.arb create mode 100644 lib/l10n/intl_en.arb create mode 100644 lib/l10n/intl_nl.arb diff --git a/l10n.yaml b/l10n.yaml new file mode 100644 index 00000000..57194ac6 --- /dev/null +++ b/l10n.yaml @@ -0,0 +1,5 @@ +arb-dir: lib/l10n +template-arb-file: intl_en.arb +output-localization-file: app_localizations.dart +class-name: S +main-locale: en \ No newline at end of file diff --git a/lib/generated/intl/messages_all.dart b/lib/generated/intl/messages_all.dart new file mode 100644 index 00000000..a3d30cae --- /dev/null +++ b/lib/generated/intl/messages_all.dart @@ -0,0 +1,71 @@ +// DO NOT EDIT. This is code generated via package:intl/generate_localized.dart +// This is a library that looks up messages for specific locales by +// delegating to the appropriate library. + +// Ignore issues from commonly used lints in this file. +// ignore_for_file:implementation_imports, file_names, unnecessary_new +// ignore_for_file:unnecessary_brace_in_string_interps, directives_ordering +// ignore_for_file:argument_type_not_assignable, invalid_assignment +// ignore_for_file:prefer_single_quotes, prefer_generic_function_type_aliases +// ignore_for_file:comment_references + +import 'dart:async'; + +import 'package:flutter/foundation.dart'; +import 'package:intl/intl.dart'; +import 'package:intl/message_lookup_by_library.dart'; +import 'package:intl/src/intl_helpers.dart'; + +import 'messages_de.dart' as messages_de; +import 'messages_en.dart' as messages_en; +import 'messages_nl.dart' as messages_nl; + +typedef Future LibraryLoader(); +Map _deferredLibraries = { + 'de': () => new SynchronousFuture(null), + 'en': () => new SynchronousFuture(null), + 'nl': () => new SynchronousFuture(null), +}; + +MessageLookupByLibrary? _findExact(String localeName) { + switch (localeName) { + case 'de': + return messages_de.messages; + case 'en': + return messages_en.messages; + case 'nl': + return messages_nl.messages; + default: + return null; + } +} + +/// User programs should call this before using [localeName] for messages. +Future initializeMessages(String localeName) { + var availableLocale = Intl.verifiedLocale( + localeName, (locale) => _deferredLibraries[locale] != null, + onFailure: (_) => null); + if (availableLocale == null) { + return new SynchronousFuture(false); + } + var lib = _deferredLibraries[availableLocale]; + lib == null ? new SynchronousFuture(false) : lib(); + initializeInternalMessageLookup(() => new CompositeMessageLookup()); + messageLookup.addLocale(availableLocale, _findGeneratedMessagesFor); + return new SynchronousFuture(true); +} + +bool _messagesExistFor(String locale) { + try { + return _findExact(locale) != null; + } catch (e) { + return false; + } +} + +MessageLookupByLibrary? _findGeneratedMessagesFor(String locale) { + var actualLocale = + Intl.verifiedLocale(locale, _messagesExistFor, onFailure: (_) => null); + if (actualLocale == null) return null; + return _findExact(actualLocale); +} diff --git a/lib/generated/intl/messages_de.dart b/lib/generated/intl/messages_de.dart new file mode 100644 index 00000000..ecb16615 --- /dev/null +++ b/lib/generated/intl/messages_de.dart @@ -0,0 +1,37 @@ +// DO NOT EDIT. This is code generated via package:intl/generate_localized.dart +// This is a library that provides messages for a de locale. All the +// messages from the main program should be duplicated here with the same +// function name. + +// Ignore issues from commonly used lints in this file. +// ignore_for_file:unnecessary_brace_in_string_interps, unnecessary_new +// ignore_for_file:prefer_single_quotes,comment_references, directives_ordering +// ignore_for_file:annotate_overrides,prefer_generic_function_type_aliases +// ignore_for_file:unused_import, file_names, avoid_escaping_inner_quotes +// ignore_for_file:unnecessary_string_interpolations, unnecessary_string_escapes + +import 'package:intl/intl.dart'; +import 'package:intl/message_lookup_by_library.dart'; + +final messages = new MessageLookup(); + +typedef String MessageIfAbsent(String messageStr, List args); + +class MessageLookup extends MessageLookupByLibrary { + String get localeName => 'de'; + + static String m0(name) => "Welcome in German ${name}"; + + final messages = _notInlinedMessages(_notInlinedMessages); + static Map _notInlinedMessages(_) => { + "demoScreen": + MessageLookupByLibrary.simpleMessage("Demo Screen in German"), + "mainTitle": + MessageLookupByLibrary.simpleMessage("Main Title in German"), + "name": MessageLookupByLibrary.simpleMessage("Name in German"), + "next": MessageLookupByLibrary.simpleMessage("Next in German"), + "pageHomeConfirm": + MessageLookupByLibrary.simpleMessage("Confirm in German"), + "pageHomeWelcome": m0 + }; +} diff --git a/lib/generated/intl/messages_en.dart b/lib/generated/intl/messages_en.dart new file mode 100644 index 00000000..4f79423b --- /dev/null +++ b/lib/generated/intl/messages_en.dart @@ -0,0 +1,34 @@ +// DO NOT EDIT. This is code generated via package:intl/generate_localized.dart +// This is a library that provides messages for a en locale. All the +// messages from the main program should be duplicated here with the same +// function name. + +// Ignore issues from commonly used lints in this file. +// ignore_for_file:unnecessary_brace_in_string_interps, unnecessary_new +// ignore_for_file:prefer_single_quotes,comment_references, directives_ordering +// ignore_for_file:annotate_overrides,prefer_generic_function_type_aliases +// ignore_for_file:unused_import, file_names, avoid_escaping_inner_quotes +// ignore_for_file:unnecessary_string_interpolations, unnecessary_string_escapes + +import 'package:intl/intl.dart'; +import 'package:intl/message_lookup_by_library.dart'; + +final messages = new MessageLookup(); + +typedef String MessageIfAbsent(String messageStr, List args); + +class MessageLookup extends MessageLookupByLibrary { + String get localeName => 'en'; + + static String m0(name) => "Welcome ${name}"; + + final messages = _notInlinedMessages(_notInlinedMessages); + static Map _notInlinedMessages(_) => { + "demoScreen": MessageLookupByLibrary.simpleMessage("Demo Screen"), + "mainTitle": MessageLookupByLibrary.simpleMessage("Coll Action"), + "name": MessageLookupByLibrary.simpleMessage("Name"), + "next": MessageLookupByLibrary.simpleMessage("Next"), + "pageHomeConfirm": MessageLookupByLibrary.simpleMessage("Confirm"), + "pageHomeWelcome": m0 + }; +} diff --git a/lib/generated/intl/messages_nl.dart b/lib/generated/intl/messages_nl.dart new file mode 100644 index 00000000..ee2560b5 --- /dev/null +++ b/lib/generated/intl/messages_nl.dart @@ -0,0 +1,37 @@ +// DO NOT EDIT. This is code generated via package:intl/generate_localized.dart +// This is a library that provides messages for a nl locale. All the +// messages from the main program should be duplicated here with the same +// function name. + +// Ignore issues from commonly used lints in this file. +// ignore_for_file:unnecessary_brace_in_string_interps, unnecessary_new +// ignore_for_file:prefer_single_quotes,comment_references, directives_ordering +// ignore_for_file:annotate_overrides,prefer_generic_function_type_aliases +// ignore_for_file:unused_import, file_names, avoid_escaping_inner_quotes +// ignore_for_file:unnecessary_string_interpolations, unnecessary_string_escapes + +import 'package:intl/intl.dart'; +import 'package:intl/message_lookup_by_library.dart'; + +final messages = new MessageLookup(); + +typedef String MessageIfAbsent(String messageStr, List args); + +class MessageLookup extends MessageLookupByLibrary { + String get localeName => 'nl'; + + static String m0(name) => "Welcome ${name}"; + + final messages = _notInlinedMessages(_notInlinedMessages); + static Map _notInlinedMessages(_) => { + "demoScreen": + MessageLookupByLibrary.simpleMessage("Demo Screen in Holland"), + "mainTitle": + MessageLookupByLibrary.simpleMessage("Coll Action in Holland"), + "name": MessageLookupByLibrary.simpleMessage("Name in Holland"), + "next": MessageLookupByLibrary.simpleMessage("Next in Holland"), + "pageHomeConfirm": + MessageLookupByLibrary.simpleMessage("Confirm in Holland"), + "pageHomeWelcome": m0 + }; +} diff --git a/lib/generated/l10n.dart b/lib/generated/l10n.dart new file mode 100644 index 00000000..239d2a2c --- /dev/null +++ b/lib/generated/l10n.dart @@ -0,0 +1,140 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND +import 'package:flutter/material.dart'; +import 'package:intl/intl.dart'; +import 'intl/messages_all.dart'; + +// ************************************************************************** +// Generator: Flutter Intl IDE plugin +// Made by Localizely +// ************************************************************************** + +// ignore_for_file: non_constant_identifier_names, lines_longer_than_80_chars +// ignore_for_file: join_return_with_assignment, prefer_final_in_for_each +// ignore_for_file: avoid_redundant_argument_values, avoid_escaping_inner_quotes + +class S { + S(); + + static S? _current; + + static S get current { + assert(_current != null, + 'No instance of S was loaded. Try to initialize the S delegate before accessing S.current.'); + return _current!; + } + + static const AppLocalizationDelegate delegate = AppLocalizationDelegate(); + + static Future load(Locale locale) { + final name = (locale.countryCode?.isEmpty ?? false) + ? locale.languageCode + : locale.toString(); + final localeName = Intl.canonicalizedLocale(name); + return initializeMessages(localeName).then((_) { + Intl.defaultLocale = localeName; + final instance = S(); + S._current = instance; + + return instance; + }); + } + + static S of(BuildContext context) { + final instance = S.maybeOf(context); + assert(instance != null, + 'No instance of S present in the widget tree. Did you add S.delegate in localizationsDelegates?'); + return instance!; + } + + static S? maybeOf(BuildContext context) { + return Localizations.of(context, S); + } + + /// `Confirm` + String get pageHomeConfirm { + return Intl.message( + 'Confirm', + name: 'pageHomeConfirm', + desc: '', + args: [], + ); + } + + /// `Welcome {name}` + String pageHomeWelcome(Object name) { + return Intl.message( + 'Welcome $name', + name: 'pageHomeWelcome', + desc: '', + args: [name], + ); + } + + /// `Name` + String get name { + return Intl.message( + 'Name', + name: 'name', + desc: '', + args: [], + ); + } + + /// `Next` + String get next { + return Intl.message( + 'Next', + name: 'next', + desc: '', + args: [], + ); + } + + /// `Coll Action` + String get mainTitle { + return Intl.message( + 'Coll Action', + name: 'mainTitle', + desc: '', + args: [], + ); + } + + /// `Demo Screen` + String get demoScreen { + return Intl.message( + 'Demo Screen', + name: 'demoScreen', + desc: '', + args: [], + ); + } +} + +class AppLocalizationDelegate extends LocalizationsDelegate { + const AppLocalizationDelegate(); + + List get supportedLocales { + return const [ + Locale.fromSubtags(languageCode: 'en'), + Locale.fromSubtags(languageCode: 'de'), + Locale.fromSubtags(languageCode: 'nl'), + ]; + } + + @override + bool isSupported(Locale locale) => _isSupported(locale); + @override + Future load(Locale locale) => S.load(locale); + @override + bool shouldReload(AppLocalizationDelegate old) => false; + + bool _isSupported(Locale locale) { + for (var supportedLocale in supportedLocales) { + if (supportedLocale.languageCode == locale.languageCode) { + return true; + } + } + return false; + } +} diff --git a/lib/l10n/intl_de.arb b/lib/l10n/intl_de.arb new file mode 100644 index 00000000..79651730 --- /dev/null +++ b/lib/l10n/intl_de.arb @@ -0,0 +1,8 @@ +{ + "pageHomeConfirm": "Confirm in German", + "pageHomeWelcome": "Welcome in German {name}", + "name":"Name in German", + "next":"Next in German", + "mainTitle":"Main Title in German", + "demoScreen":"Demo Screen in German" +} \ No newline at end of file diff --git a/lib/l10n/intl_en.arb b/lib/l10n/intl_en.arb new file mode 100644 index 00000000..1be0b5e7 --- /dev/null +++ b/lib/l10n/intl_en.arb @@ -0,0 +1,8 @@ +{ + "pageHomeConfirm": "Confirm", + "pageHomeWelcome": "Welcome {name}", + "name":"Name", + "next":"Next", + "mainTitle":"Coll Action", + "demoScreen":"Demo Screen" +} \ No newline at end of file diff --git a/lib/l10n/intl_nl.arb b/lib/l10n/intl_nl.arb new file mode 100644 index 00000000..90a35bca --- /dev/null +++ b/lib/l10n/intl_nl.arb @@ -0,0 +1,8 @@ +{ + "pageHomeConfirm": "Confirm in Holland", + "pageHomeWelcome": "Welcome {name}", + "name":"Name in Holland", + "next":"Next in Holland", + "mainTitle":"Coll Action in Holland", + "demoScreen":"Demo Screen in Holland" +} \ No newline at end of file diff --git a/lib/presentation/core/app_widget.dart b/lib/presentation/core/app_widget.dart index 9b3d9cdb..481a0ec0 100644 --- a/lib/presentation/core/app_widget.dart +++ b/lib/presentation/core/app_widget.dart @@ -4,9 +4,12 @@ import 'package:flutter_bloc/flutter_bloc.dart'; import '../../application/auth/auth_bloc.dart'; import '../../application/user/profile/profile_bloc.dart'; import '../../application/user/profile_tab/profile_tab_bloc.dart'; +import '../../generated/l10n.dart'; import '../../infrastructure/core/injection.dart'; import '../routes/app_routes.gr.dart'; import '../themes/themes.dart'; +import 'package:flutter_localizations/flutter_localizations.dart'; +import 'package:flutter_gen/gen_l10n/app_localizations.dart'; class AppWidget extends StatelessWidget { final _appRouter = AppRouter(); @@ -32,6 +35,14 @@ class AppWidget extends StatelessWidget { }, child: MaterialApp.router( color: Colors.white, + localizationsDelegates: [ + S.delegate, + GlobalMaterialLocalizations.delegate, + GlobalWidgetsLocalizations.delegate, + GlobalCupertinoLocalizations.delegate, + ], + supportedLocales: S.delegate.supportedLocales, + locale: Locale("nl", "NL"), title: 'CollAction', theme: lightTheme(), routerDelegate: _appRouter.delegate(), diff --git a/lib/presentation/demo/demo_screen.dart b/lib/presentation/demo/demo_screen.dart index 26412a7f..5e5a4c07 100644 --- a/lib/presentation/demo/demo_screen.dart +++ b/lib/presentation/demo/demo_screen.dart @@ -2,10 +2,12 @@ import 'package:auto_route/auto_route.dart'; import 'package:flutter/material.dart'; import '../../domain/core/i_settings_repository.dart'; +import '../../generated/l10n.dart'; import '../../infrastructure/core/injection.dart'; import '../routes/app_routes.gr.dart'; import '../shared_widgets/rectangle_button.dart'; import 'components_demo/current_user_status_text.dart'; +import 'package:flutter_gen/gen_l10n/app_localizations.dart'; class DemoPage extends StatelessWidget { DemoPage({super.key}); @@ -22,8 +24,8 @@ class DemoPage extends StatelessWidget { controller: _pageScrollController, child: Column( children: [ - const Text( - 'Welcome to Demo Screen!', + Text( + S.of(context).mainTitle, style: TextStyle(fontWeight: FontWeight.w700, fontSize: 34.0), textAlign: TextAlign.center, ), diff --git a/pubspec.lock b/pubspec.lock index 04868a93..5870727b 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -461,6 +461,11 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.0.1" + flutter_localizations: + dependency: "direct main" + description: flutter + source: sdk + version: "0.0.0" flutter_oss_licenses: dependency: "direct dev" description: diff --git a/pubspec.yaml b/pubspec.yaml index 93ad1741..c15b99ab 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -36,6 +36,8 @@ dependencies: firebase_crashlytics: ^2.9.0 flutter: sdk: flutter + flutter_localizations: + sdk: flutter flutter_bloc: ^8.1.1 flutter_dotenv: ^5.0.2 freezed_annotation: ^2.2.0 @@ -88,6 +90,7 @@ flutter_icons: flutter: uses-material-design: true + generate: true assets: - .env @@ -111,3 +114,5 @@ flutter: weight: 400 - asset: assets/fonts/Rubik/Rubik-Light.ttf weight: 300 +flutter_intl: + enabled: true