Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(145)

Unified Diff: pkg/intl/test/message_extraction/extract_to_json.dart

Issue 12733003: Adds facilities for extracting Intl.message calls and generating code from translations (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fixes from review comments, also made tests more robust, removed scheduled_test dependency Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/intl/test/message_extraction/extract_to_json.dart
diff --git a/pkg/intl/test/message_extraction/extract_to_json.dart b/pkg/intl/test/message_extraction/extract_to_json.dart
new file mode 100644
index 0000000000000000000000000000000000000000..7d719ee246196accead88ab2428ad211ccc15ec9
--- /dev/null
+++ b/pkg/intl/test/message_extraction/extract_to_json.dart
@@ -0,0 +1,67 @@
+#!/usr/bin/env dart
+// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+/**
+ * This script uses the extract_messages.dart library to find the Intl.message
+ * calls in the target dart files and produces intl_messages.json containing the
+ * information on those messages. It uses the analyzer-experimental parser
+ * to find the information.
+ *
+ * This is intended to test the basic functioning of extracting messages and
+ * serve as an example for how a program to extract them to a translation
+ * file format could work. In the tests, this file is then run through a
+ * simulated translation and the results of that are used to generate code. See
+ * message_extraction_test.dart
+ *
+ * If the environment variable INTL_MESSAGE_OUTPUT is set then it will use
+ * that as the output directory, otherwise it will use the working directory.
+ */
+library extract_to_json;
+
+import 'dart:io';
+import 'package:intl/extract_messages.dart';
+import 'dart:json' as json;
+import 'package:pathos/path.dart' as path;
+import 'package:intl/src/intl_message.dart';
+import 'find_output_directory.dart';
+
+main() {
+ var args = new Options().arguments;
+ if (args.length == 0) {
+ print('Usage: extract_to_json [--output-dir=<dir>] [files.dart]');
+ print('Accepts Dart files and produces intl_messages.json');
+ exit(0);
+ }
+ var allMessages = [];
+ for (var arg in args.where((x) => x.contains(".dart"))) {
+ var messages = parseFile(new File(arg));
+ messages.forEach((k, v) => allMessages.add(toJson(v)));
+ }
+ var targetDir = findOutputDirectory(args);
+ var file = new File(path.join(targetDir, 'intl_messages.json'));
+ file.writeAsStringSync(json.stringify(allMessages));
+}
+
+/**
+ * This is a placeholder for transforming a parameter substitution from
+ * the translation file format into a Dart interpolation. In our case we
+ * store it to the file in Dart interpolation syntax, so the transformation
+ * is trivial.
+ */
+String leaveTheInterpolationsInDartForm(IntlMessage msg, chunk) =>
+ (chunk is String) ? chunk : "\$${msg.arguments[chunk]}";
+
+/**
+ * Convert the [IntlMessage] to a trivial JSON format.
+ */
+Map toJson(IntlMessage message) {
+ return {
+ "name" : message.name,
+ "description" : message.description,
+ "message" : message.fullMessage(leaveTheInterpolationsInDartForm),
+ "examples" : message.examples,
+ "arguments" : message.arguments
+ };
+}

Powered by Google App Engine
This is Rietveld 408576698