| OLD | NEW |
| 1 #!/usr/bin/env dart | 1 #!/usr/bin/env dart |
| 2 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 4 // BSD-style license that can be found in the LICENSE file. |
| 5 | 5 |
| 6 /** | 6 /** |
| 7 * A main program that takes as input a source Dart file and a number | 7 * A main program that takes as input a source Dart file and a number |
| 8 * of JSON files representing translations of messages from the corresponding | 8 * of JSON files representing translations of messages from the corresponding |
| 9 * Dart file. See extract_to_json.dart and make_hardcoded_translation.dart. | 9 * Dart file. See extract_to_json.dart and make_hardcoded_translation.dart. |
| 10 * | 10 * |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 import 'package:intl/src/intl_message.dart'; | 21 import 'package:intl/src/intl_message.dart'; |
| 22 import 'package:intl/generate_localized.dart'; | 22 import 'package:intl/generate_localized.dart'; |
| 23 import 'package:path/path.dart' as path; | 23 import 'package:path/path.dart' as path; |
| 24 import 'package:args/args.dart'; | 24 import 'package:args/args.dart'; |
| 25 import 'package:serialization/serialization.dart'; | 25 import 'package:serialization/serialization.dart'; |
| 26 | 26 |
| 27 /** | 27 /** |
| 28 * Keeps track of all the messages we have processed so far, keyed by message | 28 * Keeps track of all the messages we have processed so far, keyed by message |
| 29 * name. | 29 * name. |
| 30 */ | 30 */ |
| 31 Map<String, MainMessage> messages; | 31 Map<String, List<MainMessage>> messages; |
| 32 | 32 |
| 33 main(List<String> args) { | 33 main(List<String> args) { |
| 34 var targetDir; | 34 var targetDir; |
| 35 var parser = new ArgParser(); | 35 var parser = new ArgParser(); |
| 36 parser.addFlag("suppress-warnings", defaultsTo: false, | 36 parser.addFlag("suppress-warnings", defaultsTo: false, |
| 37 callback: (x) => suppressWarnings = x); | 37 callback: (x) => suppressWarnings = x); |
| 38 parser.addOption("output-dir", defaultsTo: '.', | 38 parser.addOption("output-dir", defaultsTo: '.', |
| 39 callback: (x) => targetDir = x); | 39 callback: (x) => targetDir = x); |
| 40 parser.addOption("generated-file-prefix", defaultsTo: '', | 40 parser.addOption("generated-file-prefix", defaultsTo: '', |
| 41 callback: (x) => generatedFilePrefix = x); | 41 callback: (x) => generatedFilePrefix = x); |
| 42 parser.parse(args); | 42 parser.parse(args); |
| 43 var dartFiles = args.where((x) => x.endsWith("dart")).toList(); | 43 var dartFiles = args.where((x) => x.endsWith("dart")).toList(); |
| 44 var jsonFiles = args.where((x) => x.endsWith(".json")).toList(); | 44 var jsonFiles = args.where((x) => x.endsWith(".json")).toList(); |
| 45 if (dartFiles.length == 0 || jsonFiles.length == 0) { | 45 if (dartFiles.length == 0 || jsonFiles.length == 0) { |
| 46 print('Usage: generate_from_json [--output-dir=<dir>]' | 46 print('Usage: generate_from_json [--output-dir=<dir>]' |
| 47 ' [--generated-file-prefix=<prefix>] file1.dart file2.dart ...' | 47 ' [--generated-file-prefix=<prefix>] file1.dart file2.dart ...' |
| 48 ' translation1.json translation2.json ...'); | 48 ' translation1.json translation2.json ...'); |
| 49 exit(0); | 49 exit(0); |
| 50 } | 50 } |
| 51 | 51 |
| 52 // We're re-parsing the original files to find the corresponding messages, | 52 // We're re-parsing the original files to find the corresponding messages, |
| 53 // so if there are warnings extracting the messages, suppress them. | 53 // so if there are warnings extracting the messages, suppress them. |
| 54 suppressWarnings = true; | 54 suppressWarnings = true; |
| 55 var allMessages = dartFiles.map((each) => parseFile(new File(each))); | 55 var allMessages = dartFiles.map((each) => parseFile(new File(each))); |
| 56 | 56 |
| 57 messages = new Map(); | 57 messages = new Map(); |
| 58 for (var eachMap in allMessages) { | 58 for (var eachMap in allMessages) { |
| 59 eachMap.forEach((key, value) => messages[key] = value); | 59 eachMap.forEach((key, value) => |
| 60 messages.putIfAbsent(key, () => []).add(value)); |
| 60 } | 61 } |
| 61 for (var arg in jsonFiles) { | 62 for (var arg in jsonFiles) { |
| 62 var file = new File(arg); | 63 var file = new File(arg); |
| 63 generateLocaleFile(file, targetDir); | 64 generateLocaleFile(file, targetDir); |
| 64 } | 65 } |
| 65 | 66 |
| 66 var mainImportFile = new File(path.join(targetDir, | 67 var mainImportFile = new File(path.join(targetDir, |
| 67 '${generatedFilePrefix}messages_all.dart')); | 68 '${generatedFilePrefix}messages_all.dart')); |
| 68 mainImportFile.writeAsStringSync(generateMainImportFile()); | 69 mainImportFile.writeAsStringSync(generateMainImportFile()); |
| 69 } | 70 } |
| (...skipping 29 matching lines...) Expand all Loading... |
| 99 data.forEach((key, value) { | 100 data.forEach((key, value) { |
| 100 if (key[0] != "_") { | 101 if (key[0] != "_") { |
| 101 translations.add(new BasicTranslatedMessage(key, value)); | 102 translations.add(new BasicTranslatedMessage(key, value)); |
| 102 } | 103 } |
| 103 }); | 104 }); |
| 104 generateIndividualMessageFile(locale, translations, targetDir); | 105 generateIndividualMessageFile(locale, translations, targetDir); |
| 105 } | 106 } |
| 106 | 107 |
| 107 /** | 108 /** |
| 108 * A TranslatedMessage that just uses the name as the id and knows how to look | 109 * A TranslatedMessage that just uses the name as the id and knows how to look |
| 109 * up its original message in our [messages]. | 110 * up its original messages in our [messages]. |
| 110 */ | 111 */ |
| 111 class BasicTranslatedMessage extends TranslatedMessage { | 112 class BasicTranslatedMessage extends TranslatedMessage { |
| 112 BasicTranslatedMessage(String name, translated) : | 113 BasicTranslatedMessage(String name, translated) : |
| 113 super(name, translated); | 114 super(name, translated); |
| 114 | 115 |
| 115 MainMessage get originalMessage => | 116 List<MainMessage> get originalMessages => (super.originalMessages == null) ? |
| 116 (super.originalMessage == null) ? _findOriginal() : super.originalMessage; | 117 _findOriginals() : super.originalMessages; |
| 117 | 118 |
| 118 // We know that our [id] is the name of the message, which is used as the | 119 // We know that our [id] is the name of the message, which is used as the |
| 119 //key in [messages]. | 120 //key in [messages]. |
| 120 MainMessage _findOriginal() => originalMessage = messages[id]; | 121 List<MainMessage> _findOriginals() => originalMessages = messages[id]; |
| 121 } | 122 } |
| OLD | NEW |