| 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. |
| 11 * | 11 * |
| 12 * This is intended to test the basic functioning of extracting messages and | 12 * This is intended to test the basic functioning of extracting messages and |
| 13 * serve as an example for how a program to extract them to a translation | 13 * serve as an example for how a program to extract them to a translation |
| 14 * file format could work. In the tests, this file is then run through a | 14 * file format could work. In the tests, this file is then run through a |
| 15 * simulated translation and the results of that are used to generate code. See | 15 * simulated translation and the results of that are used to generate code. See |
| 16 * message_extraction_test.dart | 16 * message_extraction_test.dart |
| 17 * | 17 * |
| 18 * If the environment variable INTL_MESSAGE_OUTPUT is set then it will use | 18 * If the environment variable INTL_MESSAGE_OUTPUT is set then it will use |
| 19 * that as the output directory, otherwise it will use the working directory. | 19 * that as the output directory, otherwise it will use the working directory. |
| 20 */ | 20 */ |
| 21 library extract_to_json; | 21 library extract_to_json; |
| 22 | 22 |
| 23 import 'dart:io'; | 23 import 'dart:io'; |
| 24 import 'package:intl/extract_messages.dart'; | 24 import 'package:intl/extract_messages.dart'; |
| 25 import 'dart:json' as json; | 25 import 'dart:json' as json; |
| 26 import 'package:pathos/path.dart' as path; | 26 import 'package:pathos/path.dart' as path; |
| 27 import 'package:intl/src/intl_message.dart'; | 27 import 'package:intl/src/intl_message.dart'; |
| 28 import 'find_output_directory.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; |
| 33 var parser = new ArgParser(); |
| 34 parser.addFlag("suppress-warnings", defaultsTo: false, |
| 35 callback: (x) => suppressWarnings = x); |
| 36 |
| 37 void setTargetDir(value) => targetDir = value; |
| 38 |
| 39 parser.addOption("output-dir", defaultsTo: '.', callback: setTargetDir); |
| 40 parser.parse(args); |
| 32 if (args.length == 0) { | 41 if (args.length == 0) { |
| 33 print('Usage: extract_to_json [--output-dir=<dir>] [files.dart]'); | 42 print('Usage: extract_to_json [--output-dir=<dir>] [files.dart]'); |
| 34 print('Accepts Dart files and produces intl_messages.json'); | 43 print('Accepts Dart files and produces intl_messages.json'); |
| 35 exit(0); | 44 exit(0); |
| 36 } | 45 } |
| 37 var allMessages = []; | 46 var allMessages = []; |
| 38 for (var arg in args.where((x) => x.contains(".dart"))) { | 47 for (var arg in args.where((x) => x.contains(".dart"))) { |
| 39 var messages = parseFile(new File(arg)); | 48 var messages = parseFile(new File(arg)); |
| 40 messages.forEach((k, v) => allMessages.add(toJson(v))); | 49 messages.forEach((k, v) => allMessages.add(toJson(v))); |
| 41 } | 50 } |
| 42 var targetDir = findOutputDirectory(args); | |
| 43 var file = new File(path.join(targetDir, 'intl_messages.json')); | 51 var file = new File(path.join(targetDir, 'intl_messages.json')); |
| 44 file.writeAsStringSync(json.stringify(allMessages)); | 52 file.writeAsStringSync(json.stringify(allMessages)); |
| 45 } | 53 } |
| 46 | 54 |
| 47 /** | 55 /** |
| 48 * This is a placeholder for transforming a parameter substitution from | 56 * This is a placeholder for transforming a parameter substitution from |
| 49 * the translation file format into a Dart interpolation. In our case we | 57 * the translation file format into a Dart interpolation. In our case we |
| 50 * store it to the file in Dart interpolation syntax, so the transformation | 58 * store it to the file in Dart interpolation syntax, so the transformation |
| 51 * is trivial. | 59 * is trivial. |
| 52 */ | 60 */ |
| 53 String leaveTheInterpolationsInDartForm(IntlMessage msg, chunk) => | 61 String leaveTheInterpolationsInDartForm(IntlMessage msg, chunk) => |
| 54 (chunk is String) ? chunk : "\$${msg.arguments[chunk]}"; | 62 (chunk is String) ? chunk : "\$${msg.arguments[chunk]}"; |
| 55 | 63 |
| 56 /** | 64 /** |
| 57 * Convert the [IntlMessage] to a trivial JSON format. | 65 * Convert the [IntlMessage] to a trivial JSON format. |
| 58 */ | 66 */ |
| 59 Map toJson(IntlMessage message) { | 67 Map toJson(IntlMessage message) { |
| 60 return { | 68 return { |
| 61 "name" : message.name, | 69 "name" : message.name, |
| 62 "description" : message.description, | 70 "description" : message.description, |
| 63 "message" : message.fullMessage(leaveTheInterpolationsInDartForm), | 71 "message" : message.fullMessage(leaveTheInterpolationsInDartForm), |
| 64 "examples" : message.examples, | 72 "examples" : message.examples, |
| 65 "arguments" : message.arguments | 73 "arguments" : message.arguments |
| 66 }; | 74 }; |
| 67 } | 75 } |
| OLD | NEW |