From ae677c8fd54ea32c3c2736d9077b8895d443e87b Mon Sep 17 00:00:00 2001 From: James McIntosh Date: Mon, 29 Jan 2024 17:53:13 -0300 Subject: [PATCH] Fix resetting of failed locale lookup when successfully resolving locale in composite message lookup --- pkgs/intl/lib/message_lookup_by_library.dart | 4 +-- pkgs/intl/test/message_lookup_by_library.dart | 31 +++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 pkgs/intl/test/message_lookup_by_library.dart diff --git a/pkgs/intl/lib/message_lookup_by_library.dart b/pkgs/intl/lib/message_lookup_by_library.dart index 3a75d6cd..89ffee05 100644 --- a/pkgs/intl/lib/message_lookup_by_library.dart +++ b/pkgs/intl/lib/message_lookup_by_library.dart @@ -75,8 +75,8 @@ class CompositeMessageLookup implements MessageLookup { if (newLocale != null) { availableMessages[localeName] = newLocale; availableMessages[canonical] = newLocale; - // If there was already a failed lookup for [newLocale], null the cache. - if (_lastLocale == newLocale) { + // If there was already a failed lookup for [localeName], null the cache. + if (_lastLocale == localeName) { _lastLocale = null; _lastLookup = null; } diff --git a/pkgs/intl/test/message_lookup_by_library.dart b/pkgs/intl/test/message_lookup_by_library.dart new file mode 100644 index 00000000..688ec358 --- /dev/null +++ b/pkgs/intl/test/message_lookup_by_library.dart @@ -0,0 +1,31 @@ +// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +library message_lookup_by_library_test; + +import 'package:intl/message_lookup_by_library.dart'; +import 'package:test/test.dart'; + +void main() { + test('Resolves message successfully after unsuccessful lookup', () { + final CompositeMessageLookup lookup = CompositeMessageLookup(); + final lookupMessage = lookup.lookupMessage('Hello', 'pt', 'greeting', null, null); + expect(lookupMessage, 'Hello'); + + lookup.addLocale('pt', (locale) => TestMessageLookupByLibrary('pt', {'greeting': () => 'Bom dia'})); + + final lookupMessage2 = lookup.lookupMessage('Hello', 'pt', 'greeting', null, null); + expect(lookupMessage2, 'Bom dia'); + }); +} + +class TestMessageLookupByLibrary extends MessageLookupByLibrary { + @override + final Map messages; + + @override + final String localeName; + + TestMessageLookupByLibrary(this.localeName, this.messages); +}