| 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 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 */ | 61 */ |
| 62 abstract class TranslatedMessage { | 62 abstract class TranslatedMessage { |
| 63 /** | 63 /** |
| 64 * 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 |
| 65 * parameter from the Intl.message call, | 65 * parameter from the Intl.message call, |
| 66 * but it can be any identifier that this program and the output of the | 66 * but it can be any identifier that this program and the output of the |
| 67 * translation can agree on as identifying a message. | 67 * translation can agree on as identifying a message. |
| 68 */ | 68 */ |
| 69 final String id; | 69 final String id; |
| 70 | 70 |
| 71 /** Our translated version of [originalMessage]. */ | 71 /** Our translated version of all the [originalMessages]. */ |
| 72 final Message translated; | 72 final Message translated; |
| 73 | 73 |
| 74 /** The original message that we are a translation of. */ | 74 /** |
| 75 MainMessage originalMessage; | 75 * The original messages that we are a translation of. There can |
| 76 * be more than one original message for the same translation. |
| 77 */ |
| 78 List<MainMessage> originalMessages; |
| 79 |
| 80 /** |
| 81 * For backward compatibility, we still have the originalMessage API. |
| 82 */ |
| 83 MainMessage get originalMessage => originalMessages.first; |
| 84 set originalMessage(MainMessage m) => originalMessages = [m]; |
| 76 | 85 |
| 77 TranslatedMessage(this.id, this.translated); | 86 TranslatedMessage(this.id, this.translated); |
| 78 | 87 |
| 79 Message get message => translated; | 88 Message get message => translated; |
| 80 | 89 |
| 81 toString() => id.toString(); | 90 toString() => id.toString(); |
| 82 } | 91 } |
| 83 | 92 |
| 84 /** | 93 /** |
| 85 * We can't use a hyphen in a Dart library name, so convert the locale | 94 * We can't use a hyphen in a Dart library name, so convert the locale |
| 86 * separator to an underscore. | 95 * separator to an underscore. |
| 87 */ | 96 */ |
| 88 String _libraryName(String x) => 'messages_' + x.replaceAll('-', '_'); | 97 String _libraryName(String x) => 'messages_' + x.replaceAll('-', '_'); |
| 89 | 98 |
| 90 /** | 99 /** |
| 91 * Generate a file <[generated_file_prefix]>_messages_<[locale]>.dart | 100 * Generate a file <[generated_file_prefix]>_messages_<[locale]>.dart |
| 92 * for the [translations] in [locale] and put it in [targetDir]. | 101 * for the [translations] in [locale] and put it in [targetDir]. |
| 93 */ | 102 */ |
| 94 void generateIndividualMessageFile(String locale, | 103 void generateIndividualMessageFile(String locale, |
| 95 Iterable<TranslatedMessage> translations, String targetDir) { | 104 Iterable<TranslatedMessage> translations, String targetDir) { |
| 96 var result = new StringBuffer(); | 105 var result = new StringBuffer(); |
| 97 locale = new MainMessage().escapeAndValidateString(locale); | 106 locale = new MainMessage().escapeAndValidateString(locale); |
| 98 result.write(prologue(locale)); | 107 result.write(prologue(locale)); |
| 99 // Exclude messages with no translation and translations with no matching | 108 // Exclude messages with no translation and translations with no matching |
| 100 // original message (e.g. if we're using some messages from a larger catalog) | 109 // original message (e.g. if we're using some messages from a larger catalog) |
| 101 var usableTranslations = translations.where( | 110 var usableTranslations = translations.where( |
| 102 (each) => each.originalMessage != null && each.message != null).toList(); | 111 (each) => each.originalMessages != null && each.message != null).toList(); |
| 103 for (var each in usableTranslations) { | 112 for (var each in usableTranslations) { |
| 104 each.originalMessage.addTranslation(locale, each.message); | 113 for (var original in each.originalMessages) { |
| 114 original.addTranslation(locale, each.message); |
| 115 } |
| 105 } | 116 } |
| 106 usableTranslations.sort((a, b) => | 117 usableTranslations.sort((a, b) => |
| 107 a.originalMessage.name.compareTo(b.originalMessage.name)); | 118 a.originalMessages.first.name.compareTo(b.originalMessages.first.name)); |
| 108 for (var translation in usableTranslations) { | 119 for (var translation in usableTranslations) { |
| 109 result | 120 for (var original in translation.originalMessages) { |
| 110 ..write(" ") | 121 result |
| 111 ..write(translation.originalMessage.toCodeForLocale(locale)) | 122 ..write(" ") |
| 112 ..write("\n\n"); | 123 ..write(original.toCodeForLocale(locale)) |
| 124 ..write("\n\n"); |
| 125 } |
| 113 } | 126 } |
| 114 result.write("\n final messages = const {\n"); | 127 result.write("\n final messages = const {\n"); |
| 115 var entries = usableTranslations | 128 var entries = usableTranslations |
| 116 .map((translation) => translation.originalMessage.name) | 129 .expand((translation) => translation.originalMessages) |
| 130 .map((original) => original.name) |
| 117 .map((name) => " \"$name\" : $name"); | 131 .map((name) => " \"$name\" : $name"); |
| 118 result | 132 result |
| 119 ..write(entries.join(",\n")) | 133 ..write(entries.join(",\n")) |
| 120 ..write("\n };\n}"); | 134 ..write("\n };\n}"); |
| 121 | 135 |
| 122 new File(path.join(targetDir,"${generatedFilePrefix}messages_$locale.dart")) | 136 new File(path.join(targetDir,"${generatedFilePrefix}messages_$locale.dart")) |
| 123 ..writeAsStringSync(result.toString()); | 137 ..writeAsStringSync(result.toString()); |
| 124 } | 138 } |
| 125 | 139 |
| 126 /** | 140 /** |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 return new Future.value(true); | 239 return new Future.value(true); |
| 226 } | 240 } |
| 227 | 241 |
| 228 MessageLookupByLibrary _findGeneratedMessagesFor(locale) { | 242 MessageLookupByLibrary _findGeneratedMessagesFor(locale) { |
| 229 var actualLocale = Intl.verifiedLocale(locale, (x) => _findExact(x) != null, | 243 var actualLocale = Intl.verifiedLocale(locale, (x) => _findExact(x) != null, |
| 230 onFailure: (_) => null); | 244 onFailure: (_) => null); |
| 231 if (actualLocale == null) return null; | 245 if (actualLocale == null) return null; |
| 232 return _findExact(actualLocale); | 246 return _findExact(actualLocale); |
| 233 } | 247 } |
| 234 """; | 248 """; |
| OLD | NEW |