readArbFile static method

Future<Map<String, dynamic>> readArbFile(
  1. String filePath
)

Reads an ARB file from the given filePath and returns its content as a Map.

Performs validation to ensure the file contains valid JSON and follows ARB format conventions.

Example:

final content = await ArbHelper.readArbFile('lib/l10n/app_en.arb');
print(content['appTitle']); // prints the title string

filePath: The file path to the ARB file.

Returns a Future<Map<String, dynamic>> containing the key-value pairs from the ARB file.

Throws ArbFileNotFoundException if the file does not exist. Throws ArbFileFormatException if the file contains invalid JSON. Throws ArbValidationException if the file doesn't follow ARB conventions.

Implementation

static Future<Map<String, dynamic>> readArbFile(String filePath) async {
  final file = File(filePath);
  if (!await file.exists()) {
    throw ArbFileNotFoundException(filePath);
  }

  try {
    final content = await file.readAsString();

    final Map<String, dynamic> arbData;
    try {
      arbData = json.decode(content) as Map<String, dynamic>;
    } catch (e) {
      throw ArbFileFormatException(filePath, 'Invalid JSON format: $e');
    }

    // Validate ARB file structure
    final validationIssues = _validateArbStructure(arbData);
    if (validationIssues.isNotEmpty) {
      throw ArbValidationException(filePath, validationIssues);
    }

    return arbData;
  } catch (e) {
    if (e is ArbException) rethrow;
    throw ArbFileFormatException(filePath, 'Failed to read file: $e');
  }
}