| 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 * |
| 11 * An example of usage can be found | 11 * An example of usage can be found |
| 12 * in test/message_extract/generate_from_json.dart | 12 * in test/message_extract/generate_from_json.dart |
| 13 */ | 13 */ |
| 14 library generate_localized; | 14 library generate_localized; |
| 15 | 15 |
| 16 import 'extract_messages.dart'; | 16 import 'extract_messages.dart'; |
| 17 import 'src/intl_message.dart'; | 17 import 'src/intl_message.dart'; |
| 18 import 'dart:io'; | 18 import 'dart:io'; |
| 19 import 'package:pathos/path.dart' as path; | 19 import 'package:pathos/path.dart' as path; |
| 20 | 20 |
| 21 /** | 21 /** |
| 22 * If the import path following package: is something else, modify the |
| 23 * [intlImportPath] variable to change the import directives in the generated |
| 24 * code. |
| 25 */ |
| 26 var intlImportPath = 'intl'; |
| 27 |
| 28 /** |
| 29 * If the path to the generated files is something other than the current |
| 30 * directory, update the [generatedImportPath] variable to change the import |
| 31 * directives in the generated code. |
| 32 */ |
| 33 var generatedImportPath = ''; |
| 34 |
| 35 /** |
| 36 * Given a base file, return the file prefixed with the path to import it. |
| 37 * By default, that is in the current directory, but if [generatedImportPath] |
| 38 * has been set, then use that as a prefix. |
| 39 */ |
| 40 String importForGeneratedFile(String file) => |
| 41 generatedImportPath.isEmpty ? file : "$generatedImportPath/$file"; |
| 42 |
| 43 /** |
| 22 * A list of all the locales for which we have translations. Code that does | 44 * A list of all the locales for which we have translations. Code that does |
| 23 * the reading of translations should add to this. | 45 * the reading of translations should add to this. |
| 24 */ | 46 */ |
| 25 List<String> allLocales = []; | 47 List<String> allLocales = []; |
| 26 | 48 |
| 27 /** | 49 /** |
| 28 * This represents a message and its translation. We assume that the translation | 50 * 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 | 51 * 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 | 52 * 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 | 53 * some way, e.g. to turn whatever format the translation uses for variables |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 */ | 111 */ |
| 90 String prologue(String locale) => """ | 112 String prologue(String locale) => """ |
| 91 /** | 113 /** |
| 92 * DO NOT EDIT. This is code generated via pkg/intl/generate_localized.dart | 114 * 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 | 115 * 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 | 116 * messages from the main program should be duplicated here with the same |
| 95 * function name. | 117 * function name. |
| 96 */ | 118 */ |
| 97 | 119 |
| 98 library messages_${locale.replaceAll('-','_')}; | 120 library messages_${locale.replaceAll('-','_')}; |
| 99 import 'package:intl/intl.dart'; | 121 import 'package:$intlImportPath/intl.dart'; |
| 100 import 'package:intl/message_lookup_by_library.dart'; | 122 import 'package:$intlImportPath/message_lookup_by_library.dart'; |
| 101 | 123 |
| 102 final messages = new MessageLookup(); | 124 final messages = new MessageLookup(); |
| 103 | 125 |
| 104 class MessageLookup extends MessageLookupByLibrary { | 126 class MessageLookup extends MessageLookupByLibrary { |
| 105 | 127 |
| 106 get localeName => '$locale'; | 128 get localeName => '$locale'; |
| 107 """; | 129 """; |
| 108 | 130 |
| 109 /** | 131 /** |
| 110 * This section generates the messages_all.dart file based on the list of | 132 * This section generates the messages_all.dart file based on the list of |
| 111 * [allLocales]. | 133 * [allLocales]. |
| 112 */ | 134 */ |
| 113 String generateMainImportFile() { | 135 String generateMainImportFile() { |
| 114 var output = new StringBuffer(); | 136 var output = new StringBuffer(); |
| 115 output.write(mainPrologue); | 137 output.write(mainPrologue); |
| 116 for (var each in allLocales) { | 138 for (var each in allLocales) { |
| 117 output.write("import 'messages_$each.dart' as ${asLibraryName(each)};\n"); | 139 var baseFile = 'messages_$each.dart'; |
| 140 var file = importForGeneratedFile(baseFile); |
| 141 output.write("import '$file' as ${asLibraryName(each)};\n"); |
| 118 } | 142 } |
| 119 output.write( | 143 output.write( |
| 120 "\nMessageLookupByLibrary _findExact(localeName) {\n" | 144 "\nMessageLookupByLibrary _findExact(localeName) {\n" |
| 121 " switch (localeName) {\n"); | 145 " switch (localeName) {\n"); |
| 122 for (var each in allLocales) { | 146 for (var each in allLocales) { |
| 123 output.write( | 147 output.write( |
| 124 " case '$each' : return ${asLibraryName(each)}.messages;\n"); | 148 " case '$each' : return ${asLibraryName(each)}.messages;\n"); |
| 125 } | 149 } |
| 126 output.write(closing); | 150 output.write(closing); |
| 127 return output.toString(); | 151 return output.toString(); |
| 128 } | 152 } |
| 129 | 153 |
| 130 /** | 154 /** |
| 131 * Constant string used in [generateMainImportFile] for the beginning of the | 155 * Constant string used in [generateMainImportFile] for the beginning of the |
| 132 * file. | 156 * file. |
| 133 */ | 157 */ |
| 134 const mainPrologue = """ | 158 var mainPrologue = """ |
| 135 /** | 159 /** |
| 136 * DO NOT EDIT. This is code generated via pkg/intl/generate_localized.dart | 160 * 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 | 161 * This is a library that looks up messages for specific locales by |
| 138 * delegating to the appropriate library. | 162 * delegating to the appropriate library. |
| 139 */ | 163 */ |
| 140 | 164 |
| 141 library messages_all; | 165 library messages_all; |
| 142 | 166 |
| 143 import 'dart:async'; | 167 import 'dart:async'; |
| 144 import 'package:intl/message_lookup_by_library.dart'; | 168 import 'package:$intlImportPath/message_lookup_by_library.dart'; |
| 145 import 'package:intl/src/intl_helpers.dart'; | 169 import 'package:$intlImportPath/src/intl_helpers.dart'; |
| 146 import 'package:intl/intl.dart'; | 170 import 'package:$intlImportPath/intl.dart'; |
| 147 | 171 |
| 148 """; | 172 """; |
| 149 | 173 |
| 150 /** | 174 /** |
| 151 * Constant string used in [generateMainImportfile] as the end of the file. | 175 * Constant string used in [generateMainImportfile] as the end of the file. |
| 152 */ | 176 */ |
| 153 const closing = """ | 177 const closing = """ |
| 154 default: return null; | 178 default: return null; |
| 155 } | 179 } |
| 156 } | 180 } |
| 157 | 181 |
| 158 /** User programs should call this before using [localeName] for messages.*/ | 182 /** User programs should call this before using [localeName] for messages.*/ |
| 159 initializeMessages(localeName) { | 183 initializeMessages(localeName) { |
| 160 initializeInternalMessageLookup(() => new CompositeMessageLookup()); | 184 initializeInternalMessageLookup(() => new CompositeMessageLookup()); |
| 161 messageLookup.addLocale(localeName, _findGeneratedMessagesFor); | 185 messageLookup.addLocale(localeName, _findGeneratedMessagesFor); |
| 162 return new Future.immediate(null); | 186 return new Future.immediate(null); |
| 163 } | 187 } |
| 164 | 188 |
| 165 MessageLookupByLibrary _findGeneratedMessagesFor(locale) { | 189 MessageLookupByLibrary _findGeneratedMessagesFor(locale) { |
| 166 var actualLocale = Intl.verifiedLocale(locale, (x) => _findExact(x) != null); | 190 var actualLocale = Intl.verifiedLocale(locale, (x) => _findExact(x) != null); |
| 167 if (actualLocale == null) return null; | 191 if (actualLocale == null) return null; |
| 168 return _findExact(actualLocale); | 192 return _findExact(actualLocale); |
| 169 } | 193 } |
| 170 """; | 194 """; |
| OLD | NEW |