createDeclarationFiles function

List<DeclarationFile> createDeclarationFiles({
  1. required List<DeclarationField> fields,
  2. required Map<String, Map<String, String>> locales,
  3. String className = 'L10N',
  4. bool emitProxy = false,
  5. bool emitSupportedLocales = false,
  6. bool emitProxyLoader = false,
  7. bool emitFlutterGlue = false,
})

Implementation

List<DeclarationFile> createDeclarationFiles({
  required List<DeclarationField> fields,
  required Map<String, Map<String, String>> locales,
  String className = 'L10N',
  bool emitProxy = false,
  bool emitSupportedLocales = false,
  bool emitProxyLoader = false,
  bool emitFlutterGlue = false,
}) {
  if (emitProxyLoader) {
    if (!emitProxy) {
      throw Exception(
          'It is necessary to emit proxy to emit the proxy loader.');
    }
    if (!emitSupportedLocales) {
      throw Exception(
          'It is necessary to emit supported locales to emit the proxy loader.');
    }
  }
  if (emitFlutterGlue) {
    if (!emitSupportedLocales) {
      throw Exception(
          'It is necessary to emit supported locales to emit the flutter glue.');
    }
  }

  final declarations = <DeclarationFile>[];

  final parent = _LanguageSuper(
    fileName: '${className.toLowerCase()}.dart',
    className: className,
  );

  final languagesDeclarationsFiles = <String>[];
  String? proxyDeclarationFile;
  final localesClasses = <Locale, String>{};

  if (emitProxy) {
    final proxyDeclaration = _createProxyDeclaration(
      fields,
      parent: parent,
      emitLoader: emitProxyLoader,
      useFlutterIntl: emitFlutterGlue,
    );
    final file = 'proxy_${className.toLowerCase()}.dart';
    proxyDeclarationFile = file;
    declarations.add(DeclarationFile(
      name: file,
      code: proxyDeclaration,
    ));
  }

  // for faster lookup
  final fieldsMap = {for (var field in fields) field.name: field};
  for (final locale in locales.entries) {
    final localeObj = Locale.parse(locale.key);
    final declaration = _createLanguageDeclaration(
      localeObj,
      locale.value,
      fieldsMap,
      parent: parent,
      useFlutterIntl: emitFlutterGlue,
    );

    final file = '${className.toLowerCase()}_${locale.key.toLowerCase()}.dart';
    languagesDeclarationsFiles.add(file);
    localesClasses[localeObj] = declaration.className;
    declarations.add(DeclarationFile(
      name: file,
      code: declaration.code,
    ));
  }

  final superDeclaration = _createSuperDeclaration(
    fields,
    className: parent.className,
    languagesDeclarationsFiles: languagesDeclarationsFiles,
    proxyDeclarationFile: proxyDeclarationFile,
    supportedLocales: emitSupportedLocales ? localesClasses : null,
    emitFlutterGlue: emitFlutterGlue,
  );
  declarations.add(DeclarationFile(
    name: parent.fileName,
    code: superDeclaration,
  ));

  return declarations;
}