slang-i18n / slang

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

Codegen error when context not included in i18n.json #165

Closed tomwyr closed 1 year ago

tomwyr commented 1 year ago

Consider the following slang setup: strings.i18n.json

{
  "title": "File Format",
  "name(context=FileFormat)": {
    "csv": "CSV",
    "json": "JSON",
    "yaml": "YAML"
  }
}

slang.yaml

contexts:
  FileFormat:
    generate_enum: false

file_format.dart

enum FileFormat {
  csv,
  json,
  yaml,
}

If the name entry is removed from strings.i18n.json, meaning the enum is declared in slang.yaml but never used in the json file, the generator will fail with the following output:

 *  Executing task: dart run slang 

Generating translations...

Found slang.yaml!

 -> fileType: json
 -> baseLocale: en
 -> fallbackStrategy: none
 -> inputDirectory: null (everywhere)
 -> inputFilePattern: .i18n.json
 -> outputDirectory: null (directory of input)
 -> outputFileName: strings.g.dart
 -> outputFileFormat: singleFile
 -> localeHandling: true
 -> flutterIntegration: true
 -> namespaces: false
 -> translateVar: t
 -> enumName: AppLocale
 -> translationClassVisibility: private
 -> keyCase: null (no change)
 -> keyCase (for maps): null (no change)
 -> paramCase: null (no change)
 -> stringInterpolation: dart
 -> renderFlatMap: true
 -> translationOverrides: false
 -> renderTimestamp: true
 -> maps: []
 -> pluralization/auto: cardinal
 -> pluralization/default_parameter: n
 -> pluralization/cardinal: []
 -> pluralization/ordinal: []
 -> contexts:
    - FileFormat { (inferred) }
 -> interfaces: no interfaces
 -> obfuscation: disabled
 -> imports: []

Scanning translations...

 (base) en -> assets/i18n/string.i18n.json
Unhandled exception:
Null check operator used on a null value
#0      GenerateConfigBuilder.build.<anonymous closure> (package:slang/builder/builder/generate_config_builder.dart:33:35)
#1      MappedListIterable.elementAt (dart:_internal/iterable.dart:415:31)
#2      ListIterator.moveNext (dart:_internal/iterable.dart:344:26)
#3      new _GrowableList._ofEfficientLengthIterable (dart:core-patch/growable_array.dart:189:27)
#4      new _GrowableList.of (dart:core-patch/growable_array.dart:150:28)
#5      new List.of (dart:core-patch/array_patch.dart:47:28)
#6      ListIterable.toList (dart:_internal/iterable.dart:214:7)
#7      GenerateConfigBuilder.build (package:slang/builder/builder/generate_config_builder.dart:36:10)
#8      GeneratorFacade.generate (package:slang/builder/generator_facade.dart:51:42)
#9      generateTranslations (file:///C:/Users/tomwy/AppData/Local/Pub/Cache/hosted/pub.dev/slang-3.24.0/bin/slang.dart:289:34)
<asynchronous suspension>
#10     main (file:///C:/Users/tomwy/AppData/Local/Pub/Cache/hosted/pub.dev/slang-3.24.0/bin/slang.dart:125:7)
<asynchronous suspension>

The codegen should ignore the fact that an unused context was declared, should warn about it or fail with a more meaningful message.

Tienisto commented 1 year ago

Fixed in v3.25.0