slang-i18n / slang

Type-safe i18n for Dart and Flutter
https://pub.dev/packages/slang
MIT License
474 stars 39 forks source link

Allow auto-detection of imported enum values #146

Closed Amphaal closed 1 year ago

Amphaal commented 1 year ago

Motivation Since the introduction of enhanced enums in Dart 2.17 (https://dart.dev/language/enums#declaring-enhanced-enums), associating metadata and custom behavior to enums is a common pattern, hence dicouraging using generator tools to generate enums. As Slang allows translations of existing enums via imports / generate_enum, we still need to explicitly list again all enums values in contexts.[existing_enum].enum, which naively seems redundant in cases where we need all enums values to be translated (which, IMO, represents the majority of cases).

Developer Experience Could we consider, instead of:

# Config
imports:
  - 'package:my_package/path_to_enum.dart' # define where your enum is
contexts:
  UserType:
    enum:
      - user
      - admin
    generate_enum: false # turn off enum generation 

... just do:

# Config
imports:
  - 'package:my_package/path_to_enum.dart' # define where your enum is
contexts:
  UserType:
    generate_enum: false # turn off enum generation

... and let slang search for UserType in imported dart files + check for presence of all expected translations based oncontexts.[existing_enum].paths / auto-detected paths ?

Tienisto commented 1 year ago

Parsing the source code will be difficult as slang does not know where the file is located (it may be in another package), but slang could infer the enum values from the translation files

Tienisto commented 1 year ago

Since 3.19.0, you can leave out the enum values.