generateArbFile function

String generateArbFile(
  1. Map<String, List<String>> extractedStrings,
  2. String outputPath, {
  3. String keyFormat = 'camelCase',
})

Generate ARB file from extracted strings.

Creates an Application Resource Bundle (ARB) file from the extracted translatable strings. The function generates meaningful keys for each string and handles key uniqueness automatically.

Parameters:

  • extractedStrings - Map of file paths to lists of extracted strings
  • outputPath - The path where the ARB file will be created
  • keyFormat - Format for generated keys ('snake_case', 'camelCase', etc.)

Returns the path to the generated ARB file.

Example:

final arbPath = generateArbFile(
  extractedStrings,
  'lib/l10n/intl_en.arb',
  keyFormat: 'camelCase'
);

Implementation

String generateArbFile(
    Map<String, List<String>> extractedStrings, String outputPath,
    {String keyFormat = 'camelCase'}) {
  var newStrings = <String, String>{};
  var usedKeys = <String>{};

  for (var entry in extractedStrings.entries) {
    for (var string in entry.value) {
      // Generate a proper key from the string value
      var baseKey = KeyGenerator.generateKey(string, keyFormat: keyFormat);
      var finalKey = KeyGenerator.makeKeyUnique(baseKey, usedKeys);
      usedKeys.add(finalKey);

      // Use the generated key as key and original string as value
      newStrings[finalKey] = string;
      print('🔑 Generated key: "$finalKey" for value: "$string"');
    }
  }

  ArbGenerator.generateOrMerge(
    newStrings: newStrings,
    filePath: outputPath,
    suggestMeaningfulKeys: true,
    keyFormat: keyFormat,
  );
  return outputPath;
}