2021-03-01 00:02:32 +01:00
|
|
|
/// creates a file with l10n translations from string
|
|
|
|
import 'dart:convert';
|
|
|
|
import 'dart:io';
|
|
|
|
|
2021-11-16 17:06:07 +01:00
|
|
|
import 'migrate_lemmy_l10n.dart';
|
|
|
|
|
2021-03-01 00:02:32 +01:00
|
|
|
const baseFile = 'intl_en.arb';
|
|
|
|
const autoGenHeader = '// FILE GENERATED AUTOMATICALLY, TO NOT EDIT BY HAND';
|
|
|
|
|
|
|
|
Future<void> main(List<String> args) async {
|
2021-11-16 17:06:07 +01:00
|
|
|
final strings = jsonDecode(await File('$outDir/$baseFile').readAsString())
|
2021-03-01 00:02:32 +01:00
|
|
|
as Map<String, dynamic>;
|
|
|
|
|
|
|
|
final keys = strings.keys.where((key) => !key.startsWith('@')).toSet();
|
2021-03-02 13:18:10 +01:00
|
|
|
final keysWithoutVariables = keys.where((key) {
|
|
|
|
final metadata = strings['@$key'] as Map<String, dynamic>;
|
2021-04-09 11:41:11 +02:00
|
|
|
final placeholders = metadata['placeholders'] as Map<String, dynamic>?;
|
2021-03-02 13:18:10 +01:00
|
|
|
|
|
|
|
return placeholders?.isEmpty ?? true;
|
|
|
|
}).toSet();
|
2021-03-01 00:02:32 +01:00
|
|
|
|
|
|
|
await File('lib/l10n/l10n_from_string.dart').writeAsString('''$autoGenHeader
|
2021-03-01 14:38:44 +01:00
|
|
|
// ignore_for_file: constant_identifier_names
|
2021-03-01 00:02:32 +01:00
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
2021-11-16 17:06:07 +01:00
|
|
|
|
|
|
|
import 'gen/l10n.dart';
|
2021-03-01 00:02:32 +01:00
|
|
|
|
2021-03-01 14:38:44 +01:00
|
|
|
abstract class L10nStrings {
|
|
|
|
${keys.map((key) => " static const $key = '$key';").join('\n')}
|
|
|
|
}
|
|
|
|
|
2021-03-01 00:02:32 +01:00
|
|
|
extension L10nFromString on String {
|
|
|
|
String tr(BuildContext context) {
|
|
|
|
switch (this) {
|
2021-11-16 17:06:07 +01:00
|
|
|
${keysWithoutVariables.map((key) => " case L10nStrings.$key:\n return L10n.of(context).$key;").join('\n')}
|
2021-03-01 00:02:32 +01:00
|
|
|
|
|
|
|
default:
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
''');
|
2021-03-01 14:38:44 +01:00
|
|
|
|
|
|
|
await Process.run('flutter', ['format', '.']);
|
2021-03-01 00:02:32 +01:00
|
|
|
}
|