Last active
March 22, 2024 13:07
-
-
Save paurakhsharma/867a0313d51597decabfd70708a02c56 to your computer and use it in GitHub Desktop.
Auto translate all translation keys into supported target languages for arb file in Flutter app.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:io'; | |
import 'package:http/http.dart' as http; | |
import 'dart:convert'; | |
const String googleTranslateUrl = | |
'https://translation.googleapis.com/language/translate/v2'; | |
Future<String> translateText( | |
String text, String targetLanguage, String apiKey) async { | |
final body = jsonEncode({ | |
'q': text, | |
'target': targetLanguage, | |
}); | |
final response = await http.post( | |
Uri.parse('$googleTranslateUrl?key=$apiKey'), | |
headers: {'Content-Type': 'application/json'}, | |
body: body, | |
); | |
final translation = jsonDecode(response.body); | |
return translation['data']['translations'][0]['translatedText']; | |
} | |
Map<String, dynamic> arbLoadFile(String path) { | |
final file = File(path); | |
final content = file.readAsStringSync(); | |
return jsonDecode(content); | |
} | |
String getTargetLanguage(Map<String, dynamic> locale) { | |
return locale['@@locale']; | |
} | |
void main(List<String> args) async { | |
// Replace with your project details | |
const apiKey = 'YOUR_API_KEY'; | |
// Replace 'lib/l10n' with your actual localization directory path | |
final directory = Directory('lib/l10n'); | |
final englishFile = File('${directory.path}/app_en.arb'); | |
// Parse the English file as reference | |
final englishLocale = arbLoadFile(englishFile.path); | |
// Loop through other locale files (except English) | |
for (var entity in directory.listSync()) { | |
if (entity is File && entity.path != englishFile.path) { | |
if (!entity.path.endsWith('.arb')) { | |
// Skip if not an ARB file | |
continue; | |
} | |
final localeFile = File(entity.path); | |
final locale = arbLoadFile(localeFile.path); | |
final targetLanguage = getTargetLanguage(locale); | |
final result = {}; | |
print('Translating $targetLanguage...'); | |
for (var key in englishLocale.keys) { | |
// if the key is locale use the target language | |
if (key == '@@locale') { | |
result[key] = targetLanguage; | |
continue; | |
} | |
// skip if key is meta key | |
if (key.startsWith('@')) { | |
continue; | |
} | |
// Check for missing keys compared to English | |
if (!locale.containsKey(key)) { | |
final englishValue = englishLocale[key]; | |
final translatedValue = | |
await translateText(englishValue, targetLanguage, apiKey); | |
result[key] = translatedValue; | |
print('Translated $key: $englishValue -> $translatedValue'); | |
} else { | |
result[key] = locale[key]; | |
} | |
} | |
// Write the result to the locale file | |
localeFile.writeAsStringSync(jsonEncode(result)); | |
} | |
} | |
print('Translation completed!'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Make sure you have created
app_<locale>.arb
file with at least{"@@locale": "<locale>"}
for each locale you can to translate.