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

Side by Side Diff: pkg/intl/test/message_extraction/generate_from_json.dart

Issue 12733003: Adds facilities for extracting Intl.message calls and generating code from translations (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 9 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 JSON files representing translations of messages from the corresponding
9 * Dart file. See extract_to_json.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_json;
17
18 import 'dart:io';
19 import 'package:intl/extract_messages.dart';
20 import 'package:intl/src/intl_message.dart';
21 import 'extract_to_json.dart';
22 import 'package:intl/generate_localized.dart';
23 import 'dart:json' as json;
24 import 'package:pathos/path.dart' as path;
25
26 /**
27 * Keeps track of all the messages we have processed so far, keyed by message
28 * name.
29 */
30 Map<String, IntlMessage> messages;
31
32 main() {
33 var args = new Options().arguments;
34 if (args.length == 0) {
35 print('Usage: generate_from_json [originalFiles.dart] '
36 '[translationFiles.json]');
37 exit(0);
38 }
39
40 ifNull(x, y) => (x == null) ? y : x;
41 var target = ifNull(Platform.environment["INTL_MESSAGE_OUTPUT"], '.');
42 var isDart = new RegExp(r'\.dart');
43 var isJson = new RegExp(r'\.json');
44 var dartFiles = args.where(isDart.hasMatch).toList();
45 var jsonFiles = args.where(isJson.hasMatch).toList();
46
47 var allMessages = dartFiles.map((each) => parseFile(new File(each)));
48
49 messages = new Map();
50 for (var eachMap in allMessages) {
51 eachMap.forEach((key, value) => messages[key] = value);
52 }
53 for (var arg in jsonFiles) {
54 var file = new File(path.join(target, arg));
55 var translations = generateLocaleFile(new File(arg));
56 }
57
58 var mainImportFile = new File(path.join(target, 'messages_all.dart'));
59 mainImportFile.writeAsStringSync(generateMainImportFile());
60 }
61
62 /**
63 * Create the file of generated code for a particular locale. We read the json
64 * data and create [BasicTranslatedMessage] instances from everything,
65 * excluding only the special _locale attribute that we use to indicate the
66 * locale.
67 */
68 generateLocaleFile(File file) {
69 var src = file.readAsStringSync();
70 var data = json.parse(src);
71 var locale = data["_locale"];
72 allLocales.add(locale);
73
74 var translations = [];
75 data.forEach((key, value) {
76 if (key[0] != "_") {
77 translations.add(new BasicTranslatedMessage(key, value));
78 }
79 });
80 generateIndividualMessageFile(locale, translations);
81 }
82
83 /**
84 * A TranslatedMessage that just uses the name as the id and knows how to look
85 * up its original message in our [messages].
86 */
87 class BasicTranslatedMessage extends TranslatedMessage {
88 BasicTranslatedMessage(String name, String translated) :
89 super(name, translated);
90
91 get originalMessage =>
92 (super.originalMessage == null) ? _findOriginal() : super.originalMessage;
93
94 // We know that our [id] is the name of the message, which is used as the
95 //key in [messages].
96 _findOriginal() => originalMessage = messages[id];
97 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698