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();
|
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';
|
|
|
|
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) {
|
2021-04-09 00:11:44 +02: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
|
|
|
}
|