| 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 * A main program that takes as input a source Dart file and a number | |
| 8 * of ARB files representing translations of messages from the corresponding | |
| 9 * Dart file. See extract_to_arb.dart and make_hardcoded_translation.dart. | |
| 10 * | |
| 11 * This produces a series of files named | |
| 12 * "messages_<locale>.dart" containing messages for a particular locale | |
| 13 * and a main import file named "messages_all.dart" which has imports all of | |
| 14 * them and provides an initializeMessages function. | |
| 15 */ | |
| 16 library generate_from_arb; | |
| 17 | |
| 18 import 'dart:convert'; | |
| 19 import 'dart:io'; | |
| 20 import 'package:intl/extract_messages.dart'; | |
| 21 import 'package:intl/src/icu_parser.dart'; | |
| 22 import 'package:intl/src/intl_message.dart'; | |
| 23 import 'package:intl/generate_localized.dart'; | |
| 24 import 'package:path/path.dart' as path; | |
| 25 import 'package:args/args.dart'; | |
| 26 | |
| 27 /** | |
| 28 * Keeps track of all the messages we have processed so far, keyed by message | |
| 29 * name. | |
| 30 */ | |
| 31 Map<String, List<MainMessage>> messages; | |
| 32 | |
| 33 main(List<String> args) { | |
| 34 var targetDir; | |
| 35 var parser = new ArgParser(); | |
| 36 parser.addFlag("suppress-warnings", | |
| 37 defaultsTo: false, | |
| 38 callback: (x) => suppressWarnings = x, | |
| 39 help: 'Suppress printing of warnings.'); | |
| 40 parser.addOption("output-dir", | |
| 41 defaultsTo: '.', | |
| 42 callback: (x) => targetDir = x, | |
| 43 help: 'Specify the output directory.'); | |
| 44 parser.addOption("generated-file-prefix", | |
| 45 defaultsTo: '', | |
| 46 callback: (x) => generatedFilePrefix = x, | |
| 47 help: 'Specify a prefix to be used for the generated file names.'); | |
| 48 parser.addFlag("use-deferred-loading", | |
| 49 defaultsTo: true, | |
| 50 callback: (x) => useDeferredLoading = x, | |
| 51 help: 'Generate message code that must be loaded with deferred loading. ' | |
| 52 'Otherwise, all messages are eagerly loaded.'); | |
| 53 parser.parse(args); | |
| 54 var dartFiles = args.where((x) => x.endsWith("dart")).toList(); | |
| 55 var jsonFiles = args.where((x) => x.endsWith(".arb")).toList(); | |
| 56 if (dartFiles.length == 0 || jsonFiles.length == 0) { | |
| 57 print('Usage: generate_from_arb [options]' | |
| 58 ' file1.dart file2.dart ...' | |
| 59 ' translation1_<languageTag>.arb translation2.arb ...'); | |
| 60 print(parser.usage); | |
| 61 exit(0); | |
| 62 } | |
| 63 | |
| 64 // We're re-parsing the original files to find the corresponding messages, | |
| 65 // so if there are warnings extracting the messages, suppress them. | |
| 66 suppressWarnings = true; | |
| 67 var allMessages = dartFiles.map((each) => parseFile(new File(each))); | |
| 68 | |
| 69 messages = new Map(); | |
| 70 for (var eachMap in allMessages) { | |
| 71 eachMap.forEach( | |
| 72 (key, value) => messages.putIfAbsent(key, () => []).add(value)); | |
| 73 } | |
| 74 for (var arg in jsonFiles) { | |
| 75 var file = new File(arg); | |
| 76 generateLocaleFile(file, targetDir); | |
| 77 } | |
| 78 | |
| 79 var mainImportFile = | |
| 80 new File(path.join(targetDir, '${generatedFilePrefix}messages_all.dart')); | |
| 81 mainImportFile.writeAsStringSync(generateMainImportFile()); | |
| 82 } | |
| 83 | |
| 84 /** | |
| 85 * Create the file of generated code for a particular locale. We read the ARB | |
| 86 * data and create [BasicTranslatedMessage] instances from everything, | |
| 87 * excluding only the special _locale attribute that we use to indicate the | |
| 88 * locale. If that attribute is missing, we try to get the locale from the last | |
| 89 * section of the file name. | |
| 90 */ | |
| 91 void generateLocaleFile(File file, String targetDir) { | |
| 92 var src = file.readAsStringSync(); | |
| 93 var data = JSON.decode(src); | |
| 94 data.forEach((k, v) => data[k] = recreateIntlObjects(k, v)); | |
| 95 var locale = data["_locale"]; | |
| 96 if (locale != null) { | |
| 97 locale = locale.translated.string; | |
| 98 } else { | |
| 99 // Get the locale from the end of the file name. This assumes that the file | |
| 100 // name doesn't contain any underscores except to begin the language tag | |
| 101 // and to separate language from country. Otherwise we can't tell if | |
| 102 // my_file_fr.arb is locale "fr" or "file_fr". | |
| 103 var name = path.basenameWithoutExtension(file.path); | |
| 104 locale = name.split("_").skip(1).join("_"); | |
| 105 } | |
| 106 allLocales.add(locale); | |
| 107 | |
| 108 var translations = []; | |
| 109 data.forEach((key, value) { | |
| 110 if (value != null) { | |
| 111 translations.add(value); | |
| 112 } | |
| 113 }); | |
| 114 generateIndividualMessageFile(locale, translations, targetDir); | |
| 115 } | |
| 116 | |
| 117 /** | |
| 118 * Regenerate the original IntlMessage objects from the given [data]. For | |
| 119 * things that are messages, we expect [id] not to start with "@" and | |
| 120 * [data] to be a String. For metadata we expect [id] to start with "@" | |
| 121 * and [data] to be a Map or null. For metadata we return null. | |
| 122 */ | |
| 123 BasicTranslatedMessage recreateIntlObjects(String id, data) { | |
| 124 if (id.startsWith("@")) return null; | |
| 125 if (data == null) return null; | |
| 126 var parsed = pluralAndGenderParser.parse(data).value; | |
| 127 if (parsed is LiteralString && parsed.string.isEmpty) { | |
| 128 parsed = plainParser.parse(data).value; | |
| 129 ; | |
| 130 } | |
| 131 return new BasicTranslatedMessage(id, parsed); | |
| 132 } | |
| 133 | |
| 134 /** | |
| 135 * A TranslatedMessage that just uses the name as the id and knows how to look | |
| 136 * up its original messages in our [messages]. | |
| 137 */ | |
| 138 class BasicTranslatedMessage extends TranslatedMessage { | |
| 139 BasicTranslatedMessage(String name, translated) : super(name, translated); | |
| 140 | |
| 141 List<MainMessage> get originalMessages => (super.originalMessages == null) | |
| 142 ? _findOriginals() | |
| 143 : super.originalMessages; | |
| 144 | |
| 145 // We know that our [id] is the name of the message, which is used as the | |
| 146 //key in [messages]. | |
| 147 List<MainMessage> _findOriginals() => originalMessages = messages[id]; | |
| 148 } | |
| 149 | |
| 150 final pluralAndGenderParser = new IcuParser().message; | |
| 151 final plainParser = new IcuParser().nonIcuMessage; | |
| OLD | NEW |