lemmur-app-android/scripts/gen_l10n_from_string.dart

44 lines
1.3 KiB
Dart
Raw Permalink Normal View History

2021-03-01 00:02:32 +01:00
/// creates a file with l10n translations from string
import 'dart:convert';
import 'dart:io';
const baseFile = 'intl_en.arb';
const autoGenHeader = '// FILE GENERATED AUTOMATICALLY, TO NOT EDIT BY HAND';
Future<void> main(List<String> args) async {
final strings = jsonDecode(await File('lib/l10n/$baseFile').readAsString())
as Map<String, dynamic>;
final keys = strings.keys.where((key) => !key.startsWith('@')).toSet();
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>?;
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';
import 'package:flutter_gen/gen_l10n/l10n.dart';
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) {
${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
}