OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * This provides utilities for generating localized versions of | 6 * This provides utilities for generating localized versions of |
7 * messages. It does not stand alone, but expects to be given | 7 * messages. It does not stand alone, but expects to be given |
8 * TranslatedMessage objects and generate code for a particular locale | 8 * TranslatedMessage objects and generate code for a particular locale |
9 * based on them. | 9 * based on them. |
10 * | 10 * |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 */ | 46 */ |
47 List<String> allLocales = []; | 47 List<String> allLocales = []; |
48 | 48 |
49 /** | 49 /** |
50 * If we have more than one set of messages to generate in a particular | 50 * If we have more than one set of messages to generate in a particular |
51 * directory we may want to prefix some to distinguish them. | 51 * directory we may want to prefix some to distinguish them. |
52 */ | 52 */ |
53 String generatedFilePrefix = ''; | 53 String generatedFilePrefix = ''; |
54 | 54 |
55 /** | 55 /** |
| 56 * Should we use deferred loading for the generated libraries. |
| 57 */ |
| 58 bool useDeferredLoading = true; |
| 59 |
| 60 /** |
56 * This represents a message and its translation. We assume that the translation | 61 * This represents a message and its translation. We assume that the translation |
57 * has some identifier that allows us to figure out the original message it | 62 * has some identifier that allows us to figure out the original message it |
58 * corresponds to, and that it may want to transform the translated text in | 63 * corresponds to, and that it may want to transform the translated text in |
59 * some way, e.g. to turn whatever format the translation uses for variables | 64 * some way, e.g. to turn whatever format the translation uses for variables |
60 * into a Dart string interpolation. Specific translation | 65 * into a Dart string interpolation. Specific translation |
61 * mechanisms are expected to subclass this. | 66 * mechanisms are expected to subclass this. |
62 */ | 67 */ |
63 abstract class TranslatedMessage { | 68 abstract class TranslatedMessage { |
64 /** | 69 /** |
65 * The identifier for this message. In the simplest case, this is the name | 70 * The identifier for this message. In the simplest case, this is the name |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 /** | 177 /** |
173 * This section generates the messages_all.dart file based on the list of | 178 * This section generates the messages_all.dart file based on the list of |
174 * [allLocales]. | 179 * [allLocales]. |
175 */ | 180 */ |
176 String generateMainImportFile() { | 181 String generateMainImportFile() { |
177 var output = new StringBuffer(); | 182 var output = new StringBuffer(); |
178 output.write(mainPrologue); | 183 output.write(mainPrologue); |
179 for (var locale in allLocales) { | 184 for (var locale in allLocales) { |
180 var baseFile = '${generatedFilePrefix}messages_$locale.dart'; | 185 var baseFile = '${generatedFilePrefix}messages_$locale.dart'; |
181 var file = importForGeneratedFile(baseFile); | 186 var file = importForGeneratedFile(baseFile); |
182 output.write("import '$file' deferred as ${_libraryName(locale)};\n"); | 187 output.write("import '$file' "); |
| 188 if (useDeferredLoading) output.write("deferred "); |
| 189 output.write("as ${_libraryName(locale)};\n"); |
183 } | 190 } |
184 output.write("\n"); | 191 output.write("\n"); |
185 output.write("\nMap<String, Function> _deferredLibraries = {\n"); | 192 output.write("\nMap<String, Function> _deferredLibraries = {\n"); |
186 for (var rawLocale in allLocales) { | 193 for (var rawLocale in allLocales) { |
187 var locale = Intl.canonicalizedLocale(rawLocale); | 194 var locale = Intl.canonicalizedLocale(rawLocale); |
188 output.write(" '$locale' : () => ${_libraryName(locale)}.loadLibrary(),\n")
; | 195 var loadOperation = (useDeferredLoading) |
| 196 ? " '$locale' : () => ${_libraryName(locale)}.loadLibrary(),\n" |
| 197 : " '$locale' : () => new Future.value(null),\n"; |
| 198 output.write(loadOperation); |
189 } | 199 } |
190 output.write("};\n"); | 200 output.write("};\n"); |
191 output.write( | 201 output.write( |
192 "\nMessageLookupByLibrary _findExact(localeName) {\n" | 202 "\nMessageLookupByLibrary _findExact(localeName) {\n" |
193 " switch (localeName) {\n"); | 203 " switch (localeName) {\n"); |
194 for (var rawLocale in allLocales) { | 204 for (var rawLocale in allLocales) { |
195 var locale = Intl.canonicalizedLocale(rawLocale); | 205 var locale = Intl.canonicalizedLocale(rawLocale); |
196 output.write( | 206 output.write( |
197 " case '$locale' : return ${_libraryName(locale)}.messages;\n"); | 207 " case '$locale' : return ${_libraryName(locale)}.messages;\n"); |
198 } | 208 } |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 messageLookup.addLocale(localeName, _findGeneratedMessagesFor)); | 247 messageLookup.addLocale(localeName, _findGeneratedMessagesFor)); |
238 } | 248 } |
239 | 249 |
240 MessageLookupByLibrary _findGeneratedMessagesFor(locale) { | 250 MessageLookupByLibrary _findGeneratedMessagesFor(locale) { |
241 var actualLocale = Intl.verifiedLocale(locale, (x) => _findExact(x) != null, | 251 var actualLocale = Intl.verifiedLocale(locale, (x) => _findExact(x) != null, |
242 onFailure: (_) => null); | 252 onFailure: (_) => null); |
243 if (actualLocale == null) return null; | 253 if (actualLocale == null) return null; |
244 return _findExact(actualLocale); | 254 return _findExact(actualLocale); |
245 } | 255 } |
246 """; | 256 """; |
OLD | NEW |