diff --git a/lib/src/l10n/extensions/localizations_ext.dart b/lib/src/l10n/extensions/localizations_ext.dart index 91128d659..5694bd313 100644 --- a/lib/src/l10n/extensions/localizations_ext.dart +++ b/lib/src/l10n/extensions/localizations_ext.dart @@ -1,7 +1,12 @@ import 'package:flutter/widgets.dart' show BuildContext; import '../generated/quill_localizations.dart'; +import '../generated/quill_localizations_en.dart'; +@Deprecated( + 'FlutterQuill now falls back to English strings when the localization ' + 'delegate is missing. This exception will be removed in a future release.', +) class MissingFlutterQuillLocalizationException extends UnimplementedError { MissingFlutterQuillLocalizationException(); @override @@ -13,11 +18,14 @@ class MissingFlutterQuillLocalizationException extends UnimplementedError { } extension LocalizationsExt on BuildContext { - /// Require the [FlutterQuillLocalizations] instance. + static final FlutterQuillLocalizations _fallbackLocalization = + FlutterQuillLocalizationsEn(); + + /// Retrieve the [FlutterQuillLocalizations] instance, falling back to the + /// default English messages if no delegate is registered. /// - /// `loc` is short for `localizations` + /// `loc` is short for `localizations`. FlutterQuillLocalizations get loc { - return FlutterQuillLocalizations.of(this) ?? - (throw MissingFlutterQuillLocalizationException()); + return FlutterQuillLocalizations.of(this) ?? _fallbackLocalization; } } diff --git a/test/editor/editor_test.dart b/test/editor/editor_test.dart index c5d34a8cf..c1330332a 100644 --- a/test/editor/editor_test.dart +++ b/test/editor/editor_test.dart @@ -158,19 +158,12 @@ void main() { await tester.pump(); final exception = tester.takeException(); - expect( - exception, - isNot( - isInstanceOf(), - ), - ); - expect(exception, isNull); }, ); testWidgets( - 'should throw MissingFlutterQuillLocalizationException if the delegate not provided', + 'falls back to English localization if the delegate is not provided', (tester) async { await tester.pumpWidget( MaterialApp( @@ -180,21 +173,22 @@ void main() { ), ); + await tester.pump(); + final exception = tester.takeException(); - expect(exception, isNotNull); - expect( - exception, - isA(), - ); + expect(exception, isNull); + expect(find.text('Font'), findsOneWidget); }, ); testWidgets( - 'should not throw MissingFlutterQuillLocalizationException if the delegate is provided', + 'uses the provided localization delegate when available', (tester) async { await tester.pumpWidget( MaterialApp( + locale: const Locale('es'), + supportedLocales: FlutterQuillLocalizations.supportedLocales, localizationsDelegates: FlutterQuillLocalizations.localizationsDelegates, home: Builder( @@ -203,33 +197,39 @@ void main() { ), ); + await tester.pump(); + final exception = tester.takeException(); expect(exception, isNull); - expect( - exception, - isNot(isA()), - ); + expect(find.text('Fuente'), findsOneWidget); }, ); testWidgets( - 'should throw MissingFlutterQuillLocalizationException if the delegate is not provided', + 'fallback localization provides interpolated messages when delegate is missing', (tester) async { + late String resolvedMessage; await tester.pumpWidget( MaterialApp( home: Builder( - builder: (context) => Text(context.loc.font), + builder: (context) { + resolvedMessage = + context.loc.theImageHasBeenSavedAt('/tmp/image.png'); + return const SizedBox.shrink(); + }, ), ), ); + await tester.pump(); + final exception = tester.takeException(); - expect(exception, isNotNull); + expect(exception, isNull); expect( - exception, - isA(), + resolvedMessage, + 'The image has been saved at: /tmp/image.png', ); }, );