generateArbForLanguage method
Generates a translated ARB file for the specified targetLang
based on the
sourcePath
ARB file.
This method reads the source ARB file, translates all user-facing text entries (preserving metadata), validates the output, and writes a new ARB file for the target language.
Example:
final translator = ArbTranslator(config);
await translator.generateArbForLanguage('lib/l10n/app_en.arb', 'fr');
// Generates lib/l10n/app_fr.arb
sourcePath
: The path to the source ARB file (e.g., 'lib/l10n/app_en.arb').
targetLang
: The ISO-639 language code for the target translation (e.g., 'fr').
overwrite
: Whether to overwrite existing files (default: true).
Returns a Future<String> with the path to the generated file.
Throws ArbFileNotFoundException if source file doesn't exist. Throws ArbFileFormatException if source file has invalid format. Throws TranslationException if translation fails. Throws ArbFileWriteException if output file cannot be written.
Implementation
Future<String> generateArbForLanguage(
String sourcePath,
String targetLang, {
bool overwrite = true,
}) async {
_logger.info('Starting translation: $sourcePath -> $targetLang');
try {
// Read and validate source ARB file
final sourceContent = await ArbHelper.readArbFile(sourcePath);
_logger.debug('Source file loaded with ${sourceContent.length} entries');
// Check if target file exists and handle overwrite
final targetPath = _generateTargetPath(sourcePath, targetLang);
if (!overwrite && await File(targetPath).exists()) {
_logger.warning(
'Target file exists and overwrite is disabled: $targetPath',
);
throw ArbFileWriteException(
targetPath,
'File exists and overwrite is disabled',
);
}
// Extract translations (non-metadata entries)
final translations = ArbHelper.getTranslations(sourceContent);
final metadata = ArbHelper.getMetadata(sourceContent);
if (translations.isEmpty) {
_logger.warning('No translatable content found in source file');
throw ArbValidationException(
sourcePath,
['No translatable content found'],
);
}
_logger.info('Found ${translations.length} entries to translate');
// Perform translations with batch processing
final translatedTexts = <String, String>{};
for (final entry in translations.entries) {
final text = entry.value.toString();
if (text.trim().isNotEmpty) {
translatedTexts[entry.key] = text;
}
}
final translatedResults = await _translationService.translateBatch(
translatedTexts,
targetLang,
);
// Build target content
final targetContent = <String, dynamic>{};
// Add metadata with updated locale
for (final entry in metadata.entries) {
if (entry.key == '@@locale') {
targetContent[entry.key] = targetLang;
} else if (_config.preserveMetadata) {
targetContent[entry.key] = entry.value;
}
}
// Add translations
for (final entry in translations.entries) {
final translatedText = translatedResults[entry.key];
targetContent[entry.key] = translatedText ?? entry.value;
if (translatedText != null) {
_logger.debug('Translated ${entry.key}: "$translatedText"');
}
}
// Write target ARB file
await ArbHelper.writeArbFile(
targetPath,
targetContent,
prettyPrint: _config.prettyPrintJson,
createBackup: _config.backupOriginal,
);
// Validate output if configured
if (_config.validateOutput) {
await _validateGeneratedFile(targetPath);
}
_logger.success('Generated $targetPath successfully!');
return targetPath;
} catch (e) {
_logger.error('Failed to generate ARB file for $targetLang', e);
rethrow;
}
}