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

Unified 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: 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 side-by-side diff with in-line comments
Download patch
Index: pkg/intl/lib/message_lookup_by_library.dart
diff --git a/pkg/intl/lib/message_lookup_local.dart b/pkg/intl/lib/message_lookup_by_library.dart
similarity index 52%
copy from pkg/intl/lib/message_lookup_local.dart
copy to pkg/intl/lib/message_lookup_by_library.dart
index e8a0e97ebb09ba3df15a9ebc00fe9ca01a726416..14d73d2a42e1088c668486791c0510a9f4f8912c 100644
--- a/pkg/intl/lib/message_lookup_local.dart
+++ b/pkg/intl/lib/message_lookup_by_library.dart
@@ -11,61 +11,57 @@
*
* See Intl class comment or `tests/message_format_test.dart` for more examples.
*/
- //TODO(efortuna): documentation example involving the offset parameter?
-
-library message_lookup_local;
+library message_lookup_by_library;
import 'dart:async';
import 'intl.dart';
import 'src/intl_helpers.dart';
-import 'dart:mirrors';
-/**
- * Initialize the user messages for [localeName]. Note that this is an ASYNC
- * operation. This must be called before attempting to use messages in
- * [localeName].
- */
-Future initializeMessages(localeName, [String source = 'messages_']) {
- initializeInternalMessageLookup(
- () => new MessageLookupLocal(localeName, source));
- _initializeMessagesForLocale(localeName);
- return new Future.immediate(null);
-}
+class CompositeMessageLookup {
Emily Fortuna 2013/03/13 18:54:43 some documentation explaining this class, please?
Alan Knight 2013/03/14 17:49:05 Done.
+ Map<String,MessageLookupByLibrary> availableMessages = new Map();
-void _initializeMessagesForLocale(String localeName) {}
+ bool localeExists(localeName) => availableMessages.containsKey(localeName);
-class MessageLookupLocal {
- /** Prevent infinite recursion when looking up the message. */
- bool _lookupInProgress = false;
-
- /** The libraries we can look in for internationalization messages. */
- Map<String, LibraryMirror> _libraries;
-
- /** The prefix used to find libraries that contain localized messages.
- * So, if this is 'messages_' we would look for messages for the locale
- * 'pt_BR' in a library named 'messages_pt_BR'.
- */
- String _sourcePrefix;
+ lookupMessage(String message_str, [final String desc='',
+ final Map examples=const {}, String locale,
+ String name, List<String> args]) {
+ var actualLocale = (locale == null) ? Intl.getCurrentLocale() : locale;
+ // For this usage, if the locale doesn't exist for messages, just return
Emily Fortuna 2013/03/13 18:54:43 random indentation?
Alan Knight 2013/03/14 17:49:05 Not random, I put each argument to the function on
Emily Fortuna 2013/03/14 19:45:00 No, that's totally fine. I was complaining that li
Alan Knight 2013/03/14 20:50:04 Doh! Done.
+ // it and we'll fall back to the original version.
+ var verifiedLocale =
+ Intl.verifiedLocale(
+ actualLocale,
+ localeExists,
+ onFailure: (locale)=>locale);
+ var messages = availableMessages[verifiedLocale];
+ if (messages == null) return message_str;
+ return messages.
+ lookupMessage(message_str, desc, examples, locale, name, args);
+ }
- /**
- * Constructor. The [localeName] is of the form 'en' or 'en_US'.
- *The [source] parameter defines the prefix that is used to find
- * libraries that contain localized messages. So with the default value of
- * 'messages_', we would look for messages for the locale 'pt_BR' in a library
- * named 'messages_pt_BR'.
- */
- MessageLookupLocal(String localeName, this._sourcePrefix) {
- _libraries = currentMirrorSystem().libraries;
+ addLocale(String localeName, Function findLocale) {
+ if (localeExists(localeName)) return;
+ var newLocale = findLocale(localeName);
+ if (newLocale != null) {
+ availableMessages[localeName] = newLocale;
+ }
}
+}
+
+/**
+ * This provides an abstract class for messages looked up in generated code.
+ * Each locale will have a separate subclass of this class with its set of
+ * messages. See generate_localized.dart.
+ */
+abstract class MessageLookupByLibrary {
+ /** Prevent infinite recursion when looking up the message. */
+ bool _lookupInProgress = false;
/**
- * Return true if the locale exists, or if it is null. The null case
- * is interpreted to mean that we use the default locale.
+ * Return true if the locale exists, or if it is null. Null is treated
+ * as meaning that we use the default locale.
*/
- bool localeExists(localeName) {
- if (localeName == null) return false;
- return _libraries['$_sourcePrefix$localeName'] != null;
- }
+ bool localeExists(localeName);
/**
* Return the localized version of a message. We are passed the original
@@ -89,35 +85,30 @@ class MessageLookupLocal {
* will be extracted automatically but for the time being it must be passed
* explicitly in the [name] and [args] arguments.
*/
- Future<String> lookupMessage(String message_str, [final String desc='',
+ lookupMessage(String message_str, [final String desc='',
Emily Fortuna 2013/03/13 18:54:43 list return type? String?
Alan Knight 2013/03/14 17:49:05 Done.
final Map examples=const {}, String locale,
String name, List<String> args]) {
- if (name == null) return new Future.immediate(message_str);
+ if (name == null) return message_str;
// The translations also make use of Intl.message, so we need to not
// recurse and just stop when we find the first substitution.
- if (_lookupInProgress) return new Future.immediate(message_str);
+ if (_lookupInProgress) return message_str;
Emily Fortuna 2013/03/13 18:54:43 can we combine this branch with line 91, so we hav
Alan Knight 2013/03/14 17:49:05 Done.
_lookupInProgress = true;
var result;
try {
- var actualLocale = (locale == null) ? Intl.getCurrentLocale() : locale;
- // For this usage, if the locale doesn't exist for messages, just return
- // it and we'll fall back to the original version.
- var verifiedLocale =
- Intl.verifiedLocale(
- actualLocale,
- localeExists,
- onFailure: (locale)=>locale);
- LibraryMirror messagesForThisLocale =
- _libraries['$_sourcePrefix$verifiedLocale'];
- if (messagesForThisLocale == null) {
- return new Future.immediate(message_str);
- }
- MethodMirror localized = messagesForThisLocale.functions[name];
- if (localized == null) return new Future.immediate(message_str);
- result = messagesForThisLocale.invoke(localized.simpleName, args);
+ var function = this[name];
+ if (function == null) return message_str;
Emily Fortuna 2013/03/13 18:54:43 suggestion: instead of eagerly returning here, run
Alan Knight 2013/03/14 17:49:05 Re-organized a bit more than even that so now ther
+ result = Function.apply(function, args);
} finally {
_lookupInProgress = false;
}
- return result.then((value) => value.reflectee);
+ return result == null ? message_str : result;
}
-}
+
+ operator [](x) => messages[x];
+
+ get messages;
Emily Fortuna 2013/03/13 18:54:43 don't these need implementation? Return types? doc
Alan Knight 2013/03/14 17:49:05 They are to be implemented by the generated subcla
Emily Fortuna 2013/03/14 19:45:00 sgtm.
+
+ get localeName;
+
+ toString() => localeName;
+}

Powered by Google App Engine
This is Rietveld 408576698