Cache the lookup of messages for a locale
Cloned from CL 122487598 by 'g4 patch'.
Original change by alanknight@alanknight:verifiedLocaleFaster:619:citc on 2016/05/16 20:19:19.
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=122680157
diff --git a/lib/generate_localized.dart b/lib/generate_localized.dart
index 8307ecb..b302f45 100644
--- a/lib/generate_localized.dart
+++ b/lib/generate_localized.dart
@@ -172,11 +172,12 @@
/// User programs should call this before using [localeName] for messages.
Future initializeMessages(String localeName) {
- initializeInternalMessageLookup(() => new CompositeMessageLookup());
var lib = _deferredLibraries[Intl.canonicalizedLocale(localeName)];
var load = lib == null ? new Future.value(false) : lib();
- return load.then((_) =>
- messageLookup.addLocale(localeName, _findGeneratedMessagesFor));
+ return load.then((_) {
+ initializeInternalMessageLookup(() => new CompositeMessageLookup());
+ messageLookup.addLocale(localeName, _findGeneratedMessagesFor);
+ });
}
MessageLookupByLibrary _findGeneratedMessagesFor(locale) {
diff --git a/lib/message_lookup_by_library.dart b/lib/message_lookup_by_library.dart
index 131c028..e2139fc 100644
--- a/lib/message_lookup_by_library.dart
+++ b/lib/message_lookup_by_library.dart
@@ -23,28 +23,38 @@
/// Return true if we have a message lookup for [localeName].
bool localeExists(localeName) => availableMessages.containsKey(localeName);
+ /// The last locale in which we looked up messages.
+ ///
+ /// If this locale matches the new one then we can skip looking up the
+ /// messages and assume they will be the same as last time.
+ String _lastLocale;
+
+ /// Caches the last messages that we found
+ MessageLookupByLibrary _lastLookup;
+
/// Look up the message with the given [name] and [locale] and return
/// the translated version with the values in [args] interpolated.
/// If nothing is found, return [message_str]. The [desc] and [examples]
/// parameters are ignored
- String lookupMessage(String message_str, String locale,
- String name, List args) {
- var verifiedLocale = findLocale(locale);
- var messages = availableMessages[verifiedLocale];
+ String lookupMessage(
+ String message_str, String locale, String name, List args) {
+ // If passed null, use the default.
+ var knownLocale = locale ?? Intl.getCurrentLocale();
+ var messages = (knownLocale == _lastLocale)
+ ? _lastLookup
+ : _lookupMessageCatalog(knownLocale);
+ // If we didn't find any messages for this locale, use the original string.
if (messages == null) return message_str;
- return messages.
- lookupMessage(message_str, locale, name, args);
+ return messages.lookupMessage(message_str, locale, name, args);
}
- /// Given an initial locale or null, returns the locale that will be used
- /// for messages.
- String findLocale(String locale) {
- var actualLocale = locale ?? Intl.getCurrentLocale();
- // 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,
+ /// Find the right message lookup for [locale].
+ MessageLookupByLibrary _lookupMessageCatalog(String locale) {
+ var verifiedLocale = Intl.verifiedLocale(locale, localeExists,
onFailure: (locale) => locale);
- return verifiedLocale;
+ _lastLocale = locale;
+ _lastLookup = availableMessages[verifiedLocale];
+ return _lastLookup;
}
/// If we do not already have a locale for [localeName] then
@@ -57,6 +67,11 @@
if (newLocale != null) {
availableMessages[localeName] = newLocale;
availableMessages[canonical] = newLocale;
+ // If there was already a failed lookup for [newLocale], null the cache.
+ if (_lastLocale == newLocale) {
+ _lastLocale = null;
+ _lastLookup = null;
+ }
}
}
}
@@ -85,7 +100,8 @@
/// Ultimately, the information about the enclosing function and its arguments
/// will be extracted automatically but for the time being it must be passed
/// explicitly in the [name] and [args] arguments.
- String lookupMessage(String message_str, String locale, String name, List args) {
+ String lookupMessage(
+ String message_str, String locale, String name, List args) {
if (name == null) return message_str;
var function = this[name];
return function == null ? message_str : Function.apply(function, args);