Last active
February 13, 2024 04:20
-
-
Save szymon-mazanik/7e7882856f9e0e0829050d89611fc388 to your computer and use it in GitHub Desktop.
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:convert'; | |
import 'dart:io'; | |
import 'package:csv/csv.dart'; | |
// TODO: replace with your Google sheet | |
const _csvUrl = | |
"https://docs.google.com/spreadsheets/d/1Aw8XtyBUN4tjcEn4cc7dBB2-kHZovj9OAVE17ha4VrI/gviz/tq?tqx=out:csv&sheet=eng_to_es"; | |
const _fileName = "translations.csv"; | |
void main() async { | |
print("Downloading translations..."); | |
final http = HttpClient(); | |
final request = await http.getUrl(Uri.parse(_csvUrl)); | |
final csvFile = File(_fileName); | |
final response = await request.close(); | |
await response.pipe(csvFile.openWrite()); | |
http.close(); | |
print("Downloaded! Converting..."); | |
final languages = <String>[]; | |
final translations = <String, Map<String, dynamic>>{}; | |
final input = csvFile.openRead(); | |
final lines = input.transform(utf8.decoder).transform(const CsvToListConverter(eol: '\n')); | |
var row = 0; | |
await for (final line in lines) { | |
final filteredLine = line; | |
filteredLine.removeWhere((element) => element.isEmpty); | |
if (row == 0) { | |
languages.addAll(filteredLine.sublist(1).map((e) => e.toString())); | |
for (final lang in languages) { | |
translations[lang] = {}; | |
} | |
} else { | |
final key = filteredLine[0]; | |
for (var i = 0; i < filteredLine.length - 1; i++) { | |
final column = i; | |
final value = filteredLine[i + 1]; | |
final lang = languages[column]; | |
translations[lang]![key] = value.trim(); | |
} | |
} | |
row++; | |
} | |
print("Converted! Saving to .arb files..."); | |
for (final lang in languages) { | |
final langFile = File("build/generated/l10n/$lang.arb"); | |
if (await langFile.exists()) { | |
await langFile.delete(recursive: true); | |
} | |
final newLangFile = await langFile.create(recursive: true); | |
final json = jsonEncode(translations[lang]); | |
await newLangFile.writeAsString(json); | |
} | |
print("Saved! Cleaning up..."); | |
await csvFile.delete(recursive: true); | |
print("Cleaned! Updating translations done!"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment