Automated g4 rollback of changelist 122487598.
*** Reason for rollback ***
Broke tests
*** Original change description ***
Cache the lookup of messages for a locale.
***
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=122528718
diff --git a/lib/message_lookup_by_library.dart b/lib/message_lookup_by_library.dart
index e2139fc..131c028 100644
--- a/lib/message_lookup_by_library.dart
+++ b/lib/message_lookup_by_library.dart
@@ -23,38 +23,28 @@
/// 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) {
- // 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.
+ String lookupMessage(String message_str, String locale,
+ String name, List args) {
+ var verifiedLocale = findLocale(locale);
+ var messages = availableMessages[verifiedLocale];
if (messages == null) return message_str;
- return messages.lookupMessage(message_str, locale, name, args);
+ return messages.
+ lookupMessage(message_str, locale, name, args);
}
- /// Find the right message lookup for [locale].
- MessageLookupByLibrary _lookupMessageCatalog(String locale) {
- var verifiedLocale = Intl.verifiedLocale(locale, localeExists,
+ /// 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,
onFailure: (locale) => locale);
- _lastLocale = locale;
- _lastLookup = availableMessages[verifiedLocale];
- return _lastLookup;
+ return verifiedLocale;
}
/// If we do not already have a locale for [localeName] then
@@ -67,11 +57,6 @@
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;
- }
}
}
}
@@ -100,8 +85,7 @@
/// 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);