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 * |
11 * This produces a series of files named | 11 * This produces a series of files named |
12 * "messages_<locale>.dart" containing messages for a particular locale | 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 | 13 * and a main import file named "messages_all.dart" which has imports all of |
14 * them and provides an initializeMessages function. | 14 * them and provides an initializeMessages function. |
15 */ | 15 */ |
16 library generate_from_json; | 16 library generate_from_json; |
17 | 17 |
| 18 import 'dart:convert'; |
18 import 'dart:io'; | 19 import 'dart:io'; |
19 import 'package:intl/extract_messages.dart'; | 20 import 'package:intl/extract_messages.dart'; |
20 import 'package:intl/src/intl_message.dart'; | 21 import 'package:intl/src/intl_message.dart'; |
21 import 'package:intl/generate_localized.dart'; | 22 import 'package:intl/generate_localized.dart'; |
22 import 'dart:json' as json; | |
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, MainMessage> messages; |
32 | 32 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 } | 84 } |
85 | 85 |
86 /** | 86 /** |
87 * Create the file of generated code for a particular locale. We read the json | 87 * Create the file of generated code for a particular locale. We read the json |
88 * data and create [BasicTranslatedMessage] instances from everything, | 88 * data and create [BasicTranslatedMessage] instances from everything, |
89 * excluding only the special _locale attribute that we use to indicate the | 89 * excluding only the special _locale attribute that we use to indicate the |
90 * locale. | 90 * locale. |
91 */ | 91 */ |
92 void generateLocaleFile(File file, String targetDir) { | 92 void generateLocaleFile(File file, String targetDir) { |
93 var src = file.readAsStringSync(); | 93 var src = file.readAsStringSync(); |
94 var data = json.parse(src); | 94 var data = JSON.decode(src); |
95 data.forEach((k, v) => data[k] = recreateIntlObjects(k, v)); | 95 data.forEach((k, v) => data[k] = recreateIntlObjects(k, v)); |
96 var locale = data["_locale"].string; | 96 var locale = data["_locale"].string; |
97 allLocales.add(locale); | 97 allLocales.add(locale); |
98 | 98 |
99 var translations = []; | 99 var translations = []; |
100 data.forEach((key, value) { | 100 data.forEach((key, value) { |
101 if (key[0] != "_") { | 101 if (key[0] != "_") { |
102 translations.add(new BasicTranslatedMessage(key, value)); | 102 translations.add(new BasicTranslatedMessage(key, value)); |
103 } | 103 } |
104 }); | 104 }); |
105 generateIndividualMessageFile(locale, translations, targetDir); | 105 generateIndividualMessageFile(locale, translations, targetDir); |
106 } | 106 } |
107 | 107 |
108 /** | 108 /** |
109 * 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 |
110 * up its original message in our [messages]. | 110 * up its original message in our [messages]. |
111 */ | 111 */ |
112 class BasicTranslatedMessage extends TranslatedMessage { | 112 class BasicTranslatedMessage extends TranslatedMessage { |
113 BasicTranslatedMessage(String name, translated) : | 113 BasicTranslatedMessage(String name, translated) : |
114 super(name, translated); | 114 super(name, translated); |
115 | 115 |
116 MainMessage get originalMessage => | 116 MainMessage get originalMessage => |
117 (super.originalMessage == null) ? _findOriginal() : super.originalMessage; | 117 (super.originalMessage == null) ? _findOriginal() : super.originalMessage; |
118 | 118 |
119 // 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 |
120 //key in [messages]. | 120 //key in [messages]. |
121 MainMessage _findOriginal() => originalMessage = messages[id]; | 121 MainMessage _findOriginal() => originalMessage = messages[id]; |
122 } | 122 } |
OLD | NEW |