|
| 1 | +#!/usr/bin/env dart |
| 2 | +// Copyright (c) 2014, 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 | +/// This script uses the extract_messages.dart library to find the Intl.message |
| 7 | +/// calls in the target dart files and produces ARB format output. See |
| 8 | +/// https://code.google.com/p/arb/wiki/ApplicationResourceBundleSpecification |
| 9 | +library extract_to_arb; |
| 10 | + |
| 11 | +import 'dart:convert'; |
| 12 | +import 'dart:io'; |
| 13 | + |
| 14 | +import 'package:args/args.dart'; |
| 15 | +import 'package:path/path.dart' as path; |
| 16 | + |
| 17 | +import 'package:intl_translation/extract_messages.dart'; |
| 18 | +import 'package:intl_translation/src/intl_message.dart'; |
| 19 | + |
| 20 | +var outputFilename = 'intl_messages.arb'; |
| 21 | + |
| 22 | +main(List<String> args) { |
| 23 | + var targetDir; |
| 24 | + bool transformer; |
| 25 | + var parser = new ArgParser(); |
| 26 | + var extraction = new MessageExtraction(); |
| 27 | + parser.addFlag("suppress-warnings", |
| 28 | + defaultsTo: false, |
| 29 | + callback: (x) => extraction.suppressWarnings = x, |
| 30 | + help: 'Suppress printing of warnings.'); |
| 31 | + parser.addFlag("warnings-are-errors", |
| 32 | + defaultsTo: false, |
| 33 | + callback: (x) => extraction.warningsAreErrors = x, |
| 34 | + help: 'Treat all warnings as errors, stop processing '); |
| 35 | + parser.addFlag("embedded-plurals", |
| 36 | + defaultsTo: true, |
| 37 | + callback: (x) => extraction.allowEmbeddedPluralsAndGenders = x, |
| 38 | + help: 'Allow plurals and genders to be embedded as part of a larger ' |
| 39 | + 'string, otherwise they must be at the top level.'); |
| 40 | + parser.addFlag("transformer", |
| 41 | + defaultsTo: false, |
| 42 | + callback: (x) => transformer = x, |
| 43 | + help: "Assume that the transformer is in use, so name and args " |
| 44 | + "don't need to be specified for messages."); |
| 45 | + |
| 46 | + parser.addOption("output-dir", |
| 47 | + defaultsTo: '.', |
| 48 | + callback: (value) => targetDir = value, |
| 49 | + help: 'Specify the output directory.'); |
| 50 | + parser.parse(args); |
| 51 | + if (args.length == 0) { |
| 52 | + print('Accepts Dart files and produces $outputFilename'); |
| 53 | + print('Usage: extract_to_arb [options] [files.dart]'); |
| 54 | + print(parser.usage); |
| 55 | + exit(0); |
| 56 | + } |
| 57 | + var allMessages = {}; |
| 58 | + for (var arg in args.where((x) => x.contains(".dart"))) { |
| 59 | + var messages = extraction.parseFile(new File(arg), transformer); |
| 60 | + messages.forEach((k, v) => allMessages.addAll(toARB(v))); |
| 61 | + } |
| 62 | + var file = new File(path.join(targetDir, outputFilename)); |
| 63 | + file.writeAsStringSync(JSON.encode(allMessages)); |
| 64 | + if (extraction.hasWarnings && extraction.warningsAreErrors) { |
| 65 | + exit(1); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +/// This is a placeholder for transforming a parameter substitution from |
| 70 | +/// the translation file format into a Dart interpolation. In our case we |
| 71 | +/// store it to the file in Dart interpolation syntax, so the transformation |
| 72 | +/// is trivial. |
| 73 | +String leaveTheInterpolationsInDartForm(MainMessage msg, chunk) { |
| 74 | + if (chunk is String) return chunk; |
| 75 | + if (chunk is int) return "\$${msg.arguments[chunk]}"; |
| 76 | + return chunk.toCode(); |
| 77 | +} |
| 78 | + |
| 79 | +/// Convert the [MainMessage] to a trivial JSON format. |
| 80 | +Map toARB(MainMessage message) { |
| 81 | + if (message.messagePieces.isEmpty) return null; |
| 82 | + var out = {}; |
| 83 | + out[message.name] = icuForm(message); |
| 84 | + out["@${message.name}"] = arbMetadata(message); |
| 85 | + return out; |
| 86 | +} |
| 87 | + |
| 88 | +Map arbMetadata(MainMessage message) { |
| 89 | + var out = {}; |
| 90 | + var desc = message.description; |
| 91 | + if (desc != null) { |
| 92 | + out["description"] = desc; |
| 93 | + } |
| 94 | + out["type"] = "text"; |
| 95 | + var placeholders = {}; |
| 96 | + for (var arg in message.arguments) { |
| 97 | + addArgumentFor(message, arg, placeholders); |
| 98 | + } |
| 99 | + out["placeholders"] = placeholders; |
| 100 | + return out; |
| 101 | +} |
| 102 | + |
| 103 | +void addArgumentFor(MainMessage message, String arg, Map result) { |
| 104 | + var extraInfo = {}; |
| 105 | + if (message.examples != null && message.examples[arg] != null) { |
| 106 | + extraInfo["example"] = message.examples[arg]; |
| 107 | + } |
| 108 | + result[arg] = extraInfo; |
| 109 | +} |
| 110 | + |
| 111 | +/// Return a version of the message string with with ICU parameters "{variable}" |
| 112 | +/// rather than Dart interpolations "$variable". |
| 113 | +String icuForm(MainMessage message) => |
| 114 | + message.expanded(turnInterpolationIntoICUForm); |
| 115 | + |
| 116 | +String turnInterpolationIntoICUForm(Message message, chunk, |
| 117 | + {bool shouldEscapeICU: false}) { |
| 118 | + if (chunk is String) { |
| 119 | + return shouldEscapeICU ? escape(chunk) : chunk; |
| 120 | + } |
| 121 | + if (chunk is int && chunk >= 0 && chunk < message.arguments.length) { |
| 122 | + return "{${message.arguments[chunk]}}"; |
| 123 | + } |
| 124 | + if (chunk is SubMessage) { |
| 125 | + return chunk.expanded((message, chunk) => |
| 126 | + turnInterpolationIntoICUForm(message, chunk, shouldEscapeICU: true)); |
| 127 | + } |
| 128 | + if (chunk is Message) { |
| 129 | + return chunk.expanded((message, chunk) => turnInterpolationIntoICUForm( |
| 130 | + message, chunk, |
| 131 | + shouldEscapeICU: shouldEscapeICU)); |
| 132 | + } |
| 133 | + throw new FormatException("Illegal interpolation: $chunk"); |
| 134 | +} |
| 135 | + |
| 136 | +String escape(String s) { |
| 137 | + return s.replaceAll("'", "''").replaceAll("{", "'{'").replaceAll("}", "'}'"); |
| 138 | +} |
0 commit comments