mirror of
https://github.com/krawieck/lemmur/
synced 2024-12-22 21:38:48 +01:00
Create lemmy-translations migration script
This commit is contained in:
parent
734a63e7dd
commit
0445f34250
20
scripts/common.dart
Normal file
20
scripts/common.dart
Normal file
@ -0,0 +1,20 @@
|
||||
import 'dart:io';
|
||||
|
||||
void confirm(String message) {
|
||||
stdout.write('$message [y/n] ');
|
||||
|
||||
switch (stdin.readLineSync()) {
|
||||
case 'y':
|
||||
case 'yes':
|
||||
break;
|
||||
default:
|
||||
print('Exiting');
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
void printError(String message, {bool shouldExit = false}) {
|
||||
stderr.writeln('\x1B[31m$message\x1B[0m');
|
||||
|
||||
if (shouldExit) exit(1);
|
||||
}
|
120
scripts/migrate-lemmy-l10n.dart
Normal file
120
scripts/migrate-lemmy-l10n.dart
Normal file
@ -0,0 +1,120 @@
|
||||
/// migrates chosen strings from lemmy-translations into flutter's i18n solution
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'common.dart';
|
||||
|
||||
/// Map<key, renamedKey>, if `renamedKey` is null then no rename is performed
|
||||
const toExtract = {
|
||||
'settings': null,
|
||||
};
|
||||
|
||||
const repoName = 'lemmy-translations';
|
||||
const baseLanguage = 'en';
|
||||
const flutterIntlPrefix = 'intl_';
|
||||
|
||||
main(List<String> args) async {
|
||||
final force = args.contains('-f') || args.contains('--force');
|
||||
|
||||
final lemmyTranslations = await loadLemmyStrings();
|
||||
final lemmurTranslations = await loadLemmurStrings();
|
||||
portStrings(lemmyTranslations, lemmurTranslations, force: force);
|
||||
|
||||
await save(lemmurTranslations);
|
||||
|
||||
print("Don't forget to format the arb files!");
|
||||
}
|
||||
|
||||
/// returns a cleanup function
|
||||
Future<Future<void> Function()> cloneLemmyTranslations() async {
|
||||
await Process.run('git', ['clone', 'https://github.com/LemmyNet/$repoName']);
|
||||
|
||||
return () => Directory(repoName).delete(recursive: true);
|
||||
}
|
||||
|
||||
/// Map<languageTag, Map<stringKey, stringValue>>
|
||||
Future<Map<String, Map<String, String>>> loadLemmyStrings() async {
|
||||
final translationsDir = Directory('$repoName/translations');
|
||||
final translations = <String, Map<String, String>>{};
|
||||
|
||||
await for (final file in translationsDir.list()) {
|
||||
final transFile = File.fromUri(file.uri);
|
||||
final trans = Map<String, String>.from(
|
||||
jsonDecode(await transFile.readAsString()) as Map<String, dynamic>,
|
||||
);
|
||||
|
||||
final localeName = file.uri.pathSegments.last.split('.json').first;
|
||||
translations[localeName] = trans;
|
||||
}
|
||||
|
||||
return translations;
|
||||
}
|
||||
|
||||
/// Map<languageTag, Map<stringKey, stringValue>> + some metadata
|
||||
Future<Map<String, Map<String, dynamic>>> loadLemmurStrings() async {
|
||||
final translationsDir = Directory('lib/l10n');
|
||||
final translations = <String, Map<String, dynamic>>{};
|
||||
|
||||
await for (final file in translationsDir.list()) {
|
||||
if (!file.path.endsWith('.arb')) continue;
|
||||
|
||||
final transFile = File.fromUri(file.uri);
|
||||
final trans =
|
||||
jsonDecode(await transFile.readAsString()) as Map<String, dynamic>;
|
||||
|
||||
final localeName = file.uri.pathSegments.last
|
||||
.split('.arb')
|
||||
.first
|
||||
.split(flutterIntlPrefix)
|
||||
.last;
|
||||
translations[localeName] = trans;
|
||||
}
|
||||
|
||||
return translations;
|
||||
}
|
||||
|
||||
/// will port them into `lemmurTranslations`
|
||||
void portStrings(
|
||||
Map<String, Map<String, String>> lemmyTranslations,
|
||||
Map<String, Map<String, dynamic>> lemmurTranslations, {
|
||||
bool force = false,
|
||||
}) {
|
||||
// port all languages
|
||||
for (final language in lemmyTranslations.keys) {
|
||||
if (!lemmurTranslations.containsKey(language)) {
|
||||
lemmurTranslations[language] = {};
|
||||
lemmurTranslations[language]['@@locale'] = language;
|
||||
}
|
||||
}
|
||||
|
||||
for (final extract in toExtract.entries) {
|
||||
final key = extract.key;
|
||||
final renamedKey = extract.value ?? key;
|
||||
|
||||
if (!lemmyTranslations[baseLanguage].containsKey(key)) {
|
||||
printError(
|
||||
'"$key" does not exist in $repoName',
|
||||
shouldExit: true,
|
||||
);
|
||||
}
|
||||
|
||||
if (lemmurTranslations[baseLanguage].containsKey(key) && !force) {
|
||||
confirm('"$key" already exists in lemmur, overwrite?');
|
||||
}
|
||||
|
||||
for (final trans in lemmyTranslations.entries) {
|
||||
final language = trans.key;
|
||||
final strings = trans.value;
|
||||
|
||||
lemmurTranslations[language][renamedKey] = strings[key];
|
||||
}
|
||||
lemmurTranslations[baseLanguage]['@$renamedKey'] = {};
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> save(Map<String, Map<String, dynamic>> lemmurTranslations) async {
|
||||
for (final language in lemmurTranslations.keys) {
|
||||
await File('lib/l10n/$flutterIntlPrefix$language.arb')
|
||||
.writeAsString(jsonEncode(lemmurTranslations[language]));
|
||||
}
|
||||
}
|
@ -1,5 +1,8 @@
|
||||
/// Used to create a new lemmur release. Bumps semver, build number, updates changelog, fastlane, pubspec, and finishes by adding a git tag
|
||||
import 'dart:io';
|
||||
|
||||
import 'common.dart';
|
||||
|
||||
Future<void> main(List<String> args) async {
|
||||
await assertNoStagedGit();
|
||||
|
||||
@ -126,16 +129,3 @@ Future<void> runGitCommands(Version version) async {
|
||||
await Process.run('git', ['tag', 'v${version.toStringNoCode()}']);
|
||||
print('done');
|
||||
}
|
||||
|
||||
void confirm(String message) {
|
||||
stdout.write('$message [y/n] ');
|
||||
|
||||
switch (stdin.readLineSync()) {
|
||||
case 'y':
|
||||
case 'yes':
|
||||
break;
|
||||
default:
|
||||
print('Exiting');
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user