Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions lib/src/l10n/extensions/localizations_ext.dart
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
}
}
46 changes: 23 additions & 23 deletions test/editor/editor_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -158,19 +158,12 @@ void main() {
await tester.pump();

final exception = tester.takeException();
expect(
exception,
isNot(
isInstanceOf<MissingFlutterQuillLocalizationException>(),
),
);

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(
Expand All @@ -180,21 +173,22 @@ void main() {
),
);

await tester.pump();

final exception = tester.takeException();

expect(exception, isNotNull);
expect(
exception,
isA<MissingFlutterQuillLocalizationException>(),
);
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(
Expand All @@ -203,33 +197,39 @@ void main() {
),
);

await tester.pump();

final exception = tester.takeException();

expect(exception, isNull);
expect(
exception,
isNot(isA<MissingFlutterQuillLocalizationException>()),
);
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<MissingFlutterQuillLocalizationException>(),
resolvedMessage,
'The image has been saved at: /tmp/image.png',
);
},
);
Expand Down