| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 /** |
| 6 * This provides utilities for generating localized versions of |
| 7 * messages. It does not stand alone, but expects to be given |
| 8 * TranslatedMessage objects and generate code for a particular locale |
| 9 * based on them. |
| 10 * |
| 11 * An example of usage can be found |
| 12 * in test/message_extract/generate_from_json.dart |
| 13 */ |
| 14 library generate_localized; |
| 15 |
| 16 import 'extract_messages.dart'; |
| 17 import 'src/intl_message.dart'; |
| 18 import 'dart:io'; |
| 19 import 'package:pathos/path.dart' as path; |
| 20 |
| 21 /** |
| 22 * A list of all the locales for which we have translations. Code that does |
| 23 * the reading of translations should add to this. |
| 24 */ |
| 25 List<String> allLocales = []; |
| 26 |
| 27 /** |
| 28 * This represents a message and its translation. We assume that the translation |
| 29 * has some identifier that allows us to figure out the original message it |
| 30 * corresponds to, and that it may want to transform the translated text in |
| 31 * some way, e.g. to turn whatever format the translation uses for variables |
| 32 * into a Dart string interpolation. Specific translation |
| 33 * mechanisms are expected to subclass this. |
| 34 */ |
| 35 abstract class TranslatedMessage { |
| 36 /** The identifier for this message. In the simplest case, this is the name.*/ |
| 37 var id; |
| 38 |
| 39 String translatedString; |
| 40 IntlMessage originalMessage; |
| 41 TranslatedMessage(this.id, this.translatedString); |
| 42 |
| 43 String get message => translatedString; |
| 44 } |
| 45 |
| 46 /** |
| 47 * We can't use a hyphen in a Dart library name, so convert the locale |
| 48 * separator to an underscore. |
| 49 */ |
| 50 String asLibraryName(String x) => x.replaceAll('-', '_'); |
| 51 |
| 52 /** |
| 53 * Generate a file messages_<locale>.dart for the [translations] in |
| 54 * [locale]. |
| 55 */ |
| 56 void generateIndividualMessageFile(String locale, |
| 57 Iterable<TranslatedMessage> translations, String targetDir) { |
| 58 var result = new StringBuffer(); |
| 59 locale = new IntlMessage().escapeAndValidate(locale, locale); |
| 60 result.write(prologue(locale)); |
| 61 for (var each in translations) { |
| 62 var message = each.originalMessage; |
| 63 if (each.message != null) { |
| 64 message.addTranslation(locale, each.message); |
| 65 } |
| 66 } |
| 67 var sorted = translations.where((each) => each.message != null).toList(); |
| 68 sorted.sort((a, b) => |
| 69 a.originalMessage.name.compareTo(b.originalMessage.name)); |
| 70 for (var each in sorted) { |
| 71 result.write(" "); |
| 72 result.write(each.originalMessage.toCode(locale)); |
| 73 result.write("\n\n"); |
| 74 } |
| 75 result.write("\n final messages = const {\n"); |
| 76 var entries = sorted |
| 77 .map((translation) => translation.originalMessage.name) |
| 78 .map((name) => " \"$name\" : $name"); |
| 79 result.write(entries.join(",\n")); |
| 80 result.write("\n };\n}"); |
| 81 |
| 82 var output = new File(path.join(targetDir, "messages_$locale.dart")); |
| 83 output.writeAsStringSync(result.toString()); |
| 84 } |
| 85 |
| 86 /** |
| 87 * This returns the mostly constant string used in [generated] for the |
| 88 * beginning of the file, parameterized by [locale]. |
| 89 */ |
| 90 String prologue(String locale) => """ |
| 91 /** |
| 92 * DO NOT EDIT. This is code generated via pkg/intl/generate_localized.dart |
| 93 * This is a library that provides messages for a $locale locale. All the |
| 94 * messages from the main program should be duplicated here with the same |
| 95 * function name. |
| 96 */ |
| 97 |
| 98 library messages_${locale.replaceAll('-','_')}; |
| 99 import 'package:intl/intl.dart'; |
| 100 import 'package:intl/message_lookup_by_library.dart'; |
| 101 |
| 102 final messages = new MessageLookup(); |
| 103 |
| 104 class MessageLookup extends MessageLookupByLibrary { |
| 105 |
| 106 get localeName => '$locale'; |
| 107 """; |
| 108 |
| 109 /** |
| 110 * This section generates the messages_all.dart file based on the list of |
| 111 * [allLocales]. |
| 112 */ |
| 113 String generateMainImportFile() { |
| 114 var output = new StringBuffer(); |
| 115 output.write(mainPrologue); |
| 116 for (var each in allLocales) { |
| 117 output.write("import 'messages_$each.dart' as ${asLibraryName(each)};\n"); |
| 118 } |
| 119 output.write( |
| 120 "\nMessageLookupByLibrary _findExact(localeName) {\n" |
| 121 " switch (localeName) {\n"); |
| 122 for (var each in allLocales) { |
| 123 output.write( |
| 124 " case '$each' : return ${asLibraryName(each)}.messages;\n"); |
| 125 } |
| 126 output.write(closing); |
| 127 return output.toString(); |
| 128 } |
| 129 |
| 130 /** |
| 131 * Constant string used in [generateMainImportFile] for the beginning of the |
| 132 * file. |
| 133 */ |
| 134 const mainPrologue = """ |
| 135 /** |
| 136 * DO NOT EDIT. This is code generated via pkg/intl/generate_localized.dart |
| 137 * This is a library that looks up messages for specific locales by |
| 138 * delegating to the appropriate library. |
| 139 */ |
| 140 |
| 141 library messages_all; |
| 142 |
| 143 import 'dart:async'; |
| 144 import 'package:intl/message_lookup_by_library.dart'; |
| 145 import 'package:intl/src/intl_helpers.dart'; |
| 146 import 'package:intl/intl.dart'; |
| 147 |
| 148 """; |
| 149 |
| 150 /** |
| 151 * Constant string used in [generateMainImportfile] as the end of the file. |
| 152 */ |
| 153 const closing = """ |
| 154 default: return null; |
| 155 } |
| 156 } |
| 157 |
| 158 /** User programs should call this before using [localeName] for messages.*/ |
| 159 initializeMessages(localeName) { |
| 160 initializeInternalMessageLookup(() => new CompositeMessageLookup()); |
| 161 messageLookup.addLocale(localeName, _findGeneratedMessagesFor); |
| 162 return new Future.immediate(null); |
| 163 } |
| 164 |
| 165 MessageLookupByLibrary _findGeneratedMessagesFor(locale) { |
| 166 var actualLocale = Intl.verifiedLocale(locale, (x) => _findExact(x) != null); |
| 167 if (actualLocale == null) return null; |
| 168 return _findExact(actualLocale); |
| 169 } |
| 170 """; |
| OLD | NEW |