Line data Source code
1 : import 'package:apptive_grid_form/translation/apptive_grid_translation.dart'; 2 : import 'package:flutter/material.dart'; 3 : 4 : import 'package:apptive_grid_form/translation/l10n/translation_de.dart' as de; 5 : import 'package:apptive_grid_form/translation/l10n/translation_en.dart' as en; 6 : 7 : /// Provides Translations for ApptiveGridForm Widgets 8 : class ApptiveGridLocalization extends StatelessWidget { 9 : /// Creates a wrapper around a Form so that the descendants can use localized Strings 10 4 : const ApptiveGridLocalization({ 11 : Key? key, 12 : required this.child, 13 4 : }) : super(key: key); 14 : 15 : /// The child that should be wrapped 16 : final Widget child; 17 : 18 4 : @override 19 : Widget build(BuildContext context) { 20 4 : return _InheritedApptiveGridTranslation( 21 4 : child: Builder( 22 8 : builder: (_) => child, 23 : ), 24 : ); 25 : } 26 : 27 : /// Returns an [ApptiveGridTranslation] best matching the current App [Locale] 28 : /// 29 : /// If the Locale is not supported it defaults to an english translation 30 4 : static ApptiveGridTranslation? of(BuildContext context) { 31 : final _InheritedApptiveGridTranslation? inheritedTranslation = context 32 4 : .dependOnInheritedWidgetOfExactType<_InheritedApptiveGridTranslation>(); 33 : return inheritedTranslation 34 8 : ?.translation(Localizations.maybeLocaleOf(context)); 35 : } 36 : 37 : /// Checks if given [locale] is supported by its languageCode 38 6 : static bool isSupported(Locale locale) => _isSupported(locale); 39 : 40 3 : static bool _isSupported(Locale locale) { 41 6 : for (final supportedLocale in supportedLocales) { 42 9 : if (supportedLocale.languageCode == locale.languageCode) { 43 : return true; 44 : } 45 : } 46 : return false; 47 : } 48 : 49 : /// List of currently supported locales by ApptiveGridForm 50 3 : static List<Locale> get supportedLocales { 51 : return const <Locale>[ 52 : Locale.fromSubtags(languageCode: 'en'), 53 : Locale.fromSubtags(languageCode: 'de'), 54 : ]; 55 : } 56 : } 57 : 58 : class _InheritedApptiveGridTranslation extends InheritedWidget { 59 4 : _InheritedApptiveGridTranslation({ 60 : Key? key, 61 : required Widget child, 62 4 : }) : super(key: key, child: child) { 63 4 : final defaultTranslations = <Locale, ApptiveGridTranslation>{ 64 : const Locale.fromSubtags(languageCode: 'en'): 65 : const en.ApptiveGridLocalizedTranslation(), 66 : const Locale.fromSubtags(languageCode: 'de'): 67 : const de.ApptiveGridLocalizedTranslation(), 68 : }; 69 8 : _translations.addAll(defaultTranslations); 70 : } 71 : 72 : final Map<Locale, ApptiveGridTranslation> _translations = 73 : <Locale, ApptiveGridTranslation>{}; 74 : 75 4 : ApptiveGridTranslation translation(Locale? locale) { 76 : if (locale != null) { 77 8 : if (_translations.containsKey(locale)) { 78 2 : return _translations[locale]!; 79 3 : } else if (ApptiveGridLocalization.isSupported(locale)) { 80 6 : final translation = _translations[ 81 6 : Locale.fromSubtags(languageCode: locale.languageCode)]; 82 : if (translation != null) { 83 : return translation; 84 : } 85 : } 86 : } 87 2 : return _translations[const Locale.fromSubtags(languageCode: 'en')]!; 88 : } 89 : 90 2 : @override 91 : bool updateShouldNotify(_InheritedApptiveGridTranslation oldWidget) => 92 6 : _translations != oldWidget._translations; 93 : }