Chromium Code Reviews| 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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 54 | 54 |
| 55 /** | 55 /** |
| 56 * 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 |
| 57 * 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 |
| 58 * 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 |
| 59 * 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 |
| 60 * into a Dart string interpolation. Specific translation | 60 * into a Dart string interpolation. Specific translation |
| 61 * mechanisms are expected to subclass this. | 61 * mechanisms are expected to subclass this. |
| 62 */ | 62 */ |
| 63 abstract class TranslatedMessage { | 63 abstract class TranslatedMessage { |
| 64 /** The identifier for this message. In the simplest case, this is the name.*/ | 64 /** |
| 65 var id; | 65 * The identifier for this message. In the simplest case, this is the name, |
|
Emily Fortuna
2013/07/03 17:52:33
sorry, name of what?
Alan Knight
2013/07/03 18:41:07
Clarified.
| |
| 66 * but it can be any identifier that this program and the output of the | |
| 67 * translation can agree on as identifying a message. | |
| 68 */ | |
| 69 String id; | |
| 66 | 70 |
| 67 String translatedString; | 71 /** Our translated version of [originalMessage]. */ |
| 68 IntlMessage originalMessage; | 72 Message translated; |
| 69 TranslatedMessage(this.id, this.translatedString); | |
| 70 | 73 |
| 71 String get message => translatedString; | 74 /** The original message that we are a translation of. */ |
| 75 MainMessage originalMessage; | |
| 76 | |
| 77 TranslatedMessage(this.id, this.translated); | |
| 78 | |
| 79 get message => translated; | |
|
Emily Fortuna
2013/07/03 17:52:33
I think we prefer to still have return types, espe
Alan Knight
2013/07/03 18:41:07
There should be very few others using these APIs,
| |
| 80 | |
| 81 toString() => id.toString(); | |
| 72 } | 82 } |
| 73 | 83 |
| 74 /** | 84 /** |
| 75 * 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 |
| 76 * separator to an underscore. | 86 * separator to an underscore. |
| 77 */ | 87 */ |
| 78 String asLibraryName(String x) => x.replaceAll('-', '_'); | 88 String asLibraryName(String x) => x.replaceAll('-', '_'); |
| 79 | 89 |
| 80 /** | 90 /** |
| 81 * Generate a file messages_<locale>.dart for the [translations] in | 91 * Generate a file <[generated_file_prefix]>_messages_<[locale]>.dart |
| 82 * [locale]. | 92 * for the [translations] in [locale] and put it in [targetDir]. |
| 83 */ | 93 */ |
| 84 void generateIndividualMessageFile(String locale, | 94 void generateIndividualMessageFile(String locale, |
| 85 Iterable<TranslatedMessage> translations, String targetDir) { | 95 Iterable<TranslatedMessage> translations, String targetDir) { |
| 86 var result = new StringBuffer(); | 96 var result = new StringBuffer(); |
| 87 locale = new IntlMessage().escapeAndValidate(locale, locale); | 97 locale = new MainMessage().escapeAndValidateString(locale); |
| 88 result.write(prologue(locale)); | 98 result.write(prologue(locale)); |
| 89 // Exclude messages with no translation and translations with no matching | 99 // Exclude messages with no translation and translations with no matching |
| 90 // original message (e.g. if we're using some messages from a larger catalog) | 100 // original message (e.g. if we're using some messages from a larger catalog) |
| 91 var usableTranslations = translations.where( | 101 var usableTranslations = translations.where( |
| 92 (each) => each.originalMessage != null && each.message != null).toList(); | 102 (each) => each.originalMessage != null && each.message != null).toList(); |
| 93 for (var each in usableTranslations) { | 103 for (var each in usableTranslations) { |
| 94 each.originalMessage.addTranslation(locale, each.message); | 104 each.originalMessage.addTranslation(locale, each.message); |
| 95 } | 105 } |
| 96 usableTranslations.sort((a, b) => | 106 usableTranslations.sort((a, b) => |
| 97 a.originalMessage.name.compareTo(b.originalMessage.name)); | 107 a.originalMessage.name.compareTo(b.originalMessage.name)); |
| 98 for (var each in usableTranslations) { | 108 for (var translation in usableTranslations) { |
| 99 result.write(" "); | 109 result.write(" "); |
| 100 result.write(each.originalMessage.toCode(locale)); | 110 result.write(translation.originalMessage.toCodeForLocale(locale)); |
| 101 result.write("\n\n"); | 111 result.write("\n\n"); |
| 102 } | 112 } |
| 103 result.write("\n final messages = const {\n"); | 113 result.write("\n final messages = const {\n"); |
| 104 var entries = usableTranslations | 114 var entries = usableTranslations |
| 105 .map((translation) => translation.originalMessage.name) | 115 .map((translation) => translation.originalMessage.name) |
| 106 .map((name) => " \"$name\" : $name"); | 116 .map((name) => " \"$name\" : $name"); |
| 107 result.write(entries.join(",\n")); | 117 result.write(entries.join(",\n")); |
| 108 result.write("\n };\n}"); | 118 result.write("\n };\n}"); |
| 109 | 119 |
| 110 var output = new File(path.join(targetDir, | 120 var output = new File(path.join(targetDir, |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 193 messageLookup.addLocale(localeName, _findGeneratedMessagesFor); | 203 messageLookup.addLocale(localeName, _findGeneratedMessagesFor); |
| 194 return new Future.value(); | 204 return new Future.value(); |
| 195 } | 205 } |
| 196 | 206 |
| 197 MessageLookupByLibrary _findGeneratedMessagesFor(locale) { | 207 MessageLookupByLibrary _findGeneratedMessagesFor(locale) { |
| 198 var actualLocale = Intl.verifiedLocale(locale, (x) => _findExact(x) != null); | 208 var actualLocale = Intl.verifiedLocale(locale, (x) => _findExact(x) != null); |
| 199 if (actualLocale == null) return null; | 209 if (actualLocale == null) return null; |
| 200 return _findExact(actualLocale); | 210 return _findExact(actualLocale); |
| 201 } | 211 } |
| 202 """; | 212 """; |
| OLD | NEW |