OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
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. | |
4 | |
5 /** | |
6 * Message/plural format library with locale support. This can have different | |
7 * implementations based on the mechanism for finding the localized versions | |
8 * of messages. This version expects them to be in a library named e.g. | |
9 * 'messages_en_US'. The prefix is set in the "initializeMessages" call, which | |
10 * must be made for a locale before any lookups can be done. | |
11 * | |
12 * See Intl class comment or `tests/message_format_test.dart` for more examples. | |
13 */ | |
14 library message_lookup_by_library; | |
15 | |
16 import 'intl.dart'; | |
17 | |
18 /** | |
19 * This is a message lookup mechanism that delegates to one of a collection | |
20 * of individual [MessageLookupByLibrary] instances. | |
21 */ | |
22 class CompositeMessageLookup { | |
23 /** A map from locale names to the corresponding lookups. */ | |
24 Map<String, MessageLookupByLibrary> availableMessages = new Map(); | |
25 | |
26 /** Return true if we have a message lookup for [localeName]. */ | |
27 bool localeExists(localeName) => availableMessages.containsKey(localeName); | |
28 | |
29 /** | |
30 * Look up the message with the given [name] and [locale] and return | |
31 * the translated version with the values in [args] interpolated. | |
32 * If nothing is found, return [message_str]. The [desc] and [examples] | |
33 * parameters are ignored | |
34 */ | |
35 String lookupMessage(String message_str, [final String desc='', | |
36 final Map examples=const {}, String locale, | |
37 String name, List<String> args, String meaning]) { | |
38 var actualLocale = (locale == null) ? Intl.getCurrentLocale() : locale; | |
39 // For this usage, if the locale doesn't exist for messages, just return | |
40 // it and we'll fall back to the original version. | |
41 var verifiedLocale = | |
42 Intl.verifiedLocale( | |
43 actualLocale, | |
44 localeExists, | |
45 onFailure: (locale)=>locale); | |
46 var messages = availableMessages[verifiedLocale]; | |
47 if (messages == null) return message_str; | |
48 return messages. | |
49 lookupMessage(message_str, desc, examples, locale, name, args, meaning); | |
50 } | |
51 | |
52 /** | |
53 * If we do not already have a locale for [localeName] then | |
54 * [findLocale] will be called and the result stored as the lookup | |
55 * mechanism for that locale. | |
56 */ | |
57 addLocale(String localeName, Function findLocale) { | |
58 if (localeExists(localeName)) return; | |
59 var canonical = Intl.canonicalizedLocale(localeName); | |
60 var newLocale = findLocale(canonical); | |
61 if (newLocale != null) { | |
62 availableMessages[localeName] = newLocale; | |
63 availableMessages[canonical] = newLocale; | |
64 } | |
65 } | |
66 } | |
67 | |
68 /** | |
69 * This provides an abstract class for messages looked up in generated code. | |
70 * Each locale will have a separate subclass of this class with its set of | |
71 * messages. See generate_localized.dart. | |
72 */ | |
73 abstract class MessageLookupByLibrary { | |
74 /** Prevent infinite recursion when looking up the message. */ | |
75 bool _lookupInProgress = false; | |
76 | |
77 /** | |
78 * Return the localized version of a message. We are passed the original | |
79 * version of the message, which consists of a | |
80 * [message_str] that will be translated, and which may be interpolated | |
81 * based on one or more variables, a [desc] providing a description of usage | |
82 * for the [message_str], and a map of [examples] for each data element to be | |
83 * substituted into the message. | |
84 * | |
85 * For example, if message="Hello, $name", then | |
86 * examples = {'name': 'Sparky'}. If not using the user's default locale, or | |
87 * if the locale is not easily detectable, explicitly pass [locale]. | |
88 * | |
89 * The values of [desc] and [examples] are not used at run-time but are only | |
90 * made available to the translators, so they MUST be simple Strings available | |
91 * at compile time: no String interpolation or concatenation. | |
92 * The expected usage of this is inside a function that takes as parameters | |
93 * the variables used in the interpolated string. | |
94 * | |
95 * Ultimately, the information about the enclosing function and its arguments | |
96 * will be extracted automatically but for the time being it must be passed | |
97 * explicitly in the [name] and [args] arguments. | |
98 */ | |
99 String lookupMessage(String message_str, [final String desc='', | |
100 final Map examples=const {}, String locale, | |
101 String name, List<String> args, String meaning]) { | |
102 if (name == null) return message_str; | |
103 var function = this[name]; | |
104 return function == null ? message_str : Function.apply(function, args); | |
105 } | |
106 | |
107 /** Return our message with the given name */ | |
108 operator [](String messageName) => messages[messageName]; | |
109 | |
110 /** | |
111 * Subclasses should override this to return a list of their message | |
112 * functions. | |
113 */ | |
114 Map<String, Function> get messages; | |
115 | |
116 /** Subclasses should override this to return their locale, e.g. 'en_US' */ | |
117 String get localeName; | |
118 | |
119 toString() => localeName; | |
120 } | |
OLD | NEW |