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

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

Issue 12733003: Adds facilities for extracting Intl.message calls and generating code from translations (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fixes from review comments, also made tests more robust, removed scheduled_test dependency Created 7 years, 9 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 | Annotate | Revision Log
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 'dart:async';
17 import 'intl.dart';
18 import 'src/intl_helpers.dart';
19
20 /**
21 * This is a message lookup mechanism that delegates to one of a collection
22 * of individual [MessageLookupByLibrary] instances.
23 */
24 class CompositeMessageLookup {
25 /** A map from locale names to the corresponding lookups. */
26 Map<String, MessageLookupByLibrary> availableMessages = new Map();
27
28 /** Return true if we have a message lookup for [localeName]. */
29 bool localeExists(localeName) => availableMessages.containsKey(localeName);
30
31 /**
32 * Look up the message with the given [name] and [locale] and return
33 * the translated version with the values in [args] interpolated.
34 * If nothing is found, return [message_str]. The [desc] and [examples]
35 * parameters are ignored
36 */
37 String lookupMessage(String message_str, [final String desc='',
38 final Map examples=const {}, String locale,
39 String name, List<String> args]) {
40 var actualLocale = (locale == null) ? Intl.getCurrentLocale() : locale;
41 // For this usage, if the locale doesn't exist for messages, just return
42 // it and we'll fall back to the original version.
43 var verifiedLocale =
44 Intl.verifiedLocale(
45 actualLocale,
46 localeExists,
47 onFailure: (locale)=>locale);
48 var messages = availableMessages[verifiedLocale];
49 if (messages == null) return message_str;
50 return messages.
51 lookupMessage(message_str, desc, examples, locale, name, args);
52 }
53
54 /**
55 * If we do not already have a locale for [localeName] then
56 * [findLocale] will be called and the result stored as the lookup
57 * mechanism for that locale.
58 */
59 addLocale(String localeName, Function findLocale) {
60 if (localeExists(localeName)) return;
61 var newLocale = findLocale(localeName);
62 if (newLocale != null) {
63 availableMessages[localeName] = 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 true if the locale exists, or if it is null. Null is treated
79 * as meaning that we use the default locale.
80 */
81 bool localeExists(localeName);
82
83 /**
84 * Return the localized version of a message. We are passed the original
85 * version of the message, which consists of a
86 * [message_str] that will be translated, and which may be interpolated
87 * based on one or more variables, a [desc] providing a description of usage
88 * for the [message_str], and a map of [examples] for each data element to be
89 * substituted into the message.
90 *
91 * For example, if message="Hello, $name", then
92 * examples = {'name': 'Sparky'}. If not using the user's default locale, or
93 * if the locale is not easily detectable, explicitly pass [locale].
94 *
95 * The values of [desc] and [examples] are not used at run-time but are only
96 * made available to the translators, so they MUST be simple Strings available
97 * at compile time: no String interpolation or concatenation.
98 * The expected usage of this is inside a function that takes as parameters
99 * the variables used in the interpolated string.
100 *
101 * Ultimately, the information about the enclosing function and its arguments
102 * will be extracted automatically but for the time being it must be passed
103 * explicitly in the [name] and [args] arguments.
104 */
105 String lookupMessage(String message_str, [final String desc='',
106 final Map examples=const {}, String locale,
107 String name, List<String> args]) {
108 // If we don't have a name, return the original, and if we have
109 // been recursively invoked, also just return message_str. This
110 // happens because the replacement functions also call Intl.message,
111 // so we assume that when _lookupInProgress is true that we're
112 // already translated.
113 if (name == null || _lookupInProgress) return message_str;
114 _lookupInProgress = true;
115 // Try to apply the function holding the translated version. If there
116 // is an exception, use the original [message_str] as the result.
117 var result = message_str;
118 try {
119 var function = this[name];
120 if (function != null) result = Function.apply(function, args);
121 } finally {
122 _lookupInProgress = false;
123 }
124 return result;
125 }
126
127 /** Return our message with the given name */
128 operator [](String messageName) => messages[messageName];
129
130 /**
131 * Subclasses should override this to return a list of their message
132 * functions.
133 */
134 Map<String, Function> get messages;
135
136 /** Subclasses should override this to return their locale, e.g. 'en_US' */
137 String get localeName;
138
139 toString() => localeName;
140 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698