| OLD | NEW |
| (Empty) | |
| 1 #!/usr/bin/env dart |
| 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 |
| 4 // BSD-style license that can be found in the LICENSE file. |
| 5 |
| 6 /** |
| 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 |
| 9 * information on those messages. It uses the analyzer-experimental parser |
| 10 * to find the information. |
| 11 * |
| 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 |
| 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 |
| 16 * message_extraction_test.dart |
| 17 * |
| 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. |
| 20 */ |
| 21 library extract_to_json; |
| 22 |
| 23 import 'dart:io'; |
| 24 import 'package:intl/extract_messages.dart'; |
| 25 import 'dart:json' as json; |
| 26 import 'package:pathos/path.dart' as path; |
| 27 import 'package:intl/src/intl_message.dart'; |
| 28 import 'find_output_directory.dart'; |
| 29 |
| 30 main() { |
| 31 var args = new Options().arguments; |
| 32 if (args.length == 0) { |
| 33 print('Usage: extract_to_json [--output-dir=<dir>] [files.dart]'); |
| 34 print('Accepts Dart files and produces intl_messages.json'); |
| 35 exit(0); |
| 36 } |
| 37 var allMessages = []; |
| 38 for (var arg in args.where((x) => x.contains(".dart"))) { |
| 39 var messages = parseFile(new File(arg)); |
| 40 messages.forEach((k, v) => allMessages.add(toJson(v))); |
| 41 } |
| 42 var targetDir = findOutputDirectory(args); |
| 43 var file = new File(path.join(targetDir, 'intl_messages.json')); |
| 44 file.writeAsStringSync(json.stringify(allMessages)); |
| 45 } |
| 46 |
| 47 /** |
| 48 * This is a placeholder for transforming a parameter substitution from |
| 49 * 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 |
| 51 * is trivial. |
| 52 */ |
| 53 String leaveTheInterpolationsInDartForm(IntlMessage msg, chunk) => |
| 54 (chunk is String) ? chunk : "\$${msg.arguments[chunk]}"; |
| 55 |
| 56 /** |
| 57 * Convert the [IntlMessage] to a trivial JSON format. |
| 58 */ |
| 59 Map toJson(IntlMessage message) { |
| 60 return { |
| 61 "name" : message.name, |
| 62 "description" : message.description, |
| 63 "message" : message.fullMessage(leaveTheInterpolationsInDartForm), |
| 64 "examples" : message.examples, |
| 65 "arguments" : message.arguments |
| 66 }; |
| 67 } |
| OLD | NEW |