| 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 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 | 78 |
| 79 Message get message => translated; | 79 Message get message => translated; |
| 80 | 80 |
| 81 toString() => id.toString(); | 81 toString() => id.toString(); |
| 82 } | 82 } |
| 83 | 83 |
| 84 /** | 84 /** |
| 85 * We can't use a hyphen in a Dart library name, so convert the locale | 85 * We can't use a hyphen in a Dart library name, so convert the locale |
| 86 * separator to an underscore. | 86 * separator to an underscore. |
| 87 */ | 87 */ |
| 88 String asLibraryName(String x) => x.replaceAll('-', '_'); | 88 String _libraryName(String x) => x.replaceAll('-', '_'); |
| 89 | 89 |
| 90 /** | 90 /** |
| 91 * Generate a file <[generated_file_prefix]>_messages_<[locale]>.dart | 91 * Generate a file <[generated_file_prefix]>_messages_<[locale]>.dart |
| 92 * for the [translations] in [locale] and put it in [targetDir]. | 92 * for the [translations] in [locale] and put it in [targetDir]. |
| 93 */ | 93 */ |
| 94 void generateIndividualMessageFile(String locale, | 94 void generateIndividualMessageFile(String locale, |
| 95 Iterable<TranslatedMessage> translations, String targetDir) { | 95 Iterable<TranslatedMessage> translations, String targetDir) { |
| 96 var result = new StringBuffer(); | 96 var result = new StringBuffer(); |
| 97 locale = new MainMessage().escapeAndValidateString(locale); | 97 locale = new MainMessage().escapeAndValidateString(locale); |
| 98 result.write(prologue(locale)); | 98 result.write(prologue(locale)); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 import 'package:$intlImportPath/intl.dart'; | 139 import 'package:$intlImportPath/intl.dart'; |
| 140 import 'package:$intlImportPath/message_lookup_by_library.dart'; | 140 import 'package:$intlImportPath/message_lookup_by_library.dart'; |
| 141 | 141 |
| 142 final messages = new MessageLookup(); | 142 final messages = new MessageLookup(); |
| 143 | 143 |
| 144 class MessageLookup extends MessageLookupByLibrary { | 144 class MessageLookup extends MessageLookupByLibrary { |
| 145 | 145 |
| 146 get localeName => '$locale'; | 146 get localeName => '$locale'; |
| 147 """; | 147 """; |
| 148 | 148 |
| 149 |
| 150 _deferredName(locale) => "lazy_${_libraryName(locale)}"; |
| 151 |
| 149 /** | 152 /** |
| 150 * This section generates the messages_all.dart file based on the list of | 153 * This section generates the messages_all.dart file based on the list of |
| 151 * [allLocales]. | 154 * [allLocales]. |
| 152 */ | 155 */ |
| 153 String generateMainImportFile() { | 156 String generateMainImportFile() { |
| 154 var output = new StringBuffer(); | 157 var output = new StringBuffer(); |
| 155 output.write(mainPrologue); | 158 output.write(mainPrologue); |
| 156 for (var each in allLocales) { | 159 for (var locale in allLocales) { |
| 157 var baseFile = '${generatedFilePrefix}messages_$each.dart'; | 160 var baseFile = '${generatedFilePrefix}messages_$locale.dart'; |
| 158 var file = importForGeneratedFile(baseFile); | 161 var file = importForGeneratedFile(baseFile); |
| 159 output.write("import '$file' as ${asLibraryName(each)};\n"); | 162 output.write("@${_deferredName(locale)} "); |
| 163 output.write("import '$file' as ${_libraryName(locale)};\n"); |
| 160 } | 164 } |
| 165 output.write("\n"); |
| 166 for (var locale in allLocales) { |
| 167 output.write("const ${_deferredName(locale)} = const DeferredLibrary"); |
| 168 output.write("('${_libraryName(locale)}');\n"); |
| 169 } |
| 170 output.write("\nconst deferredLibraries = const {\n"); |
| 171 for (var locale in allLocales) { |
| 172 output.write(" '$locale' : ${_deferredName(locale)},\n"); |
| 173 } |
| 174 output.write("};\n"); |
| 161 output.write( | 175 output.write( |
| 162 "\nMessageLookupByLibrary _findExact(localeName) {\n" | 176 "\nMessageLookupByLibrary _findExact(localeName) {\n" |
| 163 " switch (localeName) {\n"); | 177 " switch (localeName) {\n"); |
| 164 for (var each in allLocales) { | 178 for (var locale in allLocales) { |
| 165 output.write( | 179 output.write( |
| 166 " case '$each' : return ${asLibraryName(each)}.messages;\n"); | 180 " case '$locale' : return ${_libraryName(locale)}.messages;\n"); |
| 167 } | 181 } |
| 168 output.write(closing); | 182 output.write(closing); |
| 169 return output.toString(); | 183 return output.toString(); |
| 170 } | 184 } |
| 171 | 185 |
| 172 /** | 186 /** |
| 173 * Constant string used in [generateMainImportFile] for the beginning of the | 187 * Constant string used in [generateMainImportFile] for the beginning of the |
| 174 * file. | 188 * file. |
| 175 */ | 189 */ |
| 176 var mainPrologue = """ | 190 var mainPrologue = """ |
| (...skipping 14 matching lines...) Expand all Loading... |
| 191 | 205 |
| 192 /** | 206 /** |
| 193 * Constant string used in [generateMainImportFile] as the end of the file. | 207 * Constant string used in [generateMainImportFile] as the end of the file. |
| 194 */ | 208 */ |
| 195 const closing = """ | 209 const closing = """ |
| 196 default: return null; | 210 default: return null; |
| 197 } | 211 } |
| 198 } | 212 } |
| 199 | 213 |
| 200 /** User programs should call this before using [localeName] for messages.*/ | 214 /** User programs should call this before using [localeName] for messages.*/ |
| 201 initializeMessages(localeName) { | 215 Future initializeMessages(String localeName) { |
| 202 initializeInternalMessageLookup(() => new CompositeMessageLookup()); | 216 initializeInternalMessageLookup(() => new CompositeMessageLookup()); |
| 203 messageLookup.addLocale(localeName, _findGeneratedMessagesFor); | 217 messageLookup.addLocale(localeName, _findGeneratedMessagesFor); |
| 204 return new Future.value(); | 218 return deferredLibraries[localeName].load(); |
| 205 } | 219 } |
| 206 | 220 |
| 207 MessageLookupByLibrary _findGeneratedMessagesFor(locale) { | 221 MessageLookupByLibrary _findGeneratedMessagesFor(locale) { |
| 208 var actualLocale = Intl.verifiedLocale(locale, (x) => _findExact(x) != null); | 222 var actualLocale = Intl.verifiedLocale(locale, (x) => _findExact(x) != null); |
| 209 if (actualLocale == null) return null; | 223 if (actualLocale == null) return null; |
| 210 return _findExact(actualLocale); | 224 return _findExact(actualLocale); |
| 211 } | 225 } |
| 212 """; | 226 """; |
| OLD | NEW |