| OLD | NEW |
| 1 #!/usr/bin/env dart | 1 #!/usr/bin/env dart |
| 2 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 2 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 3 // for details. All rights reserved. Use of this source code is governed by a | 3 // for details. All rights reserved. Use of this source code is governed by a |
| 4 // BSD-style license that can be found in the LICENSE file. | 4 // BSD-style license that can be found in the LICENSE file. |
| 5 | 5 |
| 6 /** | 6 /** |
| 7 * This script uses the extract_messages.dart library to find the Intl.message | 7 * This script uses the extract_messages.dart library to find the Intl.message |
| 8 * calls in the target dart files and produces intl_messages.json containing the | 8 * calls in the target dart files and produces intl_messages.json containing the |
| 9 * information on those messages. It uses the analyzer-experimental parser | 9 * information on those messages. It uses the analyzer-experimental parser |
| 10 * to find the information. | 10 * to find the information. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 import 'package:path/path.dart' as path; | 26 import 'package:path/path.dart' as path; |
| 27 import 'package:intl/src/intl_message.dart'; | 27 import 'package:intl/src/intl_message.dart'; |
| 28 import 'package:args/args.dart'; | 28 import 'package:args/args.dart'; |
| 29 | 29 |
| 30 main() { | 30 main() { |
| 31 var args = new Options().arguments; | 31 var args = new Options().arguments; |
| 32 var targetDir; | 32 var targetDir; |
| 33 var parser = new ArgParser(); | 33 var parser = new ArgParser(); |
| 34 parser.addFlag("suppress-warnings", defaultsTo: false, | 34 parser.addFlag("suppress-warnings", defaultsTo: false, |
| 35 callback: (x) => suppressWarnings = x); | 35 callback: (x) => suppressWarnings = x); |
| 36 parser.addFlag("warnings-are-errors", defaultsTo: false, |
| 37 callback: (x) => warningsAreErrors = x); |
| 36 | 38 |
| 37 parser.addOption("output-dir", defaultsTo: '.', | 39 parser.addOption("output-dir", defaultsTo: '.', |
| 38 callback: (value) => targetDir = value); | 40 callback: (value) => targetDir = value); |
| 39 parser.parse(args); | 41 parser.parse(args); |
| 40 if (args.length == 0) { | 42 if (args.length == 0) { |
| 41 print('Usage: extract_to_json [--output-dir=<dir>] [files.dart]'); | 43 print('Usage: extract_to_json [--output-dir=<dir>] [files.dart]'); |
| 42 print('Accepts Dart files and produces intl_messages.json'); | 44 print('Accepts Dart files and produces intl_messages.json'); |
| 43 exit(0); | 45 exit(0); |
| 44 } | 46 } |
| 45 var allMessages = []; | 47 var allMessages = []; |
| 46 for (var arg in args.where((x) => x.contains(".dart"))) { | 48 for (var arg in args.where((x) => x.contains(".dart"))) { |
| 47 var messages = parseFile(new File(arg)); | 49 var messages = parseFile(new File(arg)); |
| 48 messages.forEach((k, v) => allMessages.add(toJson(v))); | 50 messages.forEach((k, v) => allMessages.add(toJson(v))); |
| 49 } | 51 } |
| 50 var file = new File(path.join(targetDir, 'intl_messages.json')); | 52 var file = new File(path.join(targetDir, 'intl_messages.json')); |
| 51 file.writeAsStringSync(json.stringify(allMessages)); | 53 file.writeAsStringSync(json.stringify(allMessages)); |
| 54 if (hasWarnings && warningsAreErrors) { |
| 55 exit(1); |
| 56 } |
| 52 } | 57 } |
| 53 | 58 |
| 54 /** | 59 /** |
| 55 * This is a placeholder for transforming a parameter substitution from | 60 * This is a placeholder for transforming a parameter substitution from |
| 56 * the translation file format into a Dart interpolation. In our case we | 61 * the translation file format into a Dart interpolation. In our case we |
| 57 * store it to the file in Dart interpolation syntax, so the transformation | 62 * store it to the file in Dart interpolation syntax, so the transformation |
| 58 * is trivial. | 63 * is trivial. |
| 59 */ | 64 */ |
| 60 String leaveTheInterpolationsInDartForm(MainMessage msg, chunk) { | 65 String leaveTheInterpolationsInDartForm(MainMessage msg, chunk) { |
| 61 if (chunk is String) return chunk; | 66 if (chunk is String) return chunk; |
| 62 if (chunk is int) return "\$${msg.arguments[chunk]}"; | 67 if (chunk is int) return "\$${msg.arguments[chunk]}"; |
| 63 return chunk.toCode(); | 68 return chunk.toCode(); |
| 64 } | 69 } |
| 65 | 70 |
| 66 /** | 71 /** |
| 67 * Convert the [MainMessage] to a trivial JSON format. | 72 * Convert the [MainMessage] to a trivial JSON format. |
| 68 */ | 73 */ |
| 69 Map toJson(MainMessage message) { | 74 Map toJson(MainMessage message) { |
| 70 var result = new Map<String, Object>(); | 75 var result = new Map<String, Object>(); |
| 71 for (var attribute in message.attributeNames) { | 76 for (var attribute in message.attributeNames) { |
| 72 result[attribute] = message[attribute]; | 77 result[attribute] = message[attribute]; |
| 73 } | 78 } |
| 74 result["message"] = message.expanded(leaveTheInterpolationsInDartForm); | 79 result["message"] = message.expanded(leaveTheInterpolationsInDartForm); |
| 75 return result; | 80 return result; |
| 76 } | 81 } |
| OLD | NEW |