addStringToArbFiles function

void addStringToArbFiles(
  1. String value,
  2. String? arabic
)

Implementation

void addStringToArbFiles(String value, String? arabic) {
  final currentDirectory = Directory.current.path;

  // Ensure the command is run from the root directory (ajman_flutter)
  if (!currentDirectory.endsWith('ajman_flutter')) {
    print('This command must be run in the root folder (ajman_flutter)');
    return;
  }

  // Generate camelCase key from the value
  final key = TextUtils.formatAsCamelCase(value);

  // Define the files to update
  final files = {
    'lib/l10n/intl_ar.arb': arabic ?? value, // Use Arabic string if provided
    'lib/l10n/intl_en.arb': value, // Use English string
  };

  bool keyExists = false;

  for (final entry in files.entries) {
    final filePath = entry.key;
    final stringValue = entry.value;

    final file = File(filePath);

    if (!file.existsSync()) {
      print('File not found: $filePath');
      continue;
    }

    try {
      // Read and parse the JSON content
      final content = file.readAsStringSync();
      final Map<String, dynamic> json = jsonDecode(content);

      // Check if the key already exists
      if (json.containsKey(key)) {
        keyExists = true;
        print('Key "$key" already exists in $filePath with value: "${json[key]}"');
        continue; // Skip adding the key
      }

      // Add the new string at the end
      json[key] = stringValue;

      // Write back to the file
      file.writeAsStringSync(JsonEncoder.withIndent('  ').convert(json));
      print('Added "$key": "$stringValue" to $filePath');
    } catch (e) {
      print('Failed to update $filePath: $e');
    }
  }
  if (!keyExists) {
    // Trigger the Flutter intl_utils:generate command if changes were made
    runFlutterIntlGenerate();
  }
}