createLocalesFiles function

void createLocalesFiles(
  1. Map<String, Map<String, String>> localesMap,
  2. Map<String, dynamic> masterMap
)

Generates the json and dart files according to EnvConfig.

Implementation

void createLocalesFiles(
  Map<String, Map<String, String>> localesMap,
  Map<String, dynamic> masterMap,
) {
  var dartExportPaths = <String>[];
  if (config.validTKeyFile) {
    dartExportPaths.add(config.dartTkeysPath);
    createTKeyFileFromMap(masterMap, save: true, includeToString: true);
  }

  /// collect imports for the output.
  var translateImports = <String>[];
  var translateLines = <String>[];
  var saveJsonLocales = config.hasOutputJsonDir;

  if (saveJsonLocales) {
    trace('Saving json locales in ${config.jsonOutputDir}:');
    addAssetsToPubSpec();
  }

  for (var localeKey in localesMap.keys) {
    // trace('Locale ', localeKey);
    var localeName = normLocale(localeKey);
    var localeMap = localesMap[localeKey]!;
    // trace("Locale map: $localeKey / $localeMap");
    /// save json files.
    if (saveJsonLocales) {
      saveLocaleJsonAsset(
        localeName,
        localeMap,
        beautify: true,
      );
    }

    /// save dart file.
    if (config.useDartMaps && config.validTranslationFile) {
      /// Dart translation file.
      var data = prettyJson(localeMap);

      /// cleanup special chars in translated String.
      data = data.replaceAll(r'$', '\\\$');

      var localeInfo = langInfoFromKey(localeName);
      var className = 'Locale${localeName.pascalCase}';
      var fileData = '''
/// Translation for ${localeInfo.englishName} ("${localeInfo.key.toUpperCase()}")
// ignore_for_file: lines_longer_than_80_chars
abstract class $className {
  static const Map<String,String> data = $data;
}
''';
      var fileName = '${localeName.toLowerCase().snakeCase}.dart';
      var filePath = joinDir([config.dartOutputDir, fileName]);
      saveString(filePath, fileData);
      translateImports.add('import "$fileName";');
      translateLines.add('\t\t"$localeName": $className.data,');
    }
  }

  /// create translation and locale file
  if (config.validTranslationFile) {
    createTranslationFile(
      config.locales,
      save: true,
      imports: translateImports,
      translationMaps: translateLines,
    );
    dartExportPaths.add(config.dartTranslationPath);
  }

  if (config.dartOutputFtsUtils) {
    createFtsUtilsFile();
    dartExportPaths.add(config.dartFtsUtilsPath);
  }

  /// create root file export for dartOutput dir.
  if (dartExportPaths.isNotEmpty) {
    /// create export file.
    createDartExportFile(dartExportPaths);
  }
}