| 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 29 matching lines...) Expand all Loading... |
| 40 String importForGeneratedFile(String file) => | 40 String importForGeneratedFile(String file) => |
| 41 generatedImportPath.isEmpty ? file : "$generatedImportPath/$file"; | 41 generatedImportPath.isEmpty ? file : "$generatedImportPath/$file"; |
| 42 | 42 |
| 43 /** | 43 /** |
| 44 * 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 |
| 45 * the reading of translations should add to this. | 45 * the reading of translations should add to this. |
| 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 |
| 51 * directory we may want to prefix some to distinguish them. |
| 52 */ |
| 53 String generatedFilePrefix = ''; |
| 54 |
| 55 /** |
| 50 * This represents a message and its translation. We assume that the translation | 56 * This represents a message and its translation. We assume that the translation |
| 51 * has some identifier that allows us to figure out the original message it | 57 * has some identifier that allows us to figure out the original message it |
| 52 * corresponds to, and that it may want to transform the translated text in | 58 * corresponds to, and that it may want to transform the translated text in |
| 53 * some way, e.g. to turn whatever format the translation uses for variables | 59 * some way, e.g. to turn whatever format the translation uses for variables |
| 54 * into a Dart string interpolation. Specific translation | 60 * into a Dart string interpolation. Specific translation |
| 55 * mechanisms are expected to subclass this. | 61 * mechanisms are expected to subclass this. |
| 56 */ | 62 */ |
| 57 abstract class TranslatedMessage { | 63 abstract class TranslatedMessage { |
| 58 /** The identifier for this message. In the simplest case, this is the name.*/ | 64 /** The identifier for this message. In the simplest case, this is the name.*/ |
| 59 var id; | 65 var id; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 result.write(each.originalMessage.toCode(locale)); | 100 result.write(each.originalMessage.toCode(locale)); |
| 95 result.write("\n\n"); | 101 result.write("\n\n"); |
| 96 } | 102 } |
| 97 result.write("\n final messages = const {\n"); | 103 result.write("\n final messages = const {\n"); |
| 98 var entries = sorted | 104 var entries = sorted |
| 99 .map((translation) => translation.originalMessage.name) | 105 .map((translation) => translation.originalMessage.name) |
| 100 .map((name) => " \"$name\" : $name"); | 106 .map((name) => " \"$name\" : $name"); |
| 101 result.write(entries.join(",\n")); | 107 result.write(entries.join(",\n")); |
| 102 result.write("\n };\n}"); | 108 result.write("\n };\n}"); |
| 103 | 109 |
| 104 var output = new File(path.join(targetDir, "messages_$locale.dart")); | 110 var output = new File(path.join(targetDir, |
| 111 "${generatedFilePrefix}messages_$locale.dart")); |
| 105 output.writeAsStringSync(result.toString()); | 112 output.writeAsStringSync(result.toString()); |
| 106 } | 113 } |
| 107 | 114 |
| 108 /** | 115 /** |
| 109 * This returns the mostly constant string used in [generated] for the | 116 * This returns the mostly constant string used in |
| 110 * beginning of the file, parameterized by [locale]. | 117 * [generateIndividualMessageFile] for the beginning of the file, |
| 118 * parameterized by [locale]. |
| 111 */ | 119 */ |
| 112 String prologue(String locale) => """ | 120 String prologue(String locale) => """ |
| 113 /** | 121 /** |
| 114 * DO NOT EDIT. This is code generated via pkg/intl/generate_localized.dart | 122 * DO NOT EDIT. This is code generated via pkg/intl/generate_localized.dart |
| 115 * This is a library that provides messages for a $locale locale. All the | 123 * This is a library that provides messages for a $locale locale. All the |
| 116 * messages from the main program should be duplicated here with the same | 124 * messages from the main program should be duplicated here with the same |
| 117 * function name. | 125 * function name. |
| 118 */ | 126 */ |
| 119 | 127 |
| 120 library messages_${locale.replaceAll('-','_')}; | 128 library messages_${locale.replaceAll('-','_')}; |
| 121 import 'package:$intlImportPath/intl.dart'; | 129 import 'package:$intlImportPath/intl.dart'; |
| 122 import 'package:$intlImportPath/message_lookup_by_library.dart'; | 130 import 'package:$intlImportPath/message_lookup_by_library.dart'; |
| 123 | 131 |
| 124 final messages = new MessageLookup(); | 132 final messages = new MessageLookup(); |
| 125 | 133 |
| 126 class MessageLookup extends MessageLookupByLibrary { | 134 class MessageLookup extends MessageLookupByLibrary { |
| 127 | 135 |
| 128 get localeName => '$locale'; | 136 get localeName => '$locale'; |
| 129 """; | 137 """; |
| 130 | 138 |
| 131 /** | 139 /** |
| 132 * This section generates the messages_all.dart file based on the list of | 140 * This section generates the messages_all.dart file based on the list of |
| 133 * [allLocales]. | 141 * [allLocales]. |
| 134 */ | 142 */ |
| 135 String generateMainImportFile() { | 143 String generateMainImportFile() { |
| 136 var output = new StringBuffer(); | 144 var output = new StringBuffer(); |
| 137 output.write(mainPrologue); | 145 output.write(mainPrologue); |
| 138 for (var each in allLocales) { | 146 for (var each in allLocales) { |
| 139 var baseFile = 'messages_$each.dart'; | 147 var baseFile = '${generatedFilePrefix}messages_$each.dart'; |
| 140 var file = importForGeneratedFile(baseFile); | 148 var file = importForGeneratedFile(baseFile); |
| 141 output.write("import '$file' as ${asLibraryName(each)};\n"); | 149 output.write("import '$file' as ${asLibraryName(each)};\n"); |
| 142 } | 150 } |
| 143 output.write( | 151 output.write( |
| 144 "\nMessageLookupByLibrary _findExact(localeName) {\n" | 152 "\nMessageLookupByLibrary _findExact(localeName) {\n" |
| 145 " switch (localeName) {\n"); | 153 " switch (localeName) {\n"); |
| 146 for (var each in allLocales) { | 154 for (var each in allLocales) { |
| 147 output.write( | 155 output.write( |
| 148 " case '$each' : return ${asLibraryName(each)}.messages;\n"); | 156 " case '$each' : return ${asLibraryName(each)}.messages;\n"); |
| 149 } | 157 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 165 library messages_all; | 173 library messages_all; |
| 166 | 174 |
| 167 import 'dart:async'; | 175 import 'dart:async'; |
| 168 import 'package:$intlImportPath/message_lookup_by_library.dart'; | 176 import 'package:$intlImportPath/message_lookup_by_library.dart'; |
| 169 import 'package:$intlImportPath/src/intl_helpers.dart'; | 177 import 'package:$intlImportPath/src/intl_helpers.dart'; |
| 170 import 'package:$intlImportPath/intl.dart'; | 178 import 'package:$intlImportPath/intl.dart'; |
| 171 | 179 |
| 172 """; | 180 """; |
| 173 | 181 |
| 174 /** | 182 /** |
| 175 * Constant string used in [generateMainImportfile] as the end of the file. | 183 * Constant string used in [generateMainImportFile] as the end of the file. |
| 176 */ | 184 */ |
| 177 const closing = """ | 185 const closing = """ |
| 178 default: return null; | 186 default: return null; |
| 179 } | 187 } |
| 180 } | 188 } |
| 181 | 189 |
| 182 /** User programs should call this before using [localeName] for messages.*/ | 190 /** User programs should call this before using [localeName] for messages.*/ |
| 183 initializeMessages(localeName) { | 191 initializeMessages(localeName) { |
| 184 initializeInternalMessageLookup(() => new CompositeMessageLookup()); | 192 initializeInternalMessageLookup(() => new CompositeMessageLookup()); |
| 185 messageLookup.addLocale(localeName, _findGeneratedMessagesFor); | 193 messageLookup.addLocale(localeName, _findGeneratedMessagesFor); |
| 186 return new Future.value(); | 194 return new Future.value(); |
| 187 } | 195 } |
| 188 | 196 |
| 189 MessageLookupByLibrary _findGeneratedMessagesFor(locale) { | 197 MessageLookupByLibrary _findGeneratedMessagesFor(locale) { |
| 190 var actualLocale = Intl.verifiedLocale(locale, (x) => _findExact(x) != null); | 198 var actualLocale = Intl.verifiedLocale(locale, (x) => _findExact(x) != null); |
| 191 if (actualLocale == null) return null; | 199 if (actualLocale == null) return null; |
| 192 return _findExact(actualLocale); | 200 return _findExact(actualLocale); |
| 193 } | 201 } |
| 194 """; | 202 """; |
| OLD | NEW |