Skip to content

Commit e7f39b1

Browse files
authored
[Gallery] Add Travis CI check that l10n command has been run for the gallery (#263)
* Add command to grind to verify that l10n command has ben run
1 parent 4d6d566 commit e7f39b1

File tree

12 files changed

+68
-99
lines changed

12 files changed

+68
-99
lines changed

gallery/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ for more details):
6565
flutter pub run grinder l10n
6666
```
6767

68-
To generate code segments (see separate [README](codeviewer_cli/README.md) for
68+
To generate code segments (see separate [README](gallery/tool/codeviewer_cli/README.md) for
6969
more details):
7070
```
7171
flutter pub run grinder update-code-segments

gallery/gallery/tool/grind.dart

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,32 @@ Future<void> generateLocalizations() async {
4747
@Task('Transform arb to xml for English')
4848
@Depends(generateLocalizations)
4949
Future<void> l10n() async {
50-
final l10nPath = path.join(Directory.current.parent.path, 'l10n_cli');
51-
await pubGet(directory: l10nPath);
50+
final l10nPath =
51+
path.join(Directory.current.path, 'tool', 'l10n_cli', 'main.dart');
52+
Dart.run(l10nPath);
53+
}
54+
55+
@Task('Verify xml localizations')
56+
Future<void> verifyL10n() async {
57+
final l10nPath =
58+
path.join(Directory.current.path, 'tool', 'l10n_cli', 'main.dart');
59+
60+
// Run the tool to transform arb to xml, and write the output to stdout.
61+
final xmlOutput = Dart.run(l10nPath, arguments: ['--dry-run'], quiet: true);
62+
63+
// Read the original xml file.
64+
final xmlPath =
65+
path.join(Directory.current.path, 'lib', 'l10n', 'intl_en_US.xml');
66+
final expectedXmlOutput = await File(xmlPath).readAsString();
5267

53-
Dart.run(path.join(l10nPath, 'bin', 'main.dart'));
68+
if (xmlOutput.trim() != expectedXmlOutput.trim()) {
69+
stderr.writeln(
70+
'The contents of $xmlPath are different from that produced by '
71+
'l10n_cli. Did you forget to run `flutter pub run grinder '
72+
'l10n` after updating an .arb file?',
73+
);
74+
exit(1);
75+
}
5476
}
5577

5678
@Task('Update code segments')
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# l10n
2+
3+
A command-line application that converts .arb files to .xml files for
4+
translation consumption.

gallery/l10n_cli/lib/l10n_cli.dart renamed to gallery/gallery/tool/l10n_cli/l10n_cli.dart

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,29 @@ String _escapeXml(String xml) {
3737
}
3838

3939
/// Processes the XML files.
40-
Future<void> englishArbsToXmls() async {
40+
Future<void> englishArbsToXmls({bool isDryRun = false}) async {
41+
IOSink output =
42+
isDryRun ? stdout : File('$_l10nDir/intl_en_US.xml').openWrite();
4143
await generateXmlFromArb(
4244
inputArb: File('$_l10nDir/intl_en_US.arb'),
43-
outputXml: File('$_l10nDir/intl_en_US.xml'),
45+
outputXml: output,
4446
xmlHeader: _intlHeader,
4547
);
48+
await output.close();
4649
}
4750

4851
@visibleForTesting
4952
Future<void> generateXmlFromArb({
5053
File inputArb,
51-
File outputXml,
54+
IOSink outputXml,
5255
String xmlHeader,
5356
}) async {
54-
final Map<String, dynamic> bundle = jsonDecode(await inputArb.readAsString());
57+
final Map<String, dynamic> bundle =
58+
jsonDecode(await inputArb.readAsString()) as Map<String, dynamic>;
59+
5560
String translationFor(String key) {
5661
assert(bundle[key] != null);
57-
return _escapeXml(bundle[key]);
62+
return _escapeXml(bundle[key] as String);
5863
}
5964

6065
final xml = StringBuffer(xmlHeader);
@@ -70,9 +75,9 @@ Future<void> generateXmlFromArb({
7075

7176
final resourceId = key.substring(1);
7277
final name = _escapeXml(resourceId);
73-
final Map<String, dynamic> metaInfo = bundle[key];
78+
final metaInfo = bundle[key] as Map<String, dynamic>;
7479
assert(metaInfo != null && metaInfo['description'] != null);
75-
var description = _escapeXml(metaInfo['description']);
80+
var description = _escapeXml(metaInfo['description'] as String);
7681

7782
if (metaInfo.containsKey('plural')) {
7883
// Generate a plurals resource element formatted like this:
@@ -110,8 +115,8 @@ Future<void> generateXmlFromArb({
110115
// description's 'parameters' value, are replaced with printf positional
111116
// string arguments, like "%1$s".
112117
var translation = translationFor(resourceId);
113-
assert(metaInfo['parameters'].trim().isNotEmpty);
114-
final parameters = metaInfo['parameters']
118+
assert((metaInfo['parameters'] as String).trim().isNotEmpty);
119+
final parameters = (metaInfo['parameters'] as String)
115120
.split(',')
116121
.map<String>((s) => s.trim())
117122
.toList();
@@ -139,6 +144,5 @@ Future<void> generateXmlFromArb({
139144
}
140145
}
141146
xml.writeln('</resources>');
142-
143-
await outputXml.writeAsString(xml.toString());
147+
outputXml.write(xml.toString());
144148
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2019 The Flutter team. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:args/args.dart';
6+
7+
import 'l10n_cli.dart' as l10n_cli;
8+
9+
void main(List<String> arguments) {
10+
final parser = ArgParser()
11+
..addFlag(
12+
'dry-run',
13+
help: 'Write the output to stdout.',
14+
);
15+
final argResults = parser.parse(arguments);
16+
l10n_cli.englishArbsToXmls(isDryRun: argResults['dry-run'] as bool);
17+
}

gallery/l10n_cli/.gitignore

Lines changed: 0 additions & 13 deletions
This file was deleted.

gallery/l10n_cli/CHANGELOG.md

Lines changed: 0 additions & 3 deletions
This file was deleted.

gallery/l10n_cli/README.md

Lines changed: 0 additions & 7 deletions
This file was deleted.

gallery/l10n_cli/analysis_options.yaml

Lines changed: 0 additions & 39 deletions
This file was deleted.

gallery/l10n_cli/bin/main.dart

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)