writeArbFile static method
Writes the given content Map to an ARB file at the specified filePath.
Creates parent directories if they don't exist and optionally creates backup files before overwriting existing files.
Example:
await ArbHelper.writeArbFile(
'lib/l10n/app_fr.arb',
{'appTitle': 'Mon App'},
prettyPrint: true,
createBackup: true,
);
filePath: The file path where the ARB file will be written.
content: A Map containing the key-value pairs to write.
prettyPrint: Whether to format JSON with indentation (default: true).
createBackup: Whether to create a backup before overwriting (default: false).
Returns a Future<void> that completes when the file has been written.
Throws ArbFileWriteException if writing fails. Throws ArbValidationException if content validation fails.
Implementation
static Future<void> writeArbFile(
String filePath,
Map<String, dynamic> content, {
bool prettyPrint = true,
bool createBackup = false,
}) async {
// Validate content before writing
final validationIssues = _validateArbStructure(content);
if (validationIssues.isNotEmpty) {
throw ArbValidationException(filePath, validationIssues);
}
try {
final file = File(filePath);
// Create parent directories if they don't exist
await file.parent.create(recursive: true);
// Create backup if requested and file exists
if (createBackup && await file.exists()) {
await _createBackup(filePath);
}
// Encode JSON
final encoder = prettyPrint
? const JsonEncoder.withIndent(' ')
: const JsonEncoder();
final jsonContent = encoder.convert(content);
// Write file
await file.writeAsString(jsonContent);
} catch (e) {
throw ArbFileWriteException(filePath, e.toString());
}
}