Skip to content

Commit

Permalink
fixes review/13
Browse files Browse the repository at this point in the history
  • Loading branch information
karolgeneral committed Jul 17, 2024
1 parent aa48e04 commit c6da69f
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 168 deletions.
280 changes: 170 additions & 110 deletions lib/ i18n/strings.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,26 @@ const AppLocale _baseLocale = AppLocale.en;
/// - Locale locale = AppLocale.en.flutterLocale // get flutter locale from enum
/// - if (LocaleSettings.currentLocale == AppLocale.en) // locale check
enum AppLocale with BaseAppLocale<AppLocale, Translations> {
en(languageCode: 'en', build: Translations.build);
en(languageCode: 'en', build: Translations.build);

const AppLocale({required this.languageCode, this.scriptCode, this.countryCode, required this.build}); // ignore: unused_element
const AppLocale(
{required this.languageCode,
this.scriptCode,
this.countryCode,
required this.build}); // ignore: unused_element

@override final String languageCode;
@override final String? scriptCode;
@override final String? countryCode;
@override final TranslationBuilder<AppLocale, Translations> build;
@override
final String languageCode;
@override
final String? scriptCode;
@override
final String? countryCode;
@override
final TranslationBuilder<AppLocale, Translations> build;

/// Gets current instance managed by [LocaleSettings].
Translations get translations => LocaleSettings.instance.translationMap[this]!;
/// Gets current instance managed by [LocaleSettings].
Translations get translations =>
LocaleSettings.instance.translationMap[this]!;
}

/// Method A: Simple
Expand Down Expand Up @@ -64,10 +73,14 @@ Translations get t => LocaleSettings.instance.currentTranslations;
/// final t = Translations.of(context); // Get t variable.
/// String a = t.someKey.anotherKey; // Use t variable.
/// String b = t['someKey.anotherKey']; // Only for edge cases!
class TranslationProvider extends BaseTranslationProvider<AppLocale, Translations> {
TranslationProvider({required super.child}) : super(settings: LocaleSettings.instance);
class TranslationProvider
extends BaseTranslationProvider<AppLocale, Translations> {
TranslationProvider({required super.child})
: super(settings: LocaleSettings.instance);

static InheritedLocaleData<AppLocale, Translations> of(BuildContext context) => InheritedLocaleData.of<AppLocale, Translations>(context);
static InheritedLocaleData<AppLocale, Translations> of(
BuildContext context) =>
InheritedLocaleData.of<AppLocale, Translations>(context);
}

/// Method B shorthand via [BuildContext] extension method.
Expand All @@ -76,124 +89,171 @@ class TranslationProvider extends BaseTranslationProvider<AppLocale, Translation
/// Usage (e.g. in a widget's build method):
/// context.t.someKey.anotherKey
extension BuildContextTranslationsExtension on BuildContext {
Translations get t => TranslationProvider.of(this).translations;
Translations get t => TranslationProvider.of(this).translations;
}

/// Manages all translation instances and the current locale
class LocaleSettings extends BaseFlutterLocaleSettings<AppLocale, Translations> {
LocaleSettings._() : super(utils: AppLocaleUtils.instance);

static final instance = LocaleSettings._();

// static aliases (checkout base methods for documentation)
static AppLocale get currentLocale => instance.currentLocale;
static Stream<AppLocale> getLocaleStream() => instance.getLocaleStream();
static AppLocale setLocale(AppLocale locale, {bool? listenToDeviceLocale = false}) => instance.setLocale(locale, listenToDeviceLocale: listenToDeviceLocale);
static AppLocale setLocaleRaw(String rawLocale, {bool? listenToDeviceLocale = false}) => instance.setLocaleRaw(rawLocale, listenToDeviceLocale: listenToDeviceLocale);
static AppLocale useDeviceLocale() => instance.useDeviceLocale();
@Deprecated('Use [AppLocaleUtils.supportedLocales]') static List<Locale> get supportedLocales => instance.supportedLocales;
@Deprecated('Use [AppLocaleUtils.supportedLocalesRaw]') static List<String> get supportedLocalesRaw => instance.supportedLocalesRaw;
static void setPluralResolver({String? language, AppLocale? locale, PluralResolver? cardinalResolver, PluralResolver? ordinalResolver}) => instance.setPluralResolver(
language: language,
locale: locale,
cardinalResolver: cardinalResolver,
ordinalResolver: ordinalResolver,
);
class LocaleSettings
extends BaseFlutterLocaleSettings<AppLocale, Translations> {
LocaleSettings._() : super(utils: AppLocaleUtils.instance);

static final instance = LocaleSettings._();

// static aliases (checkout base methods for documentation)
static AppLocale get currentLocale => instance.currentLocale;
static Stream<AppLocale> getLocaleStream() => instance.getLocaleStream();
static AppLocale setLocale(AppLocale locale,
{bool? listenToDeviceLocale = false}) =>
instance.setLocale(locale, listenToDeviceLocale: listenToDeviceLocale);
static AppLocale setLocaleRaw(String rawLocale,
{bool? listenToDeviceLocale = false}) =>
instance.setLocaleRaw(rawLocale,
listenToDeviceLocale: listenToDeviceLocale);
static AppLocale useDeviceLocale() => instance.useDeviceLocale();
@Deprecated('Use [AppLocaleUtils.supportedLocales]')
static List<Locale> get supportedLocales => instance.supportedLocales;
@Deprecated('Use [AppLocaleUtils.supportedLocalesRaw]')
static List<String> get supportedLocalesRaw => instance.supportedLocalesRaw;
static void setPluralResolver(
{String? language,
AppLocale? locale,
PluralResolver? cardinalResolver,
PluralResolver? ordinalResolver}) =>
instance.setPluralResolver(
language: language,
locale: locale,
cardinalResolver: cardinalResolver,
ordinalResolver: ordinalResolver,
);
}

/// Provides utility functions without any side effects.
class AppLocaleUtils extends BaseAppLocaleUtils<AppLocale, Translations> {
AppLocaleUtils._() : super(baseLocale: _baseLocale, locales: AppLocale.values);
AppLocaleUtils._()
: super(baseLocale: _baseLocale, locales: AppLocale.values);

static final instance = AppLocaleUtils._();
static final instance = AppLocaleUtils._();

// static aliases (checkout base methods for documentation)
static AppLocale parse(String rawLocale) => instance.parse(rawLocale);
static AppLocale parseLocaleParts({required String languageCode, String? scriptCode, String? countryCode}) => instance.parseLocaleParts(languageCode: languageCode, scriptCode: scriptCode, countryCode: countryCode);
static AppLocale findDeviceLocale() => instance.findDeviceLocale();
static List<Locale> get supportedLocales => instance.supportedLocales;
static List<String> get supportedLocalesRaw => instance.supportedLocalesRaw;
// static aliases (checkout base methods for documentation)
static AppLocale parse(String rawLocale) => instance.parse(rawLocale);
static AppLocale parseLocaleParts(
{required String languageCode,
String? scriptCode,
String? countryCode}) =>
instance.parseLocaleParts(
languageCode: languageCode,
scriptCode: scriptCode,
countryCode: countryCode);
static AppLocale findDeviceLocale() => instance.findDeviceLocale();
static List<Locale> get supportedLocales => instance.supportedLocales;
static List<String> get supportedLocalesRaw => instance.supportedLocalesRaw;
}

// translations

// Path: <root>
class Translations implements BaseTranslations<AppLocale, Translations> {
/// Returns the current translations of the given [context].
///
/// Usage:
/// final t = Translations.of(context);
static Translations of(BuildContext context) => InheritedLocaleData.of<AppLocale, Translations>(context).translations;

/// You can call this constructor and build your own translation instance of this locale.
/// Constructing via the enum [AppLocale.build] is preferred.
Translations.build({Map<String, Node>? overrides, PluralResolver? cardinalResolver, PluralResolver? ordinalResolver})
: assert(overrides == null, 'Set "translation_overrides: true" in order to enable this feature.'),
$meta = TranslationMetadata(
locale: AppLocale.en,
overrides: overrides ?? {},
cardinalResolver: cardinalResolver,
ordinalResolver: ordinalResolver,
) {
$meta.setFlatMapFunction(_flatMapFunction);
}

/// Metadata for the translations of <en>.
@override final TranslationMetadata<AppLocale, Translations> $meta;

/// Access flat map
dynamic operator[](String key) => $meta.getTranslation(key);

late final Translations _root = this; // ignore: unused_field

// Translations
String get app_title => 'poly test codes';
String get title1 => 'Barcode List';
String get opis => 'Description';
String get kod => 'Barcode';
String get kodplus => 'Add Barcode';
String get typ => 'Code Type';
String get error1 => 'Description cannot be empty';
String get error2 => 'Barcode cannot be empty';
String get error3 => 'Invalid barcode';
String get codetype => 'Code type';
String get dsc1 => 'Company not verified';
String get dsc2 => 'Company verified with full scores';
String get dsc3 => 'Company verified with incomplete scores';
String get dsc4 => 'Internal Code';
String get dsc5 => 'Company registered outside Poland';
String get dsc6 => 'Company registered in...';
String get dsc7 => 'Lidl\'s own brand';
String get dsc8 => 'Pola\'s Friend';
String get dsc9 => 'Extended company description';
/// Returns the current translations of the given [context].
///
/// Usage:
/// final t = Translations.of(context);
static Translations of(BuildContext context) =>
InheritedLocaleData.of<AppLocale, Translations>(context).translations;

/// You can call this constructor and build your own translation instance of this locale.
/// Constructing via the enum [AppLocale.build] is preferred.
Translations.build(
{Map<String, Node>? overrides,
PluralResolver? cardinalResolver,
PluralResolver? ordinalResolver})
: assert(overrides == null,
'Set "translation_overrides: true" in order to enable this feature.'),
$meta = TranslationMetadata(
locale: AppLocale.en,
overrides: overrides ?? {},
cardinalResolver: cardinalResolver,
ordinalResolver: ordinalResolver,
) {
$meta.setFlatMapFunction(_flatMapFunction);
}

/// Metadata for the translations of <en>.
@override
final TranslationMetadata<AppLocale, Translations> $meta;

/// Access flat map
dynamic operator [](String key) => $meta.getTranslation(key);

late final Translations _root = this; // ignore: unused_field

// Translations
String get app_title => 'poly test codes';
String get title1 => 'Barcode List';
String get opis => 'Description';
String get kod => 'Barcode';
String get kodplus => 'Add Barcode';
String get typ => 'Code Type';
String get error1 => 'Description cannot be empty';
String get error2 => 'Barcode cannot be empty';
String get error3 => 'Invalid barcode';
String get codetype => 'Code type';
String get dsc1 => 'Company not verified';
String get dsc2 => 'Company verified with full scores';
String get dsc3 => 'Company verified with incomplete scores';
String get dsc4 => 'Internal Code';
String get dsc5 => 'Company registered outside Poland';
String get dsc6 => 'Company registered in...';
String get dsc7 => 'Lidl\'s own brand';
String get dsc8 => 'Pola\'s Friend';
String get dsc9 => 'Extended company description';
}

/// Flat map(s) containing all translations.
/// Only for edge cases! For simple maps, use the map function of this library.
extension on Translations {
dynamic _flatMapFunction(String path) {
switch (path) {
case 'app_title': return 'poly test codes';
case 'title1': return 'Barcode List';
case 'opis': return 'Description';
case 'kod': return 'Barcode';
case 'kodplus': return 'Add Barcode';
case 'typ': return 'Code Type';
case 'error1': return 'Description cannot be empty';
case 'error2': return 'Barcode cannot be empty';
case 'error3': return 'Invalid barcode';
case 'codetype': return 'Code type';
case 'dsc1': return 'Company not verified';
case 'dsc2': return 'Company verified with full scores';
case 'dsc3': return 'Company verified with incomplete scores';
case 'dsc4': return 'Internal Code';
case 'dsc5': return 'Company registered outside Poland';
case 'dsc6': return 'Company registered in...';
case 'dsc7': return 'Lidl\'s own brand';
case 'dsc8': return 'Pola\'s Friend';
case 'dsc9': return 'Extended company description';
default: return null;
}
}
dynamic _flatMapFunction(String path) {
switch (path) {
case 'app_title':
return 'poly test codes';
case 'title1':
return 'Barcode List';
case 'opis':
return 'Description';
case 'kod':
return 'Barcode';
case 'kodplus':
return 'Add Barcode';
case 'typ':
return 'Code Type';
case 'error1':
return 'Description cannot be empty';
case 'error2':
return 'Barcode cannot be empty';
case 'error3':
return 'Invalid barcode';
case 'codetype':
return 'Code type';
case 'dsc1':
return 'Company not verified';
case 'dsc2':
return 'Company verified with full scores';
case 'dsc3':
return 'Company verified with incomplete scores';
case 'dsc4':
return 'Internal Code';
case 'dsc5':
return 'Company registered outside Poland';
case 'dsc6':
return 'Company registered in...';
case 'dsc7':
return 'Lidl\'s own brand';
case 'dsc8':
return 'Pola\'s Friend';
case 'dsc9':
return 'Extended company description';
default:
return null;
}
}
}
Loading

0 comments on commit c6da69f

Please sign in to comment.