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 |
66 * parameter from the Intl.message call, | |
67 * but it can be any identifier that thiss program and the output of the | |
Emily Fortuna
2013/07/03 19:47:40
typo thiss -> this
Alan Knight
2013/07/03 19:58:54
Done.
| |
68 * translation can agree on as identifying a message. | |
69 */ | |
70 String id; | |
66 | 71 |
67 String translatedString; | 72 /** Our translated version of [originalMessage]. */ |
68 IntlMessage originalMessage; | 73 Message translated; |
69 TranslatedMessage(this.id, this.translatedString); | |
70 | 74 |
71 String get message => translatedString; | 75 /** The original message that we are a translation of. */ |
76 MainMessage originalMessage; | |
77 | |
78 TranslatedMessage(this.id, this.translated); | |
79 | |
80 Message get message => translated; | |
81 | |
82 toString() => id.toString(); | |
72 } | 83 } |
73 | 84 |
74 /** | 85 /** |
75 * We can't use a hyphen in a Dart library name, so convert the locale | 86 * We can't use a hyphen in a Dart library name, so convert the locale |
76 * separator to an underscore. | 87 * separator to an underscore. |
77 */ | 88 */ |
78 String asLibraryName(String x) => x.replaceAll('-', '_'); | 89 String asLibraryName(String x) => x.replaceAll('-', '_'); |
79 | 90 |
80 /** | 91 /** |
81 * Generate a file messages_<locale>.dart for the [translations] in | 92 * Generate a file <[generated_file_prefix]>_messages_<[locale]>.dart |
82 * [locale]. | 93 * for the [translations] in [locale] and put it in [targetDir]. |
83 */ | 94 */ |
84 void generateIndividualMessageFile(String locale, | 95 void generateIndividualMessageFile(String locale, |
85 Iterable<TranslatedMessage> translations, String targetDir) { | 96 Iterable<TranslatedMessage> translations, String targetDir) { |
86 var result = new StringBuffer(); | 97 var result = new StringBuffer(); |
87 locale = new IntlMessage().escapeAndValidate(locale, locale); | 98 locale = new MainMessage().escapeAndValidateString(locale); |
88 result.write(prologue(locale)); | 99 result.write(prologue(locale)); |
89 // Exclude messages with no translation and translations with no matching | 100 // 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) | 101 // original message (e.g. if we're using some messages from a larger catalog) |
91 var usableTranslations = translations.where( | 102 var usableTranslations = translations.where( |
92 (each) => each.originalMessage != null && each.message != null).toList(); | 103 (each) => each.originalMessage != null && each.message != null).toList(); |
93 for (var each in usableTranslations) { | 104 for (var each in usableTranslations) { |
94 each.originalMessage.addTranslation(locale, each.message); | 105 each.originalMessage.addTranslation(locale, each.message); |
95 } | 106 } |
96 usableTranslations.sort((a, b) => | 107 usableTranslations.sort((a, b) => |
97 a.originalMessage.name.compareTo(b.originalMessage.name)); | 108 a.originalMessage.name.compareTo(b.originalMessage.name)); |
98 for (var each in usableTranslations) { | 109 for (var translation in usableTranslations) { |
99 result.write(" "); | 110 result.write(" "); |
100 result.write(each.originalMessage.toCode(locale)); | 111 result.write(translation.originalMessage.toCodeForLocale(locale)); |
101 result.write("\n\n"); | 112 result.write("\n\n"); |
102 } | 113 } |
103 result.write("\n final messages = const {\n"); | 114 result.write("\n final messages = const {\n"); |
104 var entries = usableTranslations | 115 var entries = usableTranslations |
105 .map((translation) => translation.originalMessage.name) | 116 .map((translation) => translation.originalMessage.name) |
106 .map((name) => " \"$name\" : $name"); | 117 .map((name) => " \"$name\" : $name"); |
107 result.write(entries.join(",\n")); | 118 result.write(entries.join(",\n")); |
108 result.write("\n };\n}"); | 119 result.write("\n };\n}"); |
109 | 120 |
110 var output = new File(path.join(targetDir, | 121 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); | 204 messageLookup.addLocale(localeName, _findGeneratedMessagesFor); |
194 return new Future.value(); | 205 return new Future.value(); |
195 } | 206 } |
196 | 207 |
197 MessageLookupByLibrary _findGeneratedMessagesFor(locale) { | 208 MessageLookupByLibrary _findGeneratedMessagesFor(locale) { |
198 var actualLocale = Intl.verifiedLocale(locale, (x) => _findExact(x) != null); | 209 var actualLocale = Intl.verifiedLocale(locale, (x) => _findExact(x) != null); |
199 if (actualLocale == null) return null; | 210 if (actualLocale == null) return null; |
200 return _findExact(actualLocale); | 211 return _findExact(actualLocale); |
201 } | 212 } |
202 """; | 213 """; |
OLD | NEW |