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

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: Fixes from review comments, also made tests more robust, removed scheduled_test dependency 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 import 'find_output_directory.dart';
26
27 /**
28 * Keeps track of all the messages we have processed so far, keyed by message
29 * name.
30 */
31 Map<String, IntlMessage> messages;
32
33 main() {
34 var args = new Options().arguments;
35 if (args.length == 0) {
36 print('Usage: generate_from_json [--output-dir=<dir>] [originalFiles.dart]'
37 ' [translationFiles.json]');
38 exit(0);
39 }
40
41 var targetDir = findOutputDirectory(args);
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(targetDir, arg));
55 var translations = generateLocaleFile(file, targetDir);
56 }
57
58 var mainImportFile = new File(path.join(targetDir, '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 void generateLocaleFile(File file, String targetDir) {
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, targetDir);
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 IntlMessage 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 IntlMessage _findOriginal() => originalMessage = messages[id];
97 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698