Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(316)

Side by Side Diff: intl/lib/message_lookup_by_library.dart

Issue 1400473008: Roll Observatory packages and add a roll script (Closed) Base URL: git@github.com:dart-lang/observatory_pub_packages.git@master
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « intl/lib/intl_standalone.dart ('k') | intl/lib/number_symbols.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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, String name,
37 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 = Intl.verifiedLocale(actualLocale, localeExists,
42 onFailure: (locale) => locale);
43 var messages = availableMessages[verifiedLocale];
44 if (messages == null) return message_str;
45 return messages.lookupMessage(
46 message_str, desc, examples, locale, name, args, meaning);
47 }
48
49 /**
50 * If we do not already have a locale for [localeName] then
51 * [findLocale] will be called and the result stored as the lookup
52 * mechanism for that locale.
53 */
54 addLocale(String localeName, Function findLocale) {
55 if (localeExists(localeName)) return;
56 var canonical = Intl.canonicalizedLocale(localeName);
57 var newLocale = findLocale(canonical);
58 if (newLocale != null) {
59 availableMessages[localeName] = newLocale;
60 availableMessages[canonical] = newLocale;
61 }
62 }
63 }
64
65 /**
66 * This provides an abstract class for messages looked up in generated code.
67 * Each locale will have a separate subclass of this class with its set of
68 * messages. See generate_localized.dart.
69 */
70 abstract class MessageLookupByLibrary {
71 /**
72 * Return the localized version of a message. We are passed the original
73 * version of the message, which consists of a
74 * [message_str] that will be translated, and which may be interpolated
75 * based on one or more variables, a [desc] providing a description of usage
76 * for the [message_str], and a map of [examples] for each data element to be
77 * substituted into the message.
78 *
79 * For example, if message="Hello, $name", then
80 * examples = {'name': 'Sparky'}. If not using the user's default locale, or
81 * if the locale is not easily detectable, explicitly pass [locale].
82 *
83 * The values of [desc] and [examples] are not used at run-time but are only
84 * made available to the translators, so they MUST be simple Strings available
85 * at compile time: no String interpolation or concatenation.
86 * The expected usage of this is inside a function that takes as parameters
87 * the variables used in the interpolated string.
88 *
89 * Ultimately, the information about the enclosing function and its arguments
90 * will be extracted automatically but for the time being it must be passed
91 * explicitly in the [name] and [args] arguments.
92 */
93 String lookupMessage(String message_str, [final String desc = '',
94 final Map examples = const {}, String locale, String name,
95 List<String> args, String meaning]) {
96 if (name == null) return message_str;
97 var function = this[name];
98 return function == null ? message_str : Function.apply(function, args);
99 }
100
101 /** Return our message with the given name */
102 operator [](String messageName) => messages[messageName];
103
104 /**
105 * Subclasses should override this to return a list of their message
106 * functions.
107 */
108 Map<String, Function> get messages;
109
110 /** Subclasses should override this to return their locale, e.g. 'en_US' */
111 String get localeName;
112
113 toString() => localeName;
114 }
OLDNEW
« no previous file with comments | « intl/lib/intl_standalone.dart ('k') | intl/lib/number_symbols.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698