Skip to content

Instantly share code, notes, and snippets.

@LucasMW
Created February 18, 2022 16:30
Show Gist options
  • Save LucasMW/8561d089e3500d2637ca5963cf4c4db6 to your computer and use it in GitHub Desktop.
Save LucasMW/8561d089e3500d2637ca5963cf4c4db6 to your computer and use it in GitHub Desktop.
translation_script.dart helps generate AppLocalizations for Flutter projects.
import 'dart:convert';
import 'dart:io';
import 'package:csv/csv.dart';
const projectPaTH = "."; //EDIT THIS
const enPath = '$projectPATH/lib/assets/l10n/app_en.arb';
const esPath = '$projectPATH/lib/assets/l10n/app_es.arb';
const ptPath = '$projectPATH/lib/assets/l10n/app_pt.arb';
String toCapitalized(String x) {
return x.length > 0
? '${x[0].toUpperCase()}${x.substring(1).toLowerCase()}'
: '';
}
String toCamelCase(String x) {
final almost = x
.replaceAll(RegExp(' +'), ' ')
.split(' ')
.map((str) => toCapitalized(str))
.join('');
if (almost.length > 2) {
return almost.replaceFirst(almost[0], almost[0].toLowerCase());
}
return almost;
}
String scape(String x) {
return x
.replaceAll("’", " ")
.replaceAll("(", "")
.replaceAll(")", "")
.replaceAll(".", "")
.replaceAll("\"", "\\\"")
.replaceAll("-", " ")
.replaceAll("?", " ")
.replaceAll(",", " ")
.replaceAll("&", "and")
.replaceAll("!", " ")
.replaceAll("/", "slash")
.replaceAll("\\", "slash2")
.replaceAll("%", "percent")
.replaceAll("#", "hash")
.replaceAll(":", "")
.replaceAll("`", " ")
.replaceAll("*", " ")
.replaceAll("‘", " ")
.replaceAll("€", "eur");
}
final reservedWords = [
"continue",
"if",
"int",
"double",
"else",
"void",
"break",
"return"
];
Future<void> main() async {
final input =
File('$HOME/Documents/translation_table.csv').openRead();
final fields = await input
.transform(utf8.decoder)
.transform(new CsvToListConverter())
.toList();
print(fields.length);
print(fields.first.length);
final enOut = File(enPath).openWrite();
final esOut = File(esPath).openWrite();
final ptOut = File(ptPath).openWrite();
print("openFiles");
enOut.writeln("{");
esOut.writeln("{");
ptOut.writeln("{");
List<String> fieldKeys = [];
for (final field in fields) {
final String enString = field[1];
final esString = field[3];
final ptString = field[2];
String keyString = toCamelCase(scape(enString));
print(keyString);
int count = 2;
if (enString.isNotEmpty && enString.length > 2) {
if (!fieldKeys.contains(enString)) {
if (reservedWords.contains(keyString)) {
keyString = "${keyString}x";
}
if (fieldKeys.contains(keyString)) {
keyString = "$keyString$count";
count++;
}
enOut.writeln("\"$keyString\" : \"$enString\",");
esOut.writeln("\"$keyString\" : \"$esString\",");
ptOut.writeln("\"$keyString\" : \"$ptString\",");
fieldKeys.add(enString);
}
}
}
enOut.writeln("\"end\" : \"end\"");
esOut.writeln("\"end\" : \"fin\"");
ptOut.writeln("\"end\" : \"fim\"");
enOut.writeln("}");
esOut.writeln("}");
ptOut.writeln("}");
enOut.close();
esOut.close();
ptOut.close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment