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

Unified Diff: packages/intl/bin/extract_to_arb.dart

Issue 1400473008: Roll Observatory packages and add a roll script (Closed) Base URL: git@github.com:dart-lang/observatory_pub_packages.git@master
Patch Set: Created 5 years, 2 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
« no previous file with comments | « packages/intl/README.md ('k') | packages/intl/bin/generate_from_arb.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: packages/intl/bin/extract_to_arb.dart
diff --git a/packages/intl/bin/extract_to_arb.dart b/packages/intl/bin/extract_to_arb.dart
new file mode 100644
index 0000000000000000000000000000000000000000..a79023b880755a0c63b2ebaa451d7b42d36e381f
--- /dev/null
+++ b/packages/intl/bin/extract_to_arb.dart
@@ -0,0 +1,134 @@
+#!/usr/bin/env dart
+// Copyright (c) 2014, 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 ARB format output. See
+ * https://code.google.com/p/arb/wiki/ApplicationResourceBundleSpecification
+ */
+library extract_to_arb;
+
+import 'dart:convert';
+import 'dart:io';
+import 'package:intl/extract_messages.dart';
+import 'package:path/path.dart' as path;
+import 'package:intl/src/intl_message.dart';
+import 'package:args/args.dart';
+
+main(List<String> args) {
+ var targetDir;
+ var parser = new ArgParser();
+ parser.addFlag("suppress-warnings",
+ defaultsTo: false,
+ callback: (x) => suppressWarnings = x,
+ help: 'Suppress printing of warnings.');
+ parser.addFlag("warnings-are-errors",
+ defaultsTo: false,
+ callback: (x) => warningsAreErrors = x,
+ help: 'Treat all warnings as errors, stop processing ');
+ parser.addFlag("embedded-plurals",
+ defaultsTo: true,
+ callback: (x) => allowEmbeddedPluralsAndGenders = x,
+ help: 'Allow plurals and genders to be embedded as part of a larger '
+ 'string, otherwise they must be at the top level.');
+
+ parser.addOption("output-dir",
+ defaultsTo: '.',
+ callback: (value) => targetDir = value,
+ help: 'Specify the output directory.');
+ parser.parse(args);
+ if (args.length == 0) {
+ print('Accepts Dart files and produces intl_messages.json');
+ print('Usage: extract_to_arb [options] [files.dart]');
+ print(parser.usage);
+ 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.addAll(toARB(v)));
+ }
+ var file = new File(path.join(targetDir, 'intl_messages.arb'));
+ file.writeAsStringSync(JSON.encode(allMessages));
+ if (hasWarnings && warningsAreErrors) {
+ exit(1);
+ }
+}
+
+/**
+ * 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(MainMessage msg, chunk) {
+ if (chunk is String) return chunk;
+ if (chunk is int) return "\$${msg.arguments[chunk]}";
+ return chunk.toCode();
+}
+
+/**
+ * Convert the [MainMessage] to a trivial JSON format.
+ */
+Map toARB(MainMessage message) {
+ if (message.messagePieces.isEmpty) return null;
+ var out = {};
+ out[message.name] = icuForm(message);
+ out["@${message.name}"] = arbMetadata(message);
+ return out;
+}
+
+Map arbMetadata(MainMessage message) {
+ var out = {};
+ var desc = message.description;
+ if (desc != null) {
+ out["description"] = desc;
+ }
+ out["type"] = "text";
+ var placeholders = {};
+ for (var arg in message.arguments) {
+ addArgumentFor(message, arg, placeholders);
+ }
+ out["placeholders"] = placeholders;
+ return out;
+}
+
+void addArgumentFor(MainMessage message, String arg, Map result) {
+ var extraInfo = {};
+ if (message.examples != null && message.examples[arg] != null) {
+ extraInfo["example"] = message.examples[arg];
+ }
+ result[arg] = extraInfo;
+}
+
+/**
+ * Return a version of the message string with
+ * with ICU parameters "{variable}" rather than Dart interpolations "$variable".
+ */
+String icuForm(MainMessage message) =>
+ message.expanded(turnInterpolationIntoICUForm);
+
+String turnInterpolationIntoICUForm(Message message, chunk,
+ {bool shouldEscapeICU: false}) {
+ if (chunk is String) {
+ return shouldEscapeICU ? escape(chunk) : chunk;
+ }
+ if (chunk is int && chunk >= 0 && chunk < message.arguments.length) {
+ return "{${message.arguments[chunk]}}";
+ }
+ if (chunk is SubMessage) {
+ return chunk.expanded((message, chunk) =>
+ turnInterpolationIntoICUForm(message, chunk, shouldEscapeICU: true));
+ }
+ if (chunk is Message) {
+ return chunk.expanded((message, chunk) => turnInterpolationIntoICUForm(
+ message, chunk, shouldEscapeICU: shouldEscapeICU));
+ }
+ throw new FormatException("Illegal interpolation: $chunk");
+}
+
+String escape(String s) {
+ return s.replaceAll("'", "''").replaceAll("{", "'{'").replaceAll("}", "'}'");
+}
« no previous file with comments | « packages/intl/README.md ('k') | packages/intl/bin/generate_from_arb.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698